整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

纯CSS实现波浪效果

纯CSS实现波浪效果

直以来,使用纯 CSS 实现波浪效果都是十分困难的。

因为实现波浪的曲线需要借助贝塞尔曲线。

而使用纯 CSS 的方式,实现贝塞尔曲线,额,暂时是没有很好的方法。

当然,借助其他力量(SVG、CANVAS),是可以很轻松的完成所谓的波浪效果的,先看看,非 CSS 方式实现的波浪效果。

使用 SVG 实现波浪效果

借助 SVG ,是很容易画出三次贝塞尔曲线的。

看看效果:

代码如下:

<svg width="200px" height="200px" version="1.1" xmlns="http://www.w3.org/2000/svg">
    <text class="liquidFillGaugeText" text-anchor="middle" font-size="42px" transform="translate(100,120)" style="fill: #000">50.0%</text>
    <!-- Wave -->
    <g id="wave">
        <path id="wave-2" fill="rgba(154, 205, 50, .8)" d="M 0 100 C 133.633 85.12 51.54 116.327 200 100 A 95 95 0 0 1 0 100 Z">
        <animate dur="5s" repeatCount="indefinite" attributeName="d" attributeType="XML" values="M0 100 C90 28, 92 179, 200 100 A95 95 0 0 1 0 100 Z;
                                    M0 100 C145 100, 41 100, 200 100 A95 95 0 0 1 0 100 Z;
                                    M0 100 C90 28, 92 179, 200 100 A95 95 0 0 1 0 100 Z"></animate>
        </path>
    </g>
    <circle cx="100" cy="100" r="80" stroke-width="10" stroke="white" fill="transparent"></circle>
    <circle cx="100" cy="100" r="90" stroke-width="20" stroke="yellowgreen" fill="none" class="percentage-pie-svg"></circle>
</svg>

画出三次贝塞尔曲线的核心在于这一段。感兴趣的可以自行去研究研究。

使用 canvas 实现波浪效果

使用 canvas 实现波浪效果的原理与 SVG 一样,都是利用路径绘制出三次贝塞尔曲线并赋予动画效果。

使用 canvas 的话,代码如下:

$(function() {
    let canvas = $("canvas");
    let ctx = canvas[0].getContext('2d');
    let radians = (Math.PI / 180) * 180;
    let startTime = Date.now();
    let time = 2000;
    let clockwise = 1;
    let cp1x, cp1y, cp2x, cp2y;
     
    // 初始状态
    // ctx.bezierCurveTo(90, 28, 92, 179, 200, 100);
    // 末尾状态
    // ctx.bezierCurveTo(145, 100, 41, 100, 200, 100);
     
    requestAnimationFrame(function waveDraw() { 
        let t = Math.min(1.0, (Date.now() - startTime) / time);
           
        if(clockwise) {
            cp1x = 90 + (55 * t);
            cp1y = 28 + (72 * t);
            cp2x = 92 - (51 * t);
            cp2y = 179 - (79 * t);
        } else {
            cp1x = 145 - (55 * t);
            cp1y = 100 - (72 * t);
            cp2x = 41 + (51 * t);
            cp2y = 100 + (79 * t);
        }
         
        ctx.clearRect(0, 0, 200, 200);
        ctx.beginPath();
        ctx.moveTo(0, 100);
        // 绘制三次贝塞尔曲线
        ctx.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, 200, 100);
        // 绘制圆弧
        ctx.arc(100, 100, 100, 0, radians, 0);
        ctx.fillStyle = "rgba(154, 205, 50, .8)";
        ctx.fill();
        ctx.save(); 
         
        if( t == 1 ) {
            startTime = Date.now();
            clockwise = !clockwise;
        }
 
        requestAnimationFrame(waveDraw);
    });
})

主要是利用了动态绘制 ctx.bezierCurveTo() 三次贝塞尔曲线实现波浪的运动效果,感兴趣的可以自行研究。

纯 CSS 实现波浪效果

好,接下来才是本文的重点!使用纯 CSS 的方式,实现波浪的效果。

你 TM 在逗我?刚刚不是还说使用 CSS 无能为力吗?

是,我们没有办法直接绘制出三次贝塞尔曲线,但是我们可以利用一些讨巧的方法,模拟达到波浪运动时的效果,姑且把下面这种方法看作一种奇技淫巧。

原理

原理十分简单,我们都知道,一个正方形,给它添加 border-radius: 50%,将会得到一个圆形。

border-radius:用来设置边框圆角,当使用一个半径时确定一个圆形。

好的,如果 border-radius 没到 50%,但是接近 50% ,我们会得到一个这样的图形:

注意边角,整个图形给人的感觉是有点圆,却不是很圆。额,这不是废话吗

好的,那整这么个图形又有什么用?还能变出波浪来不成?

没错!就是这么神奇。:) 我们让上面这个图形滚动起来(rotate) ,看看效果:

可能很多人看到这里还没懂旋转起来的意图,仔细盯着一边看,是会有类似波浪的起伏效果的。

而我们的目的,就是要借助这个动态变换的起伏动画,模拟制造出类似波浪的效果。

实现

当然,这里看到是全景实现图,所以感觉并不明显,OK,让我们用一个个例子看看具体实现起来能达到什么样的效果。

我们利用上面原理可以做到的一种波浪运动背景效果图:

CodePen Demo -- Pure CSS Wave[1]

后面漂浮的波浪效果,其实就是利用了上面的 border-radius: 45% 的椭圆形,只是放大了很多倍,视野之外的图形都 overflow: hidden,只留下了一条边的视野,并且增加了一些相应的 transform 变换。

注意,这里背景是蓝色静止的,运动是白色的椭圆形。

代码也很简单,SCSS 代码如下:

body {
    position: relative;
    align-items: center;
    min-height: 100vh;
    background-color: rgb(118, 218, 255);
    overflow: hidden;
 
    &:before, &:after {
        content: "";
        position: absolute;
        left: 50%;
        min-width: 300vw;
        min-height: 300vw;
        background-color: #fff;
        animation-name: rotate;
        animation-iteration-count: infinite;
        animation-timing-function: linear;
    }
 
    &:before {
        bottom: 15vh;
        border-radius: 45%;
        animation-duration: 10s;
    }
 
    &:after {
        bottom: 12vh;
        opacity: .5;
        border-radius: 47%;
        animation-duration: 10s;
    }
}
 
@keyframes rotate {
    0% {
        transform: translate(-50%, 0) rotateZ(0deg);
    }
    50% {
        transform: translate(-50%, -2%) rotateZ(180deg);
    }
    100% {
        transform: translate(-50%, 0%) rotateZ(360deg);
    }
}

为了方便写 DEMO,用到的长度单位是 VW 与 VH,不太了解这两个单位的可以戳这里:vh、vw、vmin、vmax 知多少[2]

可能有部分同学,还存在疑问,OK,那我们把上面的效果缩小 10 倍,将视野之外的动画也补齐,那么其实生成波浪的原理是这样的:

图中的虚线框就是我们实际的视野范围。

值得探讨的点

值得注意的是,要看到,这里我们生成波浪,并不是利用旋转的椭圆本身,而是利用它去切割背景,产生波浪的效果。那为什么不直接使用旋转的椭圆本身模拟波浪效果呢?因为中间高,两边低的效果不符合物理学原理,看上去十分别扭;

可以点进去看看下面这个例子:

CodePen Demo -- pure css wave[3]

使用纯 CSS 实现波浪进度图

好,既然掌握了这种方法,下面我们就使用纯 CSS 实现上面最开始使用 SVG 或者 CANVAS 才能实现的波浪进度图。

HTML 结构如下:

<div class="container">
    <div class="wave"></div>
</div>
.wave {
    position: relative;
    width: 200px;
    height: 200px;
    background-color: rgb(118, 218, 255);
    border-radius: 50%;
  
    &::before,
    &::after{
        content: "";
        position: absolute;
        width: 400px;
        height: 400px;
        top: 0;
        left: 50%;
        background-color: rgba(255, 255, 255, .4);
        border-radius: 45%;
        transform: translate(-50%, -70%) rotate(0);
        animation: rotate 6s linear infinite;
        z-index: 10;
    }
     
    &::after {
        border-radius: 47%;
        background-color: rgba(255, 255, 255, .9);
        transform: translate(-50%, -70%) rotate(0);
        animation: rotate 10s linear -5s infinite;
        z-index: 20;
    }
}
 
@keyframes rotate {
    50% {
        transform: translate(-50%, -73%) rotate(180deg);
    } 100% {
        transform: translate(-50%, -70%) rotate(360deg);
    }
}

效果图:

CodePen Demo -- Pure Css Wave Loading[4]

虽然效果差了一点点,但是相较于要使用学习成本更高的 SVG 或者 CANVAS,这种纯 CSS 方法无疑可使用的场景更多,学习成本更低!

纯 CSS 的充电效果

还能实现类似这样的充电效果:

一些小技巧

单纯的让一个 border-radius 接近 50 的椭圆形旋转,动画效果可能不是那么好,我们可以适当的添加一些其他变换因素,让动画效果看上去更真实:

  • 在动画过程中,动态的改变 border-radius 的值;
  • 在动画过程中,利用 transform 对旋转椭圆进行轻微的位移、变形;
  • 上面也演示到了,多个椭圆同时转动,赋予不同时长的动画,并且添加轻微的透明度,让整个效果更加逼真。
  • 提起图标,大家可能第一个会想到PS、美工等词语,但很多小图标现在根本都不需要再打开PS了。

    1、常见的括号( 前进或后退“>” )

    .arrow{
      width:12rpx;
      height:12rpx; 
      border-top:1px solid #999;
      border-right:1px solid #999;
      transform:rotate(-45deg); 
      position:absolute; 
      right:10px; 
    }


    2、常见的关闭按钮( “X” ),这里需要用到一个伪类

    .close {
            display: inline-block;
            width: 30px;
            height: 4px;
            background: #333;
            transform: rotate(45deg);
        }
    
        .close::after {
            content: '';
            display: block;
            width: 30px;
            height: 4px;
            background: #333;
            transform: rotate(-90deg);
        }


    3、常见的勾选( “√” )

    .check {
        position: relative;
        display: inline-block;
        width: 25px;
        height: 25px;
        background: #333;
        border-radius: 25px;
    }
    .check::after {
        content: "";
        position: absolute;
        left: 5px;
        top: 8px;
        width: 50%;
        height: 25%;
        border: 2px solid #fff;
        border-radius: 1px;
        border-top: none;
        border-right: none;
        background: transparent;
        transform: rotate(-45deg);
    }


    4、常见的加号( “+” ),同样需要利用伪类

    .add {
      width: 100px;
      height: 100px;
      color: #ccc;
      transition: color .25s;
      position: relative;
    }
    
     .add::before{
      content: '';
      position: absolute;
      left: 50%;
      top: 50%;
      width: 80px;
      margin-left: -40px;
      margin-top: -5px;
      border-top: 10px solid;
    }
    
    .add::after {
     content: '';
     position: absolute;
     left: 50%;
     top: 50%;
     height: 80px;
     margin-left: -5px;
     margin-top: -40px;
     border-left: 10px solid;
    }


    5、常见的波浪线( “~” ),同样需要利用伪类

    .info::before {
    content: '';
    position: absolute;
    top: 30px;
    width: 100%;
    height: 0.25em;
    
    background:
     linear-gradient(
    135deg, 
     transparent, 
     transparent 45%, 
     #008000, 
     transparent 55%, 
     transparent 100%
     ),
    linear-gradient(
     45deg, 
     transparent, 
     transparent 45%, 
      #008000, 
     transparent 55%, 
     transparent 100%
    );
    background-size: 0.5em 0.5em;
    background-repeat: repeat-x, repeat-x;
    }


    5、常见的三角形

    .triangle-up {
    width: 0;
    height: 0;
    border-left: 50px solid transparent;
    border-right: 50px solid transparent;
    border-bottom: 100px solid red;
    }


    6、常见的扇形

    .sector {
    width: 0;
    height: 0;
    border-left: 50px solid transparent;
    border-right: 50px solid transparent;
    border-top: 100px solid #f00;
    border-radius: 50%;
    }


    7、仿微信对话框

    .alertDialog {
    /* 对话框:一个圆角矩形和一个小三角形 */
    width: 150px;
    height: 100px;
    background: #f00;
    border-radius: 10px;
    position: relative;
    }
    .alertDialog:before {
    content: "";
    width: 0;
    height: 0;
    position: absolute;
    left: -20px;
    top: 40px;
    border-top: 10px solid transparent;
    border-bottom: 10px solid transparent;
    border-right: 20px solid #f00;
    }


    8、钻石图标

    .diamond {
    /* 钻石:梯形和三角形组成 */
    width: 50px;
    height: 0;
    position: relative;
    border-bottom: 25px solid #f00;
    border-left: 25px solid transparent;
    border-right: 25px solid transparent;
    }
    .diamond:before {
    content: "";
    width: 0;
    height: 0;
    position: absolute;
    border-left: 50px solid transparent;
    border-right: 50px solid transparent;
    border-top: 70px solid #f00;
    left: -25px;
    top: 25px;
    }


    9、五角星图标

    .starFive {
     width: 0;
     height: 0;
     position: relative;
     border-left: 80px solid transparent;
     border-right: 80px solid transparent;
     border-bottom: 60px solid #f00;
     transform: rotate(35deg);
    }
    .starFive:before {
     content: "";
     position: absolute;
     width: 0;
     height: 0;
     border-left: 80px solid transparent;
     border-right: 80px solid transparent;
     border-bottom: 60px solid #f00;
     transform: rotate(-70deg);
     top: 3px;
     left: -80px;
    }
    .starFive:after {
     content: "";
     position: absolute;
     width: 0;
     height: 0;
     border-bottom: 60px solid #f00;
     border-right: 20px solid transparent;
     border-left: 20px solid transparent;
     transform: rotate(-35deg);
            top: -40px;
            left: -49px;
    }


    喜欢的可以加个关注,不定期发布更多CSS相关文章

    1)背景样式属性,用于定义 HTML 元素的背景色、背景图片,同时还可以进行背景定位、背景图片重复、背景图片固定。

    • background-color
    • background-image
    • background-size
    • background-position
    • background-repeat

    background-color 属性可以给指定标签元素设置背景色。

    举个例子! 我们给 body 元素设置一个背景颜色:

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <style>
          body {
            background-color: yellowgreen;
          }
        </style>
      </head>
      <body></body>
    </html>

    background-image 属性可以把图像插入背景。background-size 属性可以给背景图设置大小。

    举个例子! 我们给 body 元素设置一个背景图像。

    wget https://labfile.oss.aliyuncs.com/courses/3773/moon.jpg
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <style>
          body {
            background-image: url("moon.jpg");
            background-size: 300px 300px;
          }
        </style>
      </head>
      <body></body>
    </html>


    通过 background-position 属性,可以改变图像在背景中的位置。

    background-position 属性,设置属性值为居中:

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <style>
          body {
            background-image: url("moon.jpg");
            background-size: 300px 300px;
            background-position: center;
          }
        </style>
      </head>
      <body></body>
    </html>


    background-repeat 属性是用来设置背景图像是否平铺。

    下表列出了 background-repeat 属性的一些可取值以及每个可取值的含义。

    可 取 值

    描 述

    repeat

    背景图像将在垂直方向和水平方向重复(默认值)

    repeat-x

    背景图像将在水平方向重复

    repeat-y

    背景图像将在垂直方向重复

    no-repeat

    背景图像将仅显示一次

    我们规定应该从父元素继承 background-repeat 属性的设置。

    background-repeat 属性并设置值为不平铺:

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <style>
          body {
            background-image: url("moon.jpg");
            background-size: 300px 300px;
            background-position: center;
            background-repeat: no-repeat;
          }
        </style>
      </head>
      <body></body>
    </html>


    (2)文本相关的属性

    • line-height 属性
    • text-indent 属性
    • text-align 属性
    • letter-spacing 属性
    • text-decoration 属性
    • white-space 属性
    • line-break 属性

    文本属性用于定义文本的样式,通过文本属性,可以改变文本的颜色、字间距、对齐方式、文本修饰和文本缩进等。常用文本属性如下表所示:

    属 性

    可 取 值

    描 述

    line-height

    normal、number、length、%

    设置行高

    text-indent

    length、%

    设置文本缩进

    text-align

    left、right、center、justify、start、end

    设置对齐方式

    letter-spacing

    normal、length

    设置字符间距

    text-decoration

    line、color、style、thickness

    设置文本修饰

    white-space

    normal、pre、nowrap、pre-wrap、pre-line、break-spaces

    规定如何处理空白

    line-break

    auto、loose、normal、strict、anywhere、unset

    处理如何断开带有标点符号的文本的行

    line-height 用于设置多行元素的空间量,可取值具体说明如下:

    • normal:取决于用户端。
    • number:数字乘以元素的字体大小。
    • length:指定长度用于计算高度。
    • %:计算值是给定的百分比值乘以元素计算出的字体大小。

    例子,

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>line-height 的使用</title>
        <style>
          div {
            width: 300px;
            height: 400px;
            border: 1px solid;
            font-size: 15px;
            display: inline-block;
            vertical-align: top;
          }
          .div1 {
            line-height: 2; /*15 * 2*/
          }
          .div2 {
            line-height: 30%; /*15 * 30% */
          }
        </style>
      </head>
      <body>
        <div class="div1">
          <p>“海水呀,你说的是什么?”</p>
          <p>“是永恒的疑问。”</p>
          <p>“天空呀,你回答的话是什么?”</p>
          <p>“是永恒的沉默。”</p>
        </div>
        <div class="div2">
          <p>“海水呀,你说的是什么?”</p>
          <p>“是永恒的疑问。”</p>
          <p>“天空呀,你回答的话是什么?”</p>
          <p>“是永恒的沉默。”</p>
        </div>
      </body>
    </html>
    

    显示为,