css操作,简单的海浪

html-css013

css操作,简单的海浪,第1张

实现思路:

用一个铺满蓝色的背景的盒子,

利用::before与after画2个圆角值不同的不规则圆形(其中一个设置透明度或者其他颜色,以便区分):

父元素设置overflow:hidden;

最后加上animation 动画让不同规则圆形旋转起来即可:

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <title>Document</title>

</head>

<body>

    <div class="wave"></div>

</body>

<style>

    /* // 简单的盒子 */

.wave {

  position: relative

  width: 150px

  height: 150px

  background-color: #5291e0

  /* overflow: hidden*/

}

/* // 两个不规则圆形(相对盒子进行定位,距离底部距离则为波浪高度) */

.wave::before,

.wave::after {

    content: ""

    position: absolute

    left: 50%

    bottom: 15%

    width: 500%

    height: 500%

    border-radius: 45%

    background-color: #fff

  transform: translateX(-50%)

    animation: rotate 15s linear infinite

  }

  /* // 其中一个不规则圆形调整一下样式,以便区分(或者调整animation的参数来区分) */

  .wave::before {

    bottom: 10%

    opacity: .5

    border-radius: 47%

}

/* // 旋转动画 */

@keyframes rotate {

  from {

    transform: translateX(-50%) rotateZ(0deg)

  }

  to {

    transform: translateX(-50%) rotateZ(360deg)

  }

}

</style>

</html>

transform 不会使DOM脱离文档流,当通过 translateX 等属性值移动了元素后,它仍然占据原来的位置。

好处是, transform 制作的动画会直接进入合成阶段,避开重排重绘,可以通过 Performance 录制面板来查看 transform 动画的效率。

MDN animation

深入浅出CSS动画

MDN animate()

监听 CSS animation 动画的事件:

这些监听事件对 animate() 是无效的。

页面顶部经常会见到水平无限轮播的公告。

由于轮播的内容是动态的,可能很多,也可能很少,如果公告内容的宽度没有超过最大宽度限制,那么就不应该轮播,如果超过了,则发起轮播。

假设我们永远只有一条最新的公告

原理:当一次动画执行结束时,影子内容的头部刚好对准轮播内容的初始位置,那么下次动画开始时,轮播内容将重新回到初始位置,由于影子内容与轮播内容相同,那么就给人造成一种无限轮播的错觉。

逻辑实现:父元素设置了 overflow: hidden ,又想要获取父元素、子元素的真实宽度,那么可以通过 scrollWidth 获取。

兼容性:如果不支持 animate() ,那么我们可以动态创建 <style>+ @keyframes ,插入 <head>, 但也要记得移除。