zoukankan      html  css  js  c++  java
  • 如何用纯 CSS 创作一支诱人的冰棍

    效果预览

    在线演示

    按下右侧的“点击预览”按钮可以在当前页面预览,点击链接可以全屏预览。

    https://codepen.io/comehope/pen/vrxzMw

    可交互视频教程

    此视频是可以交互的,你可以随时暂停视频,编辑视频中的代码。

    请用 chrome, safari, edge 打开观看。

    https://scrimba.com/p/pEgDAM/cnpZEAZ

    源代码下载

    本地下载

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

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

    代码解读

    定义 dom,容器中包含 2 个元素:

    <div class="ice-lolly">
        <div class="flavors"></div>
        <div class="stick"></div>
    </div>
    

    居中显示:

    body {
        margin: 0;
        height: 100vh;
        display: flex;
        align-items: center;
        justify-content: center;
        background-color: darkslategray;
    }
    

    绘制出冰棍的外形:

    .flavors {
         19em;
        height: 26em;
        font-size: 10px;
        border-radius: 8em 8em 1em 1em;
    }
    

    给冰棍上色:

    .flavors {
        position: relative;
        overflow: hidden;
    }
    
    .flavors::before {
        content: '';
        position: absolute;
         140%;
        height: 120%;
        background: linear-gradient(
            hotpink 0%,
            hotpink 25%,
            deepskyblue 25%,
            deepskyblue 50%,
            gold 50%,
            gold 75%,
            lightgreen 75%,
            lightgreen 100%);
        z-index: -1;
        left: -20%;
        transform: rotate(-25deg);
    }
    

    来一点光照效果:

    .flavors::after {
        content: '';
        position: absolute;
         2em;
        height: 17em;
        background-color: rgba(255, 255, 255, 0.5);
        left: 2em;
        bottom: 2em;
        border-radius: 1em;
    }
    

    画出冰棍筷子:

    .stick {
        position: relative;
         6em;
        height: 8em;
        background-color: sandybrown;
        left: calc(50% - 6em / 2);
        border-radius: 0 0 3em 3em;
    }
    

    给冰棍筷子加一点阴影,增加立体感:

    .stick::after {
        content: '';
        position: absolute;
         inherit;
        height: 2.5em;
        background-color: sienna;
    }
    

    让冰棍的色彩滚动起来:

    .flavors::before {
        animation: moving 100s linear infinite;
    }
    
    @keyframes moving {
        to {
            background-position: 0 1000vh;
        }
    }
    

    最后,增加交互效果,当鼠标悬停时才播放动画:

    .flavors::before {
        animation-play-state: paused;
    }
    
    .ice-lolly:hover .flavors::before {
        animation-play-state: running;
    }
    

    大功告成!

    原文地址:https://segmentfault.com/a/1190000015257561
  • 相关阅读:
    (转帖) oracle是否归档模式及修改模式
    (转帖) Oracle实例恢复(Oracle instance recovery)
    实习第一周总结
    UML类图几种关系的总结
    表格排序
    利用js查找页面中的内链,外链
    事件机制(事件冒泡与事件捕获)
    谈 CSS 模块化
    初学后台框架总结篇二——快速了解CI框架
    初学后台框架总结篇一——学习过程
  • 原文地址:https://www.cnblogs.com/lalalagq/p/10007810.html
Copyright © 2011-2022 走看看