纯CSS如何实现热气球的效果

发布时间:2020-07-21 11:21:19 作者:Leah
来源:亿速云 阅读:232

这期内容当中小编将会给大家带来有关纯CSS如何实现热气球的效果,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

效果预览

纯CSS如何实现热气球的效果

代码解读

定义 dom,容器中有 2 个子元素,.envelope 代表伞盖,.basket 代表吊篮:

<figure class="balloon">
    <div class="envelope">
        <span></span>
        <span></span>
    </div>
    <div class="basket">
        <span></span>
        <span></span>
        <span></span>
        <span></span>
    </div>
</figure>

居中显示:

body {
    margin: 0;
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(deepskyblue, skyblue, lightblue 20%);
}

定义容器的尺寸,子元素 .envelope.basket 纵向居中布局:

.balloon {
    width: 12em;
    height: 19em;
    font-size: 16px;
    display: flex;
    flex-direction: column;
    align-items: center;
}

先画伞盖。
定义伞盖的尺寸:

.envelope {
    position: relative;
    width: inherit;
    height: 16em;
}

伞盖的形状是上端为球形,下端为圆锥形,在二维平面中,圆锥在平面的投影为等腰三角形,所以我们先在上部画一个圆,再在下部画一个三角形。
先画上部的圆:

.envelope span {
    position: absolute;
    width: inherit;
    height: 12em;
    border-radius: 50%;
    color: orange;
    background-color: currentColor;
}

再用伪元素画出下部的等腰三角形:

.envelope span::before {
    content: '';
    position: absolute;
    width: 0;
    height: 0;
    border-width: 10em 5.5em 0 5.5em;
    border-style: solid;
    border-color: currentColor transparent transparent transparent;
    left: calc(50% - 5.5em);
    top: 8.45em;
}

.envelope 下共有 2 个 <span> 元素,让第 2 个 <span> 变形、变色,使伞盖形成竖条纹的花纹:

.envelope span:nth-child(2) {
    transform: scaleX(0.4);
    filter: brightness(0.85) contrast(1.4);
}

隐藏 .envelope 容器外的部分,削掉三角形最下面的尖角:

.envelope {   
    overflow: hidden;
}

至此,伞盖完成,接下来画吊篮。
定义吊篮的尺寸:

.basket {
    position: relative;
    width: 2em;
    height: 3em;
}

::before 伪元素画出篮子:

.basket::before {
    content: '';
    position: absolute;
    width: inherit;
    height: 1.6em;
    background-color: peru;
    bottom: 0;
    border-radius: 0 0 0.5em 0.5em;
}

::after 伪元素画出篮子的顶边:

.basket::after {
    content: '';
    position: absolute;
    width: 105%;
    height: 0.3em;
    background-color: saddlebrown;
    left: calc((100% - 105%) / 2);
    top: 1.3em;
    border-radius: 0.3em;
}

.basket 下共有 4 个 <span> 元素,代表 4 根缆绳,设置它们的样式为竖细线:

.basket span {
    position: absolute;
    width: 0.1em;
    height: 1.5em;
    background-color: burlywood;
}

定位缆绳,并倾斜不同的角度:

.basket span {
    left: calc((var(--n) - 1) * 0.6em);
    transform-origin: bottom;
    transform: rotate(calc(var(--r) * 7deg));
}

.basket span:nth-child(1) { --n: 1; --r: -2; }
.basket span:nth-child(2) { --n: 2; --r: -1; }
.basket span:nth-child(3) { --n: 3; --r: 1; }
.basket span:nth-child(4) { --n: 4; --r: 2; }

最后,增加热气球微微浮动的动画效果:

.balloon {
    animation: drift 2s infinite alternate;
}

@keyframes drift {
    to {
        transform: translateY(-5%);
    }
}

大功告成!

上述就是小编为大家分享的纯CSS如何实现热气球的效果了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. 用纯css实现折叠效果的方法
  2. 怎么使用纯CSS实现蚊香燃烧的效果

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

css3 如何实现

上一篇:详解Jquery高级应用Deferred对象

下一篇:OkHttpClient和Jsoup进行网页爬取

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》