CSS实现开灯关灯的效果

1. html文件

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="index.css" />
    <title>Document</title>
  </head>
  <body>
    <div id="lampadario">
      <input type="radio" name="switch" value="on" />
      <input type="radio" name="switch" value="off" checked="checked" />
      <label for="switch"></label>
      <div id="filo"></div>
      <div id="lampadina">
        <div id="sorpresa">
          <div id="footer">一键三连!!!</div>
          <div id="shadow">一键三连!!!</div>
        </div>
      </div>
    </div>
  </body>
</html>

2. css文件(index.css)

html,
body {
  font-family: Georgia, serif;
  font-style: italic;
  font-size: 4.2vmin;
  line-height: 4.7vmin;
  color: rgba(255, 255, 255, 0.6);
  margin: 0;
  padding: 0;
  height: 100%;
  background: #422747;
  background: -webkit-gradient(left top, left bottom, color-stop(0%, #422747), color-stop(73%, #2b192e), color-stop(100%, #130b14));
  background: -webkit-gradient(linear, left top, left bottom, from(#422747), color-stop(73%, #2b192e), to(#130b14));
  background: linear-gradient(to bottom, #422747 0%, #2b192e 73%, #130b14 100%);
}

a {
  color: rgba(255, 255, 255, 0.6);
}

#lampadario {
  position: fixed;
  left: 50%;
  top: 0;
}

#filo {
  position: relative;
  background-color: #000000;
  width: 2px;
  height: 150px;
  left: 50%;
  margin-left: -1px;
  z-index: 1;
  -webkit-transform-origin: 0% 0%;
  transform-origin: 0% 0%;
  -webkit-animation: oscillaFilo 0.9s ease-in-out 0s infinite alternate;
  animation: oscillaFilo 0.9s ease-in-out 0s infinite alternate;
}

#filo:after {
  content: " ";
  left: -5px;
  top: 100%;
  position: absolute;
  border-bottom: 15px solid #000000;
  border-left: 4px solid transparent;
  border-right: 4px solid transparent;
  height: 0;
  width: 4px;
}

#lampadina {
  position: relative;
}

input[value="off"]:checked ~ #filo {
  -webkit-box-shadow: -80px -10px 7px 0 rgba(0, 0, 0, 0.1);
  -ms-box-shadow: -80px -10px 7px 0 rgba(0, 0, 0, 0.1);
  -o-box-shadow: -80px -10px 7px 0 rgba(0, 0, 0, 0.1);
  box-shadow: -80px -10px 7px 0 rgba(0, 0, 0, 0.1);
}

input[value="off"]:checked ~ #filo:after {
  -webkit-box-shadow: -80px -10px 10px -2px rgba(0, 0, 0, 0.1);
  -ms-box-shadow: -80px -10px 10px -2px rgba(0, 0, 0, 0.1);
  -o-box-shadow: -80px -10px 10px -2px rgba(0, 0, 0, 0.1);
  box-shadow: -80px -10px 10px -2px rgba(0, 0, 0, 0.1);
}

input {
  position: absolute;
  width: 90px;
  height: 70px;
  top: 150px;
  margin-left: -45px;
  opacity: 0;
  z-index: 1;
  cursor: pointer;
}

input[value="on"] {
  top: 150px;
}

input[value="off"] {
  top: -100px;
}

input[value="on"]:checked {
  top: -100px;
}

input[value="on"]:checked + input[value="off"] {
  top: 150px;
}

label {
  width: 51px;
  height: 51px;
  top: 164px;
  position: absolute;
  left: 0;
  margin-left: -24px;
  border-radius: 100%;
  -webkit-animation: oscillaLampadina 0.9s ease-in-out 0s infinite alternate;
  animation: oscillaLampadina 0.9s ease-in-out 0s infinite alternate;
}

input[value="off"]:checked ~ label {
  background: rgba(255, 255, 255, 0.03);
  -webkit-box-shadow: inset 0px 1px 5px rgba(255, 255, 255, 0.1), inset 0px 2px 20px rgba(255, 255, 255, 0.07), -80px -15px 15px -5px rgba(0, 0, 0, 0.1);
  -ms-box-shadow: inset 0px 1px 5px rgba(255, 255, 255, 0.1), inset 0px 2px 20px rgba(255, 255, 255, 0.07), -80px -15px 15px -5px rgba(0, 0, 0, 0.1);
  -o-box-shadow: inset 0px 1px 5px rgba(255, 255, 255, 0.1), inset 0px 2px 20px rgba(255, 255, 255, 0.07), -80px -15px 15px -5px rgba(0, 0, 0, 0.1);
  box-shadow: inset 0px 1px 5px rgba(255, 255, 255, 0.1), inset 0px 2px 20px rgba(255, 255, 255, 0.07), -80px -15px 15px -5px rgba(0, 0, 0, 0.1);
}

input[value="on"]:checked ~ label {
  background: white;
  -webkit-box-shadow: 0px 0px 10px rgba(255, 255, 255, 0.8), 0px 0px 30px rgba(255, 255, 255, 0.8), 0px 0px 50px rgba(255, 255, 255, 0.6), 0px 0px 70px rgba(255, 255, 255, 0.6), -80px -15px 120px 0px rgba(255, 255, 255, 0.4);
  -ms-box-shadow: 0px 0px 10px rgba(255, 255, 255, 0.8), 0px 0px 30px rgba(255, 255, 255, 0.8), 0px 0px 50px rgba(255, 255, 255, 0.6), 0px 0px 70px rgba(255, 255, 255, 0.6), -80px -15px 120px 0px rgba(255, 255, 255, 0.4);
  -o-box-shadow: 0px 0px 10px rgba(255, 255, 255, 0.8), 0px 0px 30px rgba(255, 255, 255, 0.8), 0px 0px 50px rgba(255, 255, 255, 0.6), 0px 0px 70px rgba(255, 255, 255, 0.6), -80px -15px 120px 0px rgba(255, 255, 255, 0.4);
  box-shadow: 0px 0px 10px rgba(255, 255, 255, 0.8), 0px 0px 30px rgba(255, 255, 255, 0.8), 0px 0px 50px rgba(255, 255, 255, 0.6), 0px 0px 70px rgba(255, 255, 255, 0.6), -80px -15px 120px 0px rgba(255, 255, 255, 0.4);
}

input[value="off"]:checked ~ label:after {
  content: " ";
  width: 15px;
  height: 15px;
  top: 0;
  position: absolute;
  left: 0;
  margin-left: 15px;
  border-radius: 100%;
  border: 2px solid rgba(255, 255, 255, 0.03);
}

#sorpresa {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
}

input[value="off"]:checked ~ #lampadina #sorpresa {
  opacity: 0;
}

input[value="on"]:checked ~ #lampadina #sorpresa {
  opacity: 1;
}

#footer {
  position: fixed;
  width: 94%;
  text-align: center;
  bottom: 30%;
  padding: 0 3%;
  z-index: 1;
}

#shadow {
  position: fixed;
  width: 94%;
  text-align: center;
  bottom: 29%;
  padding: 0 3%;
  color: rgba(0, 0, 0, 0);
  -webkit-text-shadow: 0px 0px 4px rgba(0, 0, 0, 0.3);
  -moz-text-shadow: 0px 0px 4px rgba(0, 0, 0, 0.3);
  -ms-text-shadow: 0px 0px 4px rgba(0, 0, 0, 0.3);
  -o-text-shadow: 0px 0px 4px rgba(0, 0, 0, 0.3);
  text-shadow: 0px 0px 4px rgba(0, 0, 0, 0.3);
  -webkit-transform-origin: 50% 0px;
  transform-origin: 50% 0px;
  -webkit-animation: ombraTesto 0.9s ease-in-out 0s infinite alternate;
  animation: ombraTesto 0.9s ease-in-out 0s infinite alternate;
}

@-webkit-keyframes oscillaFilo {
  from {
    -webkit-transform: rotate(5deg);
  }
  to {
    -webkit-transform: rotate(-5deg);
  }
}

@keyframes oscillaFilo {
  from {
    -webkit-transform: rotate(5deg);
            transform: rotate(5deg);
  }
  to {
    -webkit-transform: rotate(-5deg);
            transform: rotate(-5deg);
  }
}

@-webkit-keyframes oscillaLampadina {
  from {
    -webkit-transform: rotate(3deg) translate(-16.4px, -1px);
  }
  to {
    -webkit-transform: rotate(-3deg) translate(16.4px, -1px);
  }
}

@keyframes oscillaLampadina {
  from {
    -webkit-transform: rotate(3deg) translate(-16.4px, -1px);
            transform: rotate(3deg) translate(-16.4px, -1px);
  }
  to {
    -webkit-transform: rotate(-3deg) translate(16.4px, -1px);
            transform: rotate(-3deg) translate(16.4px, -1px);
  }
}

@-webkit-keyframes ombraTesto {
  from {
    -webkit-transform: translate(1px, 0px) scale(1.01, 1.06) skew(0.9deg, 0deg);
  }
  to {
    -webkit-transform: translate(-1px, 0px) scale(1.01, 1.06) skew(-0.9deg, 0deg);
  }
}

@keyframes ombraTesto {
  from {
    -webkit-transform: translate(1px, 0px) scale(1.01, 1.06) skew(0.9deg, 0deg);
            transform: translate(1px, 0px) scale(1.01, 1.06) skew(0.9deg, 0deg);
  }
  to {
    -webkit-transform: translate(-1px, 0px) scale(1.01, 1.06) skew(-0.9deg, 0deg);
            transform: translate(-1px, 0px) scale(1.01, 1.06) skew(-0.9deg, 0deg);
  }
}

3. 效果

点击灯泡灯亮了
image
再次点击灯泡,灯就被关了
image

原文地址:http://www.cnblogs.com/Fantasyfzg/p/16877764.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性