美文网首页前端开发那些事儿
CSS/JS动画基础:让元素动起来你知道几种方式?

CSS/JS动画基础:让元素动起来你知道几种方式?

作者: microkof | 来源:发表于2021-01-24 23:37 被阅读0次

前言

今天用尽可能多的方法让元素动起来,具体说,向右平移100像素。

以下每一项技术都不会深入讲解,请网上另外了解。

一、transform + translate + transition

“trans”三剑客必须合起来用。transform: translate(100px, 0)表示向右平移100px,transition: transform 1s ease-in-out 0.1s;表示transform属性的值的过渡需要用时1秒,以及延时0.1秒开始,以及使用ease-in-out运动效果。

<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    .box {
      width: 200px;
      height: 50px;
      transition: transform 1s ease-in-out 0.1s;
    }
  </style>
</head>

<body>
  <div class="box">我是容器</div>
  <button>动起来!</button>
  <script>
    document.querySelector('button').onclick = function() {
      document.querySelector('.box').style.transform = 'translate(100px, 0)';
    };
  </script>
</body>

</html>

二、animation + keyframes

这个例子是修改margin-left,你也可以修改left之类的。

<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    @keyframes mymove {
      from {margin-left: 0px;}
        to {margin-left: 200px;}
    }
    .box {
      width: 200px;
      height: 50px;
      animation: mymove 1s ease-in-out 0.1s 1 normal forwards paused;
    }
  </style>
</head>

<body>
  <div class="box">我是容器</div>
  <button>动起来!</button>
  <script>
    document.querySelector('button').onclick = function() {
      document.querySelector('.box').style.animationPlayState = 'running';
    };
  </script>
</body>

</html>

三、requestAnimationFrame为代表的延时器

尽管setTimeout和setInterval也可以实现,但推荐requestAnimationFrame,所以我也只以requestAnimationFrame举例。

requestAnimationFrame的缺点是不能简便实现timing-function,也就是linear、ease之类的。

<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    .box {
      width: 200px;
      height: 50px;
    }
  </style>
</head>

<body>
  <div class="box">我是容器</div>
  <button>动起来!</button>
  <script>
    const box = document.querySelector('.box');

    function mymove(number) {
      if (number <= 100) {
        box.style.marginLeft = number + 1 + 'px'; //每一帧向右移动1px
        requestAnimationFrame(() => mymove(number + 1));
      }
    }

    document.querySelector('button').onclick = function() {
      requestAnimationFrame(() => mymove(0));
    };
  </script>
</body>

</html>

四、el.animate()

animate方法是绑定在元素上的方法,可以以js编程方式执行动画。

我不知道它跟CSS动画相比哪个更好,但是可以肯定的是,越复杂的动画越应该使用js编程。

<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    .box {
      width: 200px;
      height: 50px;
    }
  </style>
</head>

<body>
  <div class="box">我是容器</div>
  <button>动起来!</button>
  <script>
    const box = document.querySelector('.box');

    var keyframes = [
      { 
        marginLeft: '0'
      },
      { 
        marginLeft: '100px'
      }
    ];

    document.querySelector('button').onclick = function() {
      box.animate(keyframes, {
        iterations: 1,
        iterationStart: 0,
        delay: 0,
        endDelay: 0,
        duration: 1000,
        fill: 'forwards',
        easing: 'ease-out',
      });
    };
  </script>
</body>

</html>

相关文章

  • CSS/JS动画基础:让元素动起来你知道几种方式?

    前言 今天用尽可能多的方法让元素动起来,具体说,向右平移100像素。 以下每一项技术都不会深入讲解,请网上另外了解...

  • 前端技能概况

    html 块级元素、行内元素、盒子模型 H5新特性 css css3新特性 flex 布局 动画 js js基础 ...

  • 2018-11-27第九天总结

    一、 动画网页动画可以通过以下几种方式实现(gif、flash 除外),css3 动画SVG 动画JS 动画(包括...

  • 2019-01-02 css3过渡动画 css3圆角阴影透明度

    一、 动画网页动画可以通过以下几种方式实现(gif、flash 除外),css3 动画SVG 动画JS 动画(包括...

  • 开发笔记3-H5页面实现帧动画

    动画的实现方式有以下几种方式:js、css、canvas、gif gif对透明效果支持不好,比较方便,但是现在动画...

  • 动画使用库,总结

    导航过渡、动画和转换(2D/3D)【relative】、渐变html元素隐藏的几种方式与动画 一、用到的css动画...

  • Vue中的动画

    基础CSS过渡和动画 动画:keyframes过渡:transition 列表动画 状态动画 组件和元素切换 1....

  • 小程序加入购物车动画

    说明: 之前用vue css3写过抛物线动画,但是小程序中,不支持js操作dom元素,所以你无法用js去去除动画...

  • CSS居中小结

    下面是CSS居中的几种方法: 水平居中元素: 通用方法,元素的宽高未知 方式一:CSS3 transform 方式...

  • 前端工程师需要掌握的技能

    1、 HTML5、DIV+CSS、JS、XML、Json基础知识精通 2、熟悉几种后端语言,通晓前后端的交互方式,...

网友评论

    本文标题:CSS/JS动画基础:让元素动起来你知道几种方式?

    本文链接:https://www.haomeiwen.com/subject/sqilmktx.html