纯css实现电池波浪充电特效

1 篇文章 0 订阅
1 篇文章 0 订阅

实现原理:

主要利用css的transform变换与animation属性,通过使用translate(x,y) 实现元素位移、rotate(deg) 实现元素旋转、@keyframes实现关键帧动画。css知识点附于本文底部。

展现效果

在这里插入图片描述

HTML部分:

<div class="container">
    <div class="header"></div>
    <div class="battery">
    </div>
    <div class="battery-copy">
        <div class="g-wave"></div>
        <div class="g-wave"></div>
        <div class="g-wave"></div>
    </div>
</div>

CSS部分:

html
,body{
    width:100%;
    height:100%;
    display:flex;
    background:#e4e4e4;
}
.container {
    position: relative;
    width: 140px;
    margin: auto;
}
.battery-copy {
    position: absolute;
    top: 0;
    left: 0;
    height: 220px;
    width: 140px;
    border-radius: 15px 15px 5px 5px;
    overflow: hidden;
}
.battery{
    width:140px;
    height:220px;
    box-sizing:border-box;
    border-radius:15px 15px 5px 5px;
    position:relative;
    background:#fff;
    z-index:1;
    filter:drop-shadow(0 1px 3px rgba(0,0,0,0.22));
    &::before {
        content: "";
        position: absolute;
        width: 26px;
        height: 10px;
        left: 50%;
        top: 0;
        transform: translate(-50%, -10px);
        border-radius: 5px 5px 0 0;
        background: rgba(240, 240, 240, .88);
    }
        &::after {
        content:"";
        position:absolute;
        bottom:0;
        left:0;
        top:80%;
        right:0;
        background:linear-gradient(to bottom , #7abcff 0%, #00BCD4 50%, #2196F3 100%);
        border-radius:0 0 5px 5px;
        box-shadow: 0 14px 28px rgba(33, 150, 243, 0), 0 10px 10px rgba(9, 188, 215, 0.08);
        filter:hue-rotate(120deg);
        animation: charging 6s linear infinite;
    }
}
@keyframes charging{
    50%{
         box-shadow: 0 14px 28px rgba(0, 150, 136, 0.83), 0px 4px 10px rgba(9, 188, 215, 0.4);
    }
    95%{
        top: 5%;
        filter: hue-rotate(0deg);
        border-radius: 0 0 5px 5px;
        box-shadow: 0 14px 28px rgba(4, 188, 213, .2), 0 10px 10px rgba(9, 188, 215, 0.08);
    }
    100%{
        top: 0%;
        filter: hue-rotate(0deg);
        border-radius: 15px 15px 5px 5px;
        box-shadow: 0 14px 28px rgba(4, 188, 213, .2), 0 10px 10px rgba(9, 188, 215, 0.08);
    }
}
.g-wave {
    position: absolute;
    width: 300px;
    height: 300px;
    background: rgba(255, 255, 255, .8);
    border-radius: 45% 47% 44% 42%;
    bottom: 25px;
    left: 50%;
    transform: translate(-50%, 0);
    z-index: 1;
    animation: move 10s linear infinite;
}
.g-wave:nth-child(2) {
    border-radius: 38% 46% 43% 47%;
    transform: translate(-50%, 0) rotate(-135deg);
}

.g-wave:nth-child(3) {
    border-radius: 42% 46% 37% 40%;
    transform: translate(-50%, 0) rotate(135deg);
}
@keyframes move {
    100% {
        transform: translate(-50%, -160px) rotate(720deg);
    }
}

CSS3 transform变换
translate(x,y) 设置盒子位移
scale(x,y) 设置盒子缩放
rotate(deg) 设置盒子旋转
skew(x-angle,y-angle) 设置盒子斜切
perspective 设置透视距离
transform-style flat | preserve-3d 设置盒子是否按3d空间显示
translateX、translateY、translateZ 设置三维移动
rotateX、rotateY、rotateZ 设置三维旋转
scaleX、scaleY、sclaeZ 设置三维缩放
tranform-origin 设置变形的中心点
backface-visibility 设置盒子背面是否可见
CSS3 animation动画
@keyframes 定义关键帧动画
animation-name 动画名称
animation-duration 动画时间
animation-timing-function 动画曲线
(1) linear 匀速
(2) ease 开始和结束慢速
(3) ease-in 开始是慢速
(4) ease-out 结束时慢速
(5) ease-in-out 开始和结束时慢速
(6) steps 动画步数
animation-delay 动画延迟
animation-iteration-count 动画播放次数 n|infinite
animation-direction
(1) normal 默认动画结束不返回
(2) Alternate 动画结束后返回
animation-play-state 动画状态
(1) paused 停止
(2) running 运动
animation-fill-mode 动画前后的状态
(1) none 不改变默认行为
(2) forwards 当动画完成后,保持最后一个属性值(在最后一个关键帧中定义)
(3) backwards 在 animation-delay 所指定的一段时间内,在动画显示之前,应用开始属性值(在第一个关键帧中定义)
(4) both 向前和向后填充模式都被应用
animation:name duration timing-function delay iteration-count direction;同时设置多个属性

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
可以使用CSS3的动画属性和伪元素来实现边框跑马灯特效。 首先,需要给元素设置一个固定的宽度和高度,并设置边框样式。然后,使用伪元素::before和::after来创建两个半透明的遮罩层,覆盖在元素的边框上方。 接下来,使用CSS3的动画属性来使伪元素移动。将伪元素的宽度设置为0,然后使用@keyframes关键字定义动画,让伪元素从左侧或右侧移动到另一侧,并将宽度逐渐增加到与元素相同的宽度。 最后,使用animation属性将动画应用到伪元素上,并设置动画的持续时间、延迟时间和循环次数。 下面是一个示例代码: HTML代码: ```html <div class="marquee"> <p>这是一个边框跑马灯特效</p> </div> ``` CSS代码: ```css .marquee { width: 500px; height: 100px; padding: 20px; border: 2px solid #000; position: relative; } .marquee::before, .marquee::after { content: ""; position: absolute; top: 0; bottom: 0; width: 0; background-color: rgba(255, 255, 255, 0.5); } .marquee::before { left: 0; animation: marquee-left 5s ease-in-out infinite; } .marquee::after { right: 0; animation: marquee-right 5s ease-in-out infinite; } @keyframes marquee-left { 0% { width: 0; transform: translateX(0); } 50% { width: 100%; transform: translateX(0); } 100% { width: 0; transform: translateX(100%); } } @keyframes marquee-right { 0% { width: 0; transform: translateX(0); } 50% { width: 100%; transform: translateX(0); } 100% { width: 0; transform: translateX(-100%); } } ``` 在这个示例中,我们创建了一个宽度为500px、高度为100px的div元素,并设置了2px的黑色边框。然后,在div元素上使用了伪元素::before和::after,并设置它们的宽度为0,背景色为半透明白色。 接着,使用@keyframes关键字分别定义了两个动画marquee-left和marquee-right,让伪元素从左侧或右侧移动到另一侧,并将宽度逐渐增加到与元素相同的宽度。最后,将动画应用到伪元素上,并设置了动画的持续时间、延迟时间和循环次数。 这样,一个简单的边框跑马灯特效就完成了。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值