逼真红色开关 Realistic Red Switch (Pure CSS)

逼真红色开关Realistic Red Switch (Pure CSS)

更多有趣示例 尽在知屋安砖社区

示例

在这里插入图片描述

HTML

<label class="switch">
  <input type="checkbox" checked/>
  <div class="button">
    <div class="light"></div>
    <div class="dots"></div>
    <div class="characters"></div>
    <div class="shine"></div>
    <div class="shadow"></div>
  </div>
</label>

CSS

$rotation: 25deg;
$pivot-distance: 20px;
$width: 150px;
$height: 1.3 * $width;

body, html {
  width: 100%;
  height: 100%;
}

body {
  background-color: #fafcff;
  background-image: radial-gradient(circle, transparent, #afb5bf), linear-gradient(transparent, #d3d8e0);
  display: flex;
  align-items: center;
  justify-content: center;
}

.switch {
  background-color: black;
  width: $width;
  height: $height;
  box-shadow: 
    0 0 10px 2px rgba(black, 0.2), 
    0 0 1px 2px black, 
    inset 0 2px 2px -2px white, 
    inset 0 0 2px 15px #47434c, 
    inset 0 0 2px 22px black;
  border-radius: 5px;
  padding: 20px;
  perspective: 700px;
  
  input {
    display: none;
    
    &:checked + .button {
      transform: translateZ($pivot-distance) rotateX($rotation);
      box-shadow: 0 -10px 20px rgb(255 2 2);
      
      .light {
        animation: flicker 0.2s infinite 0.3s;
      }
      
      .shine {
        opacity: 1;
      }
      
      .shadow {
        opacity: 0;
      }
    }
  }
  
  .button {
    transition: all 0.3s cubic-bezier(1, 0, 1, 1);
    transform-origin: center center -#{$pivot-distance};
    transform: translateZ($pivot-distance) rotateX(-$rotation);
    transform-style: preserve-3d;
    background-color: #9b0621;
    width: 100%;
    height: 100%;
    position: relative;
    cursor: pointer;
    background: linear-gradient(#9b0621 0%, #6d0719 30%, #6d0719 70%, #9b0621 100%); // Red background
    background-repeat: no-repeat;
    
    &::before {
      content: "";
      background: linear-gradient(rgba(white, 0.8) 20%, #650c1d 55%, #3c0610) 50% 50% / 97% 97%, #9b0621;
      background-repeat: no-repeat;
      width: 100%;
      height: 50px;
      transform-origin: top;
      transform: rotateX(-90deg);
      position: absolute;
      top: 0;
    }
    
    &::after {
      content: "";
      background-image: linear-gradient(#650c1d, #3c0610);
      width: 100%;
      height: 50px;
      transform-origin: top;
      transform: translateY(50px) rotateX(-90deg);
      position: absolute;
      bottom: 0;
      box-shadow: 0 50px 8px 0px black, 0 80px 20px 0px rgba(black, 0.5);
    }
  }
  
  .light {
    opacity: 0;
    animation:  light-off 1s;
    position: absolute;
    width: 100%;
    height: 100%;
    background-image: 
      radial-gradient(#f8b64b, transparent 40%),
      radial-gradient(circle, #ff1818 50%, transparent 80%);
  }
  
  .dots {
    position: absolute;
    width: 100%;
    height: 100%;
    background-image: radial-gradient(transparent 30%, rgba(#650c1d, 0.7) 70%);
    background-size: 10px 10px;
  }
  
  .characters {
    position: absolute;
    width: 100%;
    height: 100%;
    
    // Drawing with gradients!
    // Read more about it here: https://css-tricks.com/drawing-images-with-css-gradients/
    background:
      linear-gradient(white, white) 50% 20% / 5% 20%, // White vertical line
      radial-gradient(circle, transparent 50%, white 52%, white 70%, transparent 72%) 50% 80% / 33% 25%; // White circle
    background-repeat: no-repeat;
  }
  
  .shine {
    transition: all 0.3s cubic-bezier(1, 0, 1, 1);
    opacity: 0.3;
    position: absolute;
    width: 100%;
    height: 100%;
    background: 
      linear-gradient(white, transparent 3%) 50% 50% / 97% 97%,
      linear-gradient(rgba(white, 0.5), transparent 50%, transparent 80%, rgba(white, 0.5)) 50% 50% / 97% 97%;
    background-repeat: no-repeat;
  }
  
  .shadow {
    transition: all 0.3s cubic-bezier(1, 0, 1, 1);
    opacity: 1;
    position: absolute;
    width: 100%;
    height: 100%;
    background: 
      linear-gradient(transparent 70%, rgba(black, 0.8));
    background-repeat: no-repeat;
  }
}

@keyframes flicker {
  0% {opacity: 1}
  80% {opacity: 0.8}
  100% {opacity: 1}
}

@keyframes light-off {
  0% {opacity: 1}
  80% {opacity: 0}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值