CSS实现开灯关灯的效果

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

posted @ 2022-11-10 17:19  合起来的彳亍  阅读(170)  评论(0编辑  收藏  举报