当前位置:Gxlcms > css > 如何使用纯CSS实现蝴蝶标本的展示框效果

如何使用纯CSS实现蝴蝶标本的展示框效果

时间:2021-07-01 10:21:17 帮助过:19人阅读

本篇文章给大家带来的内容是关于如何使用纯CSS实现蝴蝶标本的展示框效果 ,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

效果预览

1396116987-5b1b9a1b45db9_articlex.png

源代码下载

每日前端实战系列的全部源代码请从 github 下载:

https://github.com/comehope/front-end-daily-challenges

代码解读

定义 dom,容器表示整只蝴蝶,因为蝴蝶是对称的,所以分为左右两边,每边有 3 个子元素:

  1. <div class="butterfly">
  2. <div class="left">
  3. <span></span>
  4. <span></span>
  5. <span></span>
  6. </div>
  7. <div class="right">
  8. <span></span>
  9. <span></span>
  10. <span></span>
  11. </div>
  12. </div>

居中显示:

  1. body {
  2. margin: 0;
  3. height: 100vh;
  4. display: flex;
  5. align-items: center;
  6. justify-content: center;
  7. background: linear-gradient(gray, lightyellow, gray);
  8. }

定义蝴蝶的尺寸:

  1. .butterfly {
  2. position: relative;
  3. width: 10em;
  4. height: 10em;
  5. }

先画左半边:

  1. .butterfly .left {
  2. position: absolute;
  3. width: inherit;
  4. height: inherit;
  5. }

用第 1 个子元素画出翅膀的上半部分:

  1. .butterfly span {
  2. position: absolute;
  3. border-radius: 50%;
  4. }
  5. .butterfly span:nth-child(1) {
  6. width: 5em;
  7. height: 7em;
  8. background-color: gold;
  9. }

用第 2 个子元素画出翅膀的下半部分:

  1. .butterfly span:nth-child(2) {
  2. width: 5.5em;
  3. height: 3.5em;
  4. background-color: orangered;
  5. top: 5em;
  6. left: -2.5em;
  7. filter: opacity(0.6);
  8. }

用第 3 个子元素画出触角:

  1. .butterfly span:nth-child(3) {
  2. width: 6em;
  3. height: 6em;
  4. border-right: 0.3em solid orangered;
  5. top: -0.5em;
  6. }

把左半边复制一份到右半边:

  1. .butterfly .right {
  2. position: absolute;
  3. width: inherit;
  4. height: inherit;
  5. }
  6. .butterfly .right {
  7. transform: rotateY(180deg) rotate(-90deg);
  8. top: 0.4em;
  9. left: 0.4em;
  10. }

把标本装到展示框里:

  1. .butterfly::before {
  2. content: '';
  3. position: absolute;
  4. box-sizing: border-box;
  5. top: -2.5em;
  6. left: -8em;
  7. width: 24em;
  8. height: 18em;
  9. background-color: black;
  10. border: 0.2em inset silver;
  11. }
  12. .butterfly::after {
  13. content: '';
  14. position: absolute;
  15. box-sizing: border-box;
  16. width: 40em;
  17. height: 30em;
  18. background-color: lightyellow;
  19. top: -9em;
  20. left: -16em;
  21. border: 2em solid burlywood;
  22. border-radius: 3em;
  23. box-shadow:
  24. 0 0.3em 2em 0.4em rgba(0, 0, 0, 0.3),
  25. inset 0.4em 0.4em 0.1em 0.5em rgba(0, 0, 0, .4);
  26. z-index: -1;
  27. }

最后,调整一下因图案倾斜引起的位移:

  1. .butterfly {
  2. transform: translateX(1em);
  3. }

大功告成!

相关推荐:

如何使用纯CSS实现Windows启动界面的动画效果

如何使用纯CSS实现单元素麦当劳的Logo(附源码)

以上就是如何使用纯CSS实现蝴蝶标本的展示框效果的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行