HTML+CSS制作一个动画(二)

简介: HTML+CSS制作一个动画

HTML+CSS制作一个动画(一)https://developer.aliyun.com/article/1382469


CSS部分结构如下:

:root {
    --background-color: #f5f5f5;
    --border-color: #7591AD;
    --text-color: #34495e;
    --color1: #EC3E27;
    --color2: #fd79a8;
    --color3: #6aa6d4;
    --color4: #00b894;
    --color5: #fdcb6e;
    --color6: #e056fd;
    --color7: #04a704;
    --color8: #BDC581;
}
*,
::after,
::before {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
html {
    font-size: 14px;
}
body {
    width: 100vw;
    height: 100vh;
    background-color: var(--background-color);
    display: flex;
    justify-content: center;
    align-items: center;
    font-family: 'Montserrat', sans-serif, Arial, 'Microsoft Yahei';
}
.channel {
    position: absolute;
    width: 80%;
    text-align: center;
    top: 10vh;
    left: 50%;
    transform: translateX(-50%);
    font-size: 5vmin;
    font-weight: bold;
    color: #35353588;
    z-index: 10000;
}
.container {
    position: absolute;
    width: 100vw;
    height: 100vh;
    /* background-color: #00b894; */
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
}
/* **********
 * 速度切换开关
 * 不用checkbox实现,我们用js控制样式来实现
 * **********/
#switch {
    position: relative;
    width: 60px;
    height: 30px;
    border: 2px solid #333;
    border-radius: 30px;
    cursor: pointer;
}
#switch::before,
#switch::after {
    position: absolute;
    line-height: 26px;
    text-transform: uppercase;
    font-weight: 800;
    padding: 0 20px;
    /* background-color: #0984e3; */
    /* 取消前后状态名称的鼠标效果以及点击事件响应 */
    cursor: initial;
    pointer-events: none;
}
#switch::before {
    content: 'fast';
    right: 100%;
    color: var(--color8);
}
#switch::after {
    content: 'slow';
    left: 100%;
    color: var(--color3);
}
/* 默认情况 */
#switch .circle {
    position: absolute;
    top: 3px;
    left: 3px;
    width: 20px;
    height: 20px;
    border: 2px solid #333;
    border-radius: 50%;
    box-shadow: inset 4px -2px 0 0 var(--color8);
    transition: all .3s cubic-bezier(.86, 1.51, .74, 1);
}
/* 点击选中的情况 */
#switch.switched .circle {
    left: calc(100% - 20px - 3px);
    box-shadow: inset 4px -2px 0 0 var(--color3);
}
/* **********
 * 这里是主画布外框
 * **********/
.main {
    position: relative;
    width: 400px;
    height: 300px;
    border: 2px solid #333;
    margin-top: 20px;
    overflow: hidden;
}
/* **********
 * 这里是太阳
 * **********/
.sun {
    position: absolute;
    width: 20px;
    height: 20px;
    border: 2px solid #333;
    border-radius: 50%;
    left: 270px;
    top: 50px;
    display: flex;
    justify-content: center;
    align-items: center;
    animation: sun_rotate 10s linear infinite;
}
@keyframes sun_rotate {
    0% {
        transform: rotate(0deg);
    }
    100% {
        transform: rotate(-360deg);
    }
}
.sun span {
    position: absolute;
    display: block;
    height: 2px;
    width: 16px;
    background-color: #333;
    box-shadow: 4px 4px 0 0 var(--color5);
    transform: rotate(calc(var(--i) * 45deg)) translateX(-20px);
    animation: sun_ray 1s linear infinite;
    animation-delay: calc(var(--i) * -0.5s);
}
@keyframes sun_ray {
    0% {
        transform: rotate(calc(var(--i) * 45deg)) translateX(-20px) scaleX(1)
    }
    50% {
        transform: rotate(calc(var(--i) * 45deg)) translateX(-20px) scaleX(0.6)
    }
    100% {
        transform: rotate(calc(var(--i) * 45deg)) translateX(-20px) scaleX(1)
    }
}
/* **********
 * 这里是群山
 * **********/
.mountain {
    position: absolute;
    border: 2px solid #333;
    transform: rotate(45deg);
}
.mountain.a {
    width: 14px;
    height: 14px;
    left: 80px;
    bottom: 35px;
}
.mountain.b {
    width: 18px;
    height: 18px;
    left: 130px;
    bottom: 45px;
}
.mountain.c {
    width: 20px;
    height: 20px;
    left: 190px;
    bottom: 60px;
}
.mountain.d {
    width: 2px;
    height: 2px;
    left: 240px;
    bottom: 62px;
    border-bottom: none;
}
.mountain.e {
    width: 14px;
    height: 14px;
    left: 285px;
    bottom: 41px;
}
.mountain.a::after,
.mountain.b::after,
.mountain.c::after,
.mountain.d::after,
.mountain.e::after,
.mountain.a::before,
.mountain.b::before,
.mountain.c::before,
.mountain.d::before,
.mountain.e::before {
    position: absolute;
    content: '';
    background-color: #333;
}
.mountain.a::after,
.mountain.b::after,
.mountain.c::after,
.mountain.d::after,
.mountain.e::after {
    left: -2px;
    top: 100%;
    width: 2px;
}
.mountain.a::after {
    height: 70px;
}
.mountain.b::after {
    height: 50px;
}
.mountain.c::after {
    height: 70px;
}
.mountain.d::after {
    height: 14px;
}
.mountain.e::after {
    height: 18px;
}
.mountain.a::before,
.mountain.b::before,
.mountain.c::before,
.mountain.d::before,
.mountain.e::before {
    top: -2px;
    left: 100%;
    height: 2px;
}
.mountain.a::before {
    width: 14px;
    box-shadow: -5px -5px 0 0 var(--color4);
}
.mountain.b::before {
    width: 14px;
    box-shadow: -5px -5px 0 0 var(--color4);
}
.mountain.c::before {
    width: 36px;
    box-shadow: -14px -5px 0 0 var(--color4);
}
.mountain.d::before {
    width: 55px;
    box-shadow: 5px 5px 0 0 var(--color4);
}
.mountain.e::before {
    width: 70px;
    box-shadow: 5px 5px 0 0 var(--color4);
}
/* **********
 * 这里是云和风
 * **********/
/* .main {
    overflow: visible;
} */
.cloud_wind_wrap_fast {
    /* (画布大小-去两个边框)*3 */
    width: 1188px;
    height: 100%;
    /* background-color: #af401455; */
    animation: fly 3s linear infinite;
    animation-play-state: running;
}
.slow .cloud_wind_wrap_fast {
    animation-play-state: paused;
}
.cloud_wind_wrap_slow {
    width: 1188px;
    height: 100%;
    /* background-color: #56fd7255; */
    display: flex;
    justify-content: flex-start;
    align-items: center;
    animation: fly 6s linear infinite;
    animation-play-state: paused;
}
.slow .cloud_wind_wrap_slow {
    animation-play-state: running;
}
.cloud_wind_group {
    position: relative;
    width: 100%;
    height: 100%;
    /* background-color: #00b894; */
    /* animation: fly 12s linear infinite; */
    /* animation-delay: calc(var(--i) * -6s); */
}
@keyframes fly {
    0% {
        transform: translateX(0%);
    }
    100% {
        transform: translateX(-33.33333333333333%);
    }
}
/* **********
 * 今天只做云,明天风会来
 * **********/
.cloud {
    position: absolute;
    width: 60px;
    height: 45px;
}
.cloud.a {
    left: 60px;
    top: 20px;
}
.cloud.b {
    left: 160px;
    top: 80px;
    transform: rotateY(180deg);
}
.cloud.c {
    left: 280px;
    top: 50px;
}
.cloud span {
    position: absolute;
    display: block;
    background-color: #fff;
    border: 2px solid #333;
    border-radius: 50%;
}
.cloud span:nth-child(1) {
    width: 20px;
    height: 20px;
    left: 0;
    bottom: 0;
    border-right-color: transparent;
    border-bottom-color: transparent;
    transform: rotate(-45deg);
}
.cloud span:nth-child(2) {
    width: 20px;
    height: 20px;
    left: 10px;
    bottom: 10px;
    border-top-color: transparent;
    border-right-color: transparent;
    border-bottom-color: transparent;
    transform: rotate(45deg);
}
.cloud span:nth-child(3) {
    width: 30px;
    height: 30px;
    left: 15px;
    bottom: 12px;
    border-right-color: transparent;
    border-bottom-color: transparent;
    transform: rotate(50deg);
    box-shadow: 0 -3px 0 0 var(--color3);
}
.cloud span:nth-child(4) {
    width: 27px;
    height: 27px;
    left: 31px;
    bottom: 0px;
    border-left-color: transparent;
    transform: rotate(0deg);
    box-shadow: 2px -1px 0 0 var(--color3);
}
.cloud span:nth-child(5) {
    width: 35px;
    height: 15px;
    left: 10px;
    bottom: 0;
    border-radius: 0;
    border-width: 0 0 2px 0;
}
/* 中间云的阴影 */
.cloud.b span:nth-child(1) {
    box-shadow: 0px -3px 0 0 var(--color3);
}
.cloud.b span:nth-child(2) {
    box-shadow: -2px -1px 0 0 var(--color3);
}
.cloud.b span:nth-child(3) {
    box-shadow: -3px -1px 0 0 var(--color3);
}
.cloud.b span:nth-child(4) {
    box-shadow: none;
}
/* **********
 * 风来了
 * **********/
/* 大风 */
.wind {
    position: absolute;
    width: 10px;
    height: 10px;
    /* background-color: #EC3E27; */
}
.wind.a {
    top: 100px;
    left: 40px;
}
.wind.b {
    top: 140px;
    left: 260px;
}
/* 大风纹理 */
.wind span,
.wind span::before,
.wind span::after {
    position: absolute;
    background-color: #555;
    height: 3px;
}
.wind span::before,
.wind span::after {
    content: '';
}
.wind span,
.wind span::after {
    box-shadow: 4px 4px 0 -1px var(--color3);
}
/* 大风纹理1 */
.wind span:nth-child(1) {
    top: 0;
    left: 0;
    width: 40px;
}
.wind span:nth-child(1)::before {
    width: 10px;
    height: 10px;
    left: -4px;
    bottom: 1px;
    background-color: transparent;
    border: 3px solid #555;
    border-radius: 50%;
    border-color: transparent transparent #555 #555;
    transform: rotate(45deg);
}
.wind span:nth-child(1)::after {
    width: 10px;
    top: 0;
    left: 110%;
}
/* 大风纹理2 */
.wind span:nth-child(2) {
    top: 8px;
    left: -20px;
    width: 40px;
}
.wind span:nth-child(2)::before {
    width: 10px;
    height: 10px;
    left: -4px;
    bottom: 1px;
    background-color: transparent;
    border: 3px solid #555;
    border-radius: 50%;
    border-color: transparent transparent #555 #555;
    transform: rotate(45deg);
}
.wind span:nth-child(2)::after {
    width: 50px;
    top: 0;
    left: 110%;
}
/* 大风纹理3 */
.wind span:nth-child(3) {
    top: 15px;
    left: 50px;
    width: 10px;
}
.wind span:nth-child(3)::before {
    width: 40px;
    top: 0;
    right: 140%;
}
.wind span:nth-child(3)::after {
    width: 40px;
    top: 0;
    left: 140%;
}
/* 大风纹理4 */
.wind span:nth-child(4) {
    top: 20px;
    left: 30px;
    width: 40px;
}
.wind span:nth-child(4)::before {
    width: 10px;
    top: 0;
    right: 120%;
}
.wind span:nth-child(4)::after {
    width: 10px;
    top: 0;
    left: 120%;
}
/* 小风 */
.wind_small {
    position: absolute;
    width: 100%;
    height: 100%;
    /* background-color: #00b894; */
}
/* 小风纹理 */
.wind_small span,
.wind_small span::after {
    position: absolute;
    height: 3px;
    background-color: #555;
    box-shadow: 4px 4px 0 -1px var(--color3);
}
.wind_small span::after {
    content: '';
    width: 50%;
    top: 0;
    left: 120%;
}
.wind_small span:nth-child(1) {
    top: 12px;
    left: 5px;
    width: 50px;
}
.wind_small span:nth-child(2) {
    top: 32px;
    left: 120px;
    width: 20px;
}
.wind_small span:nth-child(3) {
    top: 152px;
    left: 120px;
    width: 20px;
}
.wind_small span:nth-child(3)::after {
    display: none;
}
.wind_small span:nth-child(4) {
    top: 172px;
    left: 60px;
    width: 20px;
}
.wind_small span:nth-child(5) {
    top: 60px;
    left: 140px;
    width: 10px;
}
.wind_small span:nth-child(5)::after {
    display: none;
}
.wind_small span:nth-child(6) {
    top: 20px;
    left: 300px;
    width: 30px;
}
.wind_small span:nth-child(7) {
    top: 115px;
    left: 330px;
    width: 30px;
}
.wind_small span:nth-child(8) {
    top: 170px;
    left: 200px;
    width: 10px;
}
.wind_small span:nth-child(8)::after {
    display: none;
}
/* **********
 * 这是四棵树
 * **********/
.tree_wrap,
.tree {
    position: absolute;
    width: 100%;
    height: 60px;
    left: 0;
    bottom: 0;
    /* background-color: #00b89466; */
    transform-origin: bottom;
    transition: all 0.2s linear;
}
.tree {
    /* background-color: #e056fd55; */
    animation: tree_move 1s linear infinite;
    transform-origin: bottom;
}
.slow .tree_wrap {
    transform: skewX(-10deg);
}
@keyframes tree_move {
    0%,
    100% {
        transform: skewX(16deg);
    }
    20% {
        transform: skewX(18deg);
    }
    40% {
        transform: skewX(20deg);
    }
    60% {
        transform: skewX(17deg);
    }
    80% {
        transform: skewX(18deg);
    }
}
.tree span {
    position: absolute;
    display: block;
    width: 2px;
    height: 10px;
    bottom: 0;
    background-color: #333;
    display: flex;
    justify-content: center;
    align-items: center;
}
.tree span:nth-child(1) {
    left: 70px;
}
.tree span:nth-child(2) {
    left: 150px;
}
.tree span:nth-child(3) {
    left: 180px;
}
.tree span:nth-child(4) {
    left: 330px;
}
.tree span::before,
.tree span::after {
    position: absolute;
    content: '';
    border: 2px solid #333;
    width: 12px;
    height: 26px;
    bottom: 100%;
    border-radius: 50%;
    background-color: #fff;
    box-shadow: inset -3px 4px 2px 0 var(--color7);
}
.tree span::after {
    border-color: transparent var(--color7) transparent transparent;
    background-color: transparent;
    transform: rotate(-15deg) translate(5px, -4px);
    box-shadow: none;
}
/* **********
 * 这是三个风车
 * **********/
.windmill_wrap {
    position: absolute;
    width: 100%;
    height: 120px;
    left: 0;
    bottom: 0;
    /* background-color: #00b89466; */
}
.windmill {
    position: absolute;
    /* width: 114px; */
    height: 100px;
    left: 220px;
    bottom: 0;
    /* background-color: #BDC581; */
    display: flex;
    justify-content: center;
    align-items: center;
}
.windmill.left {
    left: 100px;
    transform: scale(0.75);
    transform-origin: bottom;
}
.windmill.right {
    left: 300px;
    transform: scale(0.8);
    transform-origin: bottom;
}
/* 风车柱子 */
.windmill .pole {
    position: absolute;
    width: 14px;
    height: 120px;
    bottom: 0px;
    /* background-color: #535c0f; */
    perspective: 70px;
}
.windmill .pole::after {
    position: absolute;
    content: '';
    width: 100%;
    height: 100%;
    background-color: #fff;
    border: 3px solid #333;
    border-width: 0 3px;
    transform-origin: bottom;
    transform: rotateX(15deg);
}
/* 风车头部 */
.windmill .head {
    position: absolute;
    width: 10px;
    height: 10px;
    top: 0;
    /* background-color: #EC3E27; */
    transform: translateY(-50%);
    display: flex;
    justify-content: center;
    align-items: center;
    animation: windmill_fan_rotate 4s linear infinite;
    animation-play-state: paused;
}
.slow .windmill .head {
    animation-play-state: running;
}
/* 扇叶组合 */
.windmill .head .fan {
    position: absolute;
    width: 10px;
    height: 10px;
    /* background-color: #27ec72; */
    display: flex;
    justify-content: center;
    align-items: center;
    animation: windmill_fan_rotate 2s linear infinite;
    animation-play-state: running;
}
.slow .windmill .head .fan {
    animation-play-state: paused;
}
/* 扇叶组合中心圆 */
.windmill .head .fan::after {
    position: absolute;
    content: '';
    width: 15px;
    height: 15px;
    background-color: #fff;
    border: 3px solid #333;
    border-radius: 50%;
}
/* 扇叶 */
.windmill .head .fan span {
    position: absolute;
    width: 70px;
    height: 15px;
    background-color: #fff;
    border: 3px solid #333;
    border-radius: 100%;
    transform: rotate(calc(var(--i) * 120deg)) translateX(30px);
}
/* 旋转动画 */
@keyframes windmill_fan_rotate {
    0% {
        transform: rotate(0deg);
    }
    100% {
        transform: rotate(-360deg);
    }
}
相关文章
|
2天前
|
前端开发 JavaScript 开发者
CSS进阶-过渡与动画的事件监听
【6月更文挑战第16天】**CSS过渡和动画事件增强交互性,但监听与控制需谨慎。了解`transitionend`用于CSS过渡结束时的响应,避免过度使用JavaScript检测变化。示例代码展示如何绑定`transitionend`事件并在结束后执行操作。对于CSS动画,理解`animationstart`, `animationiteration`, `animationend`事件的生命周期至关重要,确保在动画结束后进行适当的清理。通过这些技巧,优化用户体验并提高代码效率。**
|
2天前
|
前端开发 开发者 异构计算
CSS进阶-CSS动画关键帧
【6月更文挑战第15天】CSS的`@keyframes`创建细腻动画,定义样式变化阶段以增强网页互动性。通过`animation`属性应用动画,如`fadeIn`示例。常见问题包括动画结束状态、卡顿和浏览器兼容性,解决办法涉及优化关键帧、使用硬件加速和添加前缀。进阶技巧包括多步骤动画和控制播放状态。例如,背景色渐变动画展示了颜色随时间变化的效果。学习和实践关键帧动画,提升Web开发技能。
|
3天前
|
XML 前端开发 JavaScript
前端简介(HTML+CSS+JS)
前端简介(HTML+CSS+JS)
|
4天前
|
缓存 前端开发 JavaScript
从HTML、CSS以及其他方面介绍一些移动端网页性能优化的策略
【6月更文挑战第14天】本文探讨了移动端网页性能优化的重要性,并提出了HTML、CSS及其他方面的优化策略。HTML方面,建议精简结构、使用语义化标签、异步加载脚本和压缩文件。CSS优化包括简化样式、利用CSS3动画、媒体查询及压缩文件。其他策略涉及图片和字体优化、缓存利用、压缩传输数据、减少HTTP请求及监听网络状态。通过这些方法,可提升网页性能,改善用户体验。
10 1
|
4天前
|
缓存 移动开发 前端开发
在PWA的开发中,HTML与CSS作为前端技术的基础,发挥着至关重要的作用
【6月更文挑战第14天】PWA(渐进式网页应用)借助HTML和CSS,提供接近原生应用的体验。HTML构建页面结构和内容,响应式设计适应各种设备,语义化标签提升可访问性,Manifest文件配置应用元数据,离线页面保证无网时体验。CSS则用于定制主题样式,创建动画效果,实现响应式布局,并管理字体和图标。两者协同工作,确保PWA在不同环境下的优秀性能和用户体验。随着前端技术进步,HTML与CSS在PWA中的应用将更加深入。
17 2
|
6天前
|
前端开发 数据安全/隐私保护
紫色渐变登陆布局html+css代码
这是一段关于网页设计的代码示例,使用纯CSS实现了登录界面的样式。HTML部分包括一个简单的登录表单,包含用户名、密码输入框和登录、注册按钮。CSS部分则定义了各种元素的样式,如背景色、边框、字体颜色等,并使用渐变效果和过渡动画来增强视觉效果。整个设计采用了响应式布局,适应不同设备的屏幕宽度。
15 0
|
6天前
|
前端开发 数据安全/隐私保护 容器
简约渐变色登陆布局html+css代码
这是一段包含HTML和CSS代码的摘要。HTML部分定义了一个基本的网页结构,包括`<html>`、`<head>`、`<body>`标签,以及一个简单的登录界面,由一个容器`.container`包含一个登录框`.login-wrapper`,登录框内有输入框和登录按钮。CSS部分设置了全局样式,如字体、边距,并为HTML元素添加了样式,如背景渐变色、文字对齐、输入框和按钮的样式等。整个代码展示了创建一个具有响应式布局和特定视觉效果的简洁登录页面。
14 0
|
6天前
|
前端开发 安全 数据安全/隐私保护
Web实战丨基于django+html+css+js的在线博客网站
Web实战丨基于django+html+css+js的在线博客网站
13 2
|
6天前
|
数据库 数据安全/隐私保护 Python
Web实战丨基于django+html+css+js的电子商务网站
Web实战丨基于django+html+css+js的电子商务网站
19 3