html+css+js实现盒子

[复制链接]
发表于 前天 12:11 | 显示全部楼层 |阅读模式
结果图:


代码
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.   <head>
  4.     <meta charset="UTF-8" />
  5.     <title>禁止打开盖子</title>
  6.     <style>
  7.       * {
  8.         box-sizing: border-box;
  9.         -webkit-font-smoothing: antialiased;
  10.         text-rendering: optimizeLegibility;
  11.         scroll-behavior: smooth;
  12.       }
  13.       html,
  14.       body {
  15.         height: 100%;
  16.         overflow: hidden;
  17.       }
  18.       body {
  19.         display: flex;
  20.         flex-direction: row;
  21.         flex-wrap: wrap;
  22.         justify-content: center;
  23.         align-items: center;
  24.         margin: 0;
  25.         background: #cecece;
  26.       }
  27.       .box {
  28.         width: 20em;
  29.         height: 12em;
  30.         background: #0f0f0f;
  31.         overflow: hidden;
  32.         border-radius: 1em;
  33.         cursor: not-allowed;
  34.         position: relative;
  35.         margin: 1em;
  36.         border: 0;
  37.         box-shadow: 0.1em 0.1em 0.2em 0 #fff3, -0.1em -0.1em 0.2em 0 #0003;
  38.       }
  39.       .box .inset {
  40.         background: #cecece;
  41.         border-radius: 0.9em;
  42.         width: 100%;
  43.         height: 100%;
  44.         position: relative;
  45.         box-shadow: inset 0em 0em 0em 0em #0006, inset 0.1em 0.1em 0.2em 0 #fff3,
  46.           inset -0.1em -0.1em 0.2em 0 #0003;
  47.       }
  48.       .box .inset::before {
  49.         content: '';
  50.         display: block;
  51.         width: 0.8em;
  52.         height: 7em;
  53.         box-shadow: inset -0.1em -0.1em 0.1em 0.1em #fff3, inset 0.1em 0.1em
  54.             0.1em 0.1em #0003;
  55.         border-radius: 10px;
  56.         position: absolute;
  57.         top: 0;
  58.         bottom: 0;
  59.         margin: auto;
  60.         left: 1em;
  61.       }
  62.       .box.moving {
  63.         pointer-events: none;
  64.       }
  65.       .box.pushed .inset {
  66.         animation: pushed 2s ease-in-out forwards;
  67.       }
  68.       .box.reverse .inset {
  69.         animation: pushed 0.6s ease-in-out reverse forwards;
  70.       }
  71.       @keyframes pushed {
  72.         0% {
  73.           box-shadow: inset 0em 0em 0em 0em #0006, inset 0.1em 0.1em 0.2em 0
  74.               #fff3, inset -0.1em -0.1em 0.2em 0 #0003;
  75.           scale: 1;
  76.           translate: 0%;
  77.         }
  78.         24% {
  79.           box-shadow: inset 1.6em 1.6em 1em 0em #0006, inset 0.1em 0.1em 0.2em 0
  80.               #fff3, inset -0.1em -0.1em 0.2em 0 #0003;
  81.           translate: 0%;
  82.           scale: 0.9;
  83.         }
  84.         100% {
  85.           box-shadow: inset -2.8em 1.6em 1em 0em #0006, inset 0.1em 0.1em 0.2em
  86.               0 #fff3, inset -0.1em -0.1em 0.2em 0 #0003;
  87.           translate: 88%;
  88.           scale: 0.9;
  89.         }
  90.       }
  91.       .box .inset::after {
  92.         content: '';
  93.         display: block;
  94.         width: 100%;
  95.         height: 100%;
  96.         left: 0;
  97.         right: 0;
  98.         top: 0;
  99.         bottom: 0;
  100.         margin: auto;
  101.         background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64"><path d="M32,62A30,30,0,1,0,2,32,30,30,0,0,0,32,62ZM32,4A28,28,0,1,1,4,32,28,28,0,0,1,32,4Z"/><path d="M13.4 49.74h0a1 1 0 0 0 .71-.29L49.45 14.14a1 1 0 0 0 0-1.45A26 26 0 0 0 12.69 49.41 1 1 0 0 0 13.4 49.74zM26 32V17.5a1.5 1.5 0 0 1 3 0V31.76l-8 8V20.5a1.5 1.5 0 0 1 3 0V32zM37.5 16A1.5 1.5 0 0 1 39 17.5v4.26l-3 3V17.5A1.5 1.5 0 0 1 37.5 16zM34 17.5v9.26l-3 3V14.5a1.5 1.5 0 0 1 3 0zM32 8a24 24 0 0 1 15.27 5.48L41 19.76V17.5a3.47 3.47 0 0 0-5-3.14A3.53 3.53 0 0 0 35 12a3.5 3.5 0 0 0-6 2.34A3.36 3.36 0 0 0 27.5 14 3.5 3.5 0 0 0 24 17.36 3.36 3.36 0 0 0 22.5 17 3.5 3.5 0 0 0 19 20.5V41.76l-5.52 5.51A24 24 0 0 1 32 8zM15.63 50.78a1 1 0 0 0 .08 1.49A26 26 0 0 0 52.27 15.71a1 1 0 0 0-1.49-.08L20.45 46zM51.39 17.85A24 24 0 0 1 17.85 51.39L21 48.27A11 11 0 0 0 30 53h1.67a11 11 0 0 0 8-3.48L50.17 38.35A3.72 3.72 0 0 0 48 32.12a3.66 3.66 0 0 0-2.75.71L41 36V28.24zm-11.84 21a1 1 0 0 0 1.05-.09l5.83-4.37a1.7 1.7 0 0 1 2.39.33 1.74 1.74 0 0 1 .35 1A1.72 1.72 0 0 1 48.71 37L38.23 48.16A9 9 0 0 1 31.67 51H30a8.94 8.94 0 0 1-7.57-4.18L39 30.24V38A1 1 0 0 0 39.55 38.89z"/></svg>');
  102.         background-position: 50% 50%;
  103.         background-size: 24%;
  104.         background-repeat: no-repeat;
  105.         position: absolute;
  106.         opacity: 0.05;
  107.         transition: all 0.2s ease-in-out;
  108.       }
  109.       .box .inset:hover::after {
  110.         animation: red-glow 2s infinite ease-out;
  111.       }
  112.       @keyframes red-glow {
  113.         0% {
  114.           opacity: 0.1;
  115.           filter: invert(10%) sepia(90%) saturate(4763%) hue-rotate(357deg)
  116.             brightness(140%) contrast(114%) drop-shadow(0 0 0px red);
  117.         }
  118.         50% {
  119.           opacity: 0.3;
  120.           filter: invert(10%) sepia(90%) saturate(4763%) hue-rotate(357deg)
  121.             brightness(140%) contrast(114%) drop-shadow(0 0 4px red);
  122.         }
  123.         100% {
  124.           opacity: 0.1;
  125.           filter: invert(10%) sepia(90%) saturate(4763%) hue-rotate(357deg)
  126.             brightness(140%) contrast(114%) drop-shadow(0 0 0px red);
  127.         }
  128.       }
  129.       .diamond {
  130.         --_size: 2em;
  131.         --_ratio: 1.8;
  132.         --_speed: 2s;
  133.         --_clr-main: hsl(0, 100%, 50%);
  134.         --_clr-light: hsl(0, 100%, 90%);
  135.         --_clr-dark: hsl(0, 100%, 20%);
  136.         width: 0;
  137.         height: 0;
  138.         border: var(--_size) solid transparent;
  139.         border-bottom: calc(var(--_ratio) * var(--_size)) solid var(--_clr-main);
  140.         top: calc(0px - var(--_size));
  141.         animation: hueAni 12s linear infinite;
  142.         position: absolute;
  143.         left: 0;
  144.         right: 0;
  145.         top: 0;
  146.         bottom: 50%;
  147.         margin: auto;
  148.         transform-origin: 50% 100%;
  149.         opacity: 0.8;
  150.         transition: all 0.4s ease-in-out;
  151.       }
  152.       .box .diamond:hover {
  153.         opacity: 1;
  154.         scale: 1.2;
  155.       }
  156.       @keyframes hueAni {
  157.         0% {
  158.           filter: hue-rotate(0deg);
  159.           rotate: 0;
  160.         }
  161.         100% {
  162.           filter: hue-rotate(360deg);
  163.           rotate: -360deg;
  164.         }
  165.       }
  166.       .diamond::after {
  167.         content: '';
  168.         display: block;
  169.         position: absolute;
  170.         width: 4em;
  171.         left: -2em;
  172.         height: 7.2em;
  173.         z-index: -1;
  174.         border-radius: 100%;
  175.         background: var(--_clr-main);
  176.         opacity: 1;
  177.         filter: blur(20px);
  178.       }
  179.       .diamond::before {
  180.         content: '';
  181.         position: absolute;
  182.         left: calc(0px - var(--_size));
  183.         top: calc(var(--_ratio) * var(--_size) - 0px);
  184.         width: 0;
  185.         height: 0;
  186.         border: var(--_size) solid transparent;
  187.         border-top: calc(var(--_ratio) * var(--_size)) solid var(--_clr-main);
  188.       }
  189.       .diamond .t {
  190.         position: absolute;
  191.         right: calc(0.1px - var(--_size));
  192.         top: 0px;
  193.         width: 0;
  194.         height: 0;
  195.         border-bottom: calc(var(--_ratio) * var(--_size)) solid var(
  196.             --_clr-light
  197.           );
  198.         border-right: var(--_size) solid transparent;
  199.       }
  200.       .diamond .t::before {
  201.         content: '';
  202.         position: absolute;
  203.         left: calc(1px - var(--_size));
  204.         top: 0px;
  205.         width: 0;
  206.         height: 0;
  207.         border-left: var(--_size) solid transparent;
  208.         transform-origin: 100% 0%;
  209.         animation: ani-t var(--_speed) infinite linear reverse, ani-t2 var(
  210.               --_speed
  211.             ) infinite linear;
  212.       }
  213.       .diamond .t::after {
  214.         content: '';
  215.         position: absolute;
  216.         right: calc(0px - var(--_size));
  217.         top: 0px;
  218.         width: 0;
  219.         height: 0;
  220.         border-right: var(--_size) solid transparent;
  221.         transform-origin: 0% 0%;
  222.         animation: ani-t var(--_speed) infinite linear, ani-t2 var(--_speed) infinite
  223.             linear;
  224.       }
  225.       @keyframes ani-t {
  226.         0% {
  227.           transform: scaleX(0);
  228.         }
  229.         100% {
  230.           transform: scaleX(1);
  231.         }
  232.       }
  233.       @keyframes ani-t2 {
  234.         0% {
  235.           border-bottom: calc(var(--_ratio) * var(--_size)) solid var(
  236.               --_clr-main
  237.             );
  238.         }
  239.         100% {
  240.           border-bottom: calc(var(--_ratio) * var(--_size)) solid var(
  241.               --_clr-light
  242.             );
  243.         }
  244.       }
  245.       .diamond .b {
  246.         position: absolute;
  247.         left: calc(0.1px - var(--_size));
  248.         top: calc(var(--_ratio) * var(--_size));
  249.         width: 0;
  250.         height: 0;
  251.         border-top: calc(var(--_ratio) * var(--_size)) solid var(--_clr-dark);
  252.         border-left: var(--_size) solid transparent;
  253.       }
  254.       .diamond .b::before {
  255.         content: '';
  256.         position: absolute;
  257.         left: calc(1px - var(--_size));
  258.         top: calc(0px - var(--_size) * var(--_ratio));
  259.         width: 0;
  260.         height: 0;
  261.         border-left: var(--_size) solid transparent;
  262.         transform-origin: 100% 0%;
  263.         animation: ani-b var(--_speed) infinite linear reverse, ani-b2 var(
  264.               --_speed
  265.             ) infinite linear;
  266.       }
  267.       .diamond .b::after {
  268.         content: '';
  269.         position: absolute;
  270.         right: calc(0px - var(--_size));
  271.         top: calc(0px - var(--_size) * var(--_ratio));
  272.         width: 0;
  273.         height: 0;
  274.         border-right: var(--_size) solid transparent;
  275.         transform-origin: 0% 0%;
  276.         animation: ani-b var(--_speed) infinite linear, ani-b2 var(--_speed) infinite
  277.             linear;
  278.       }
  279.       @keyframes ani-b {
  280.         0% {
  281.           transform: scaleX(0);
  282.         }
  283.         100% {
  284.           transform: scaleX(1);
  285.         }
  286.       }
  287.       @keyframes ani-b2 {
  288.         0% {
  289.           border-top: calc(var(--_ratio) * var(--_size)) solid var(--_clr-dark);
  290.         }
  291.         100% {
  292.           border-top: calc(var(--_ratio) * var(--_size)) solid var(--_clr-main);
  293.         }
  294.       }
  295.     </style>
  296.   </head>
  297.   <body>
  298.     <div class="box">
  299.       <div class="diamond">
  300.         <span class="t"></span>
  301.         <span class="b"></span>
  302.       </div>
  303.       <div class="inset"></div>
  304.     </div>
  305.     <script>
  306.       const box = document.querySelector('.box');
  307.       let isPushed = false;
  308.       box.addEventListener('click', () => {
  309.         if (box.classList.contains('moving')) return;
  310.         box.classList.add('moving');
  311.         box.classList.remove('pushed', 'reverse');
  312.         void box.offsetWidth;
  313.         if (isPushed) {
  314.           box.classList.add('reverse');
  315.         } else {
  316.           box.classList.add('pushed');
  317.         }
  318.         isPushed = !isPushed;
  319.       });
  320.       box.addEventListener('animationend', () => {
  321.         box.classList.remove('moving');
  322.       });
  323.     </script>
  324.   </body>
  325. </html>
复制代码

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!qidao123.com:ToB企服之家,中国第一个企服评测及软件市场,开放入驻,技术点评得现金

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表