当前位置:Gxlcms > css > 如何使用纯CSS实现飞机舷窗风格的toggle控件

如何使用纯CSS实现飞机舷窗风格的toggle控件

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

本篇文章给大家带来的内容是关于如何使用纯CSS实现飞机舷窗风格的toggle控件,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

效果预览

2567059719-5bc44ca342df2_articlex.gif

源代码下载

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

代码解读

定义 dom,.windows 容器表示舷窗,它的子元素 .curtain 表示窗帘:

  1. <figure class="window">
  2. <div class="curtain"></div>
  3. </figure>

居中显示:

  1. body {
  2. margin: 0;
  3. height: 100vh;
  4. display: flex;
  5. align-items: center;
  6. justify-content: center;
  7. background-color: skyblue;
  8. }

设置舷窗的尺寸,因为后面还会用到字号,所以字号用变量定义:

  1. :root {
  2. --font-size: 10px;
  3. }
  4. .window {
  5. position: relative;
  6. box-sizing: border-box;
  7. width: 25em;
  8. height: 35em;
  9. font-size: var(--font-size);
  10. background-color: #d9d9d9;
  11. }

用阴影画出厚窗框:

  1. .window {
  2. border-radius: 5em;
  3. box-shadow:
  4. inset 0 0 8em rgba(0, 0, 0, 0.2),
  5. 0 0 0 0.4em #808080,
  6. 0 0 0 4em whitesmoke,
  7. 0 0 0 4.4em #808080,
  8. 0 2em 4em 4em rgba(0, 0, 0, 0.1);
  9. }

设置窗帘样式,和窗口尺寸一样,但不拉到底:

  1. .window .curtain {
  2. position: absolute;
  3. width: inherit;
  4. height: inherit;
  5. border-radius: 5em;
  6. box-shadow:
  7. 0 0 0 0.5em #808080,
  8. 0 0 3em rgba(0, 0, 0, 0.4);
  9. background-color: whitesmoke;
  10. left: 0;
  11. top: -5%;
  12. }

用伪元素在窗帘上画出指示灯,当窗帘关闭时亮红色光:

  1. .window .curtain::before {
  2. content: '';
  3. position: absolute;
  4. width: 40%;
  5. height: 0.8em;
  6. background-color: #808080;
  7. left: 30%;
  8. bottom: 1.6em;
  9. border-radius: 0.4em;
  10. }
  11. .window .curtain::after {
  12. content: '';
  13. position: absolute;
  14. width: 1.6em;
  15. height: 0.8em;
  16. background-image: radial-gradient(orange, orangered);
  17. bottom: 1.6em;
  18. border-radius: 0.4em;
  19. left: calc((100% - 1.6em) / 2);
  20. }

以上是舷窗关闭时的样子,接下来绘制舷窗打开时的效果。
先在 dom 中添加一个 checkbox,当它被 checked 时即表示舷窗被打开:

  1. <input type="checkbox" class="toggle">
  2. <figure class="window">
  3. <div class="handle"></div>
  4. </figure>

隐藏 checkbox,用 opacity(0) 可以使元素在不可见的状态下仍可交互,把它的尺寸设置得到舷窗一样大,并且图层在舷窗之上,得到的效果就是点击舷窗时实际是点击了 checkbox

  1. .toggle {
  2. position: absolute;
  3. filter: opacity(0);
  4. width: 25em;
  5. height: 35em;
  6. font-size: var(--font-size);
  7. cursor: pointer;
  8. z-index: 2;
  9. }

当舷窗打开时,.curtain 要向上移动,并且指示灯亮绿色光:

  1. .window .curtain {
  2. transition: 0.5s ease-in-out;
  3. }
  4. .toggle:checked ~ .window .curtain {
  5. top: -90%;
  6. }
  7. .toggle:checked ~ .window .curtain::after {
  8. background-image: radial-gradient(lightgreen, limegreen);
  9. }

隐藏超出窗户的部分:

  1. .window {
  2. overflow: hidden;
  3. }

接下来绘制舷窗外的风景。
在 dom 中增加表示云朵的 .clouds 元素,其中的 5 个 <span> 子元素分别表示 1 朵白云:

  1. <input type="checkbox" class="toggle">
  2. <figure class="window">
  3. <div class="curtain"></div>
  4. <div class="clouds">
  5. <span></span>
  6. <span></span>
  7. <span></span>
  8. <span></span>
  9. <span></span>
  10. </div>
  11. </figure>

用云朵容器画出窗外的蓝天:

  1. .window .clouds {
  2. position: relative;
  3. width: 20em;
  4. height: 30em;
  5. background-color: deepskyblue;
  6. box-shadow: 0 0 0 0.4em #808080;
  7. left: calc((100% - 20em) / 2);
  8. top: calc((100% - 30em) / 2);
  9. border-radius: 7em;
  10. }

每朵云由 3 部分组成,先画面积最大的部分:

  1. .clouds span {
  2. position: absolute;
  3. width: 10em;
  4. height: 4em;
  5. background-color: white;
  6. top: 20%;
  7. border-radius: 4em;
  8. }

再用伪元素画 2 个突起的圆弧:

  1. .clouds span::before,
  2. .clouds span::after {
  3. content: '';
  4. position: absolute;
  5. width: 4em;
  6. height: 4em;
  7. background-color: white;
  8. border-radius: 50%;
  9. }
  10. .clouds span::before {
  11. top: -2em;
  12. left: 2em;
  13. }
  14. .clouds span::after {
  15. top: -1em;
  16. right: 1em;
  17. }

增加云朵飘动的动画效果:

  1. .clouds span {
  2. animation: move 4s linear infinite;
  3. }
  4. @keyframes move {
  5. from {
  6. left: -150%;
  7. }
  8. to {
  9. left: 150%;
  10. }
  11. }

使每朵云的大小、位置有一些变化:

  1. .clouds span:nth-child(2) {
  2. top: 40%;
  3. animation-delay: -1s;
  4. }
  5. .clouds span:nth-child(3) {
  6. top: 60%;
  7. animation-delay: -0.5s;
  8. }
  9. .clouds span:nth-child(4) {
  10. top: 20%;
  11. transform: scale(2);
  12. animation-delay: -1.5s;
  13. }
  14. .clouds span:nth-child(5) {
  15. top: 70%;
  16. transform: scale(1.5);
  17. animation-delay: -3s;
  18. }

最后,隐藏容器外的内容:

  1. .window .clouds {
  2. overflow: hidden;
  3. }

大功告成!

以上就是如何使用纯CSS实现飞机舷窗风格的toggle控件的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行