当前位置:   article > 正文

前端 CSS 经典:mac docker 效果

前端 CSS 经典:mac docker 效果

前言:浏览器上实现 mac docker 效果,实现思路,1. 布局,方框间距用元素代替,因为有放大缩小功能,不用元素的话,不好控制。2. 定义个 css 变量 i,用来代表放大比例。3. 确定每个元素的 i 值,根据鼠标移动,动态生成一个曲线。设计曲线函数。

效果:

代码实现:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta
  7. name="viewport"
  8. content="initial-scale=1.0, user-scalable=no, width=device-width"
  9. />
  10. <title>document</title>
  11. <style>
  12. * {
  13. margin: 0;
  14. padding: 0;
  15. box-sizing: border-box;
  16. text-align: justify;
  17. }
  18. body {
  19. background: #000;
  20. color: #fff;
  21. }
  22. .docker-container {
  23. position: fixed;
  24. bottom: 10px;
  25. display: flex;
  26. justify-content: center;
  27. width: 100%;
  28. }
  29. .docker {
  30. box-sizing: content-box;
  31. height: 30px;
  32. display: flex;
  33. justify-content: center;
  34. align-items: end;
  35. padding: 5px 10px;
  36. background: rgba(255, 255, 255, 0.3);
  37. }
  38. .menu {
  39. display: flex;
  40. align-items: end;
  41. }
  42. .menu-item {
  43. border-radius: 5px;
  44. width: calc(var(--i, 1) * 30px);
  45. height: calc(var(--i, 1) * 30px);
  46. margin-bottom: calc(var(--i, 1) * 15px - 15px);
  47. background: #b4433d;
  48. border: 1px solid #ccc;
  49. }
  50. .gap {
  51. width: calc(var(--i, 1) * 10px);
  52. height: 30px;
  53. }
  54. .menu .menu-item:nth-child(3n + 1) {
  55. background: blue;
  56. }
  57. .menu .menu-item:nth-child(3n) {
  58. background: yellow;
  59. }
  60. </style>
  61. </head>
  62. <body>
  63. <div class="docker-container">
  64. <div class="docker">
  65. <div class="menu">
  66. <div class="menu-item"></div>
  67. <div class="gap"></div>
  68. <div class="menu-item"></div>
  69. <div class="gap"></div>
  70. <div class="menu-item"></div>
  71. <div class="gap"></div>
  72. <div class="menu-item"></div>
  73. <div class="gap"></div>
  74. <div class="menu-item"></div>
  75. <div class="gap"></div>
  76. <div class="menu-item"></div>
  77. <div class="gap"></div>
  78. <div class="menu-item"></div>
  79. <div class="gap"></div>
  80. <div class="menu-item"></div>
  81. <div class="gap"></div>
  82. <div class="menu-item"></div>
  83. <div class="gap"></div>
  84. <div class="menu-item"></div>
  85. <div class="gap"></div>
  86. <div class="menu-item"></div>
  87. </div>
  88. </div>
  89. </div>
  90. <script>
  91. const docker = document.querySelector(".docker");
  92. const items = document.querySelector(".menu").children;
  93. const range = 500;
  94. const maxScale = 1.8;
  95. const layout = (curve) => {
  96. for (const item of items) {
  97. const rect = item.getBoundingClientRect();
  98. const x = rect.x + rect.width / 2;
  99. const scale = curve(x);
  100. item.style.setProperty("--i", scale);
  101. }
  102. };
  103. const baseCure = (x) => {
  104. if (x < 0) return 0;
  105. if (x > 1) return 0;
  106. return Math.sin(x * Math.PI);
  107. };
  108. const createCure = (totalXDis, topX, minY, maxY) => {
  109. return function curve(x) {
  110. const beginX = topX - totalXDis / 2;
  111. const endX = topX + totalXDis / 2;
  112. if (x < beginX) return minY;
  113. if (x > endX) return minY;
  114. const yDis = maxY - minY;
  115. return baseCure((x - beginX) / totalXDis) * yDis + minY;
  116. };
  117. };
  118. docker.onmousemove = (e) => {
  119. const curve = createCure(range, e.clientX, 1, maxScale);
  120. layout(curve);
  121. };
  122. docker.onmouseleave = () => {
  123. layout(() => 1);
  124. };
  125. </script>
  126. </body>
  127. </html>

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/686866
推荐阅读
相关标签
  

闽ICP备14008679号