js+css如何实现动画效果?

html-css09

js+css如何实现动画效果?,第1张

简单的不用js就行

<!DOCTYPE HTML>

<html>

<head>

  <meta charset= "utf8">

  <title>untitled</title>

  <link rel = "stylesheet" type = "text/css" href = "">

  <style type = "text/css">

  *{

    margin: 0px

    padding: 0px

  }

  #a{

   position: absolute

   width: 50px

   height: 50px

   background-color: #f3e9e0

   border-radius: 50%

   left: 400px

   top: 200px

  }

  #a div{

   position: absolute

   width: 50px

   height: 50px

   border-radius: 50%

   transition: all 0.5s

   left: 0px

   top: 0px

  }

  #a :nth-child(1){

   background-color: #c1d4ed

  }

  #a :nth-child(2){

   background-color: #7d6e69

  }

  #a :nth-child(3){

   background-color: #dad6d5

  }

  #a :nth-child(4){

   background-color: #caaa9d

  }

  #a :nth-child(5){

   background-color: #6bdeff

  }

  #a:hover :nth-child(1){

   left: 150px

   top: -150px

  }

  #a:hover :nth-child(2){

   left: 150px

   top: 150px

  }

  #a:hover :nth-child(3){

   left: 300px

   top: -150px

  }

  #a:hover :nth-child(4){

   left: 300px

   top: 150px

  }

  #a:hover :nth-child(5){

   left: 450px

   top: 0px

  }

  </style>

</head>

<body>

<div id = 'a'>

<div></div>

<div></div>

<div></div>

<div></div>

<div></div>

</div>

</body>

</html>

鼠标伸到球上 自动扩散移动

引入jquery

然后给你要设置动画的对象增加或者删除css3动画的类就可以了。

如我这里用colorchange这个渐变类在css里面写好动画效果以后在js里面给对象添加上就可以实现动画了

<!DOCTYPE html>

<html>

<head lang="en">

    <meta charset="UTF-8">

    <title>Test</title>

    <style type="text/css">

        body{

            padding: 20px

            background-color:#FFF

        }

        .colorchange

        {

            animation:myfirst 5s

            -moz-animation:myfirst 5s /* Firefox */

            -webkit-animation:myfirst 5s /* Safari and Chrome */

            -o-animation:myfirst 5s /* Opera */

        }

        @keyframes myfirst

        {

            from {background:red}

            to {background:yellow}

        }

        @-moz-keyframes myfirst /* Firefox */

        {

            from {background:red}

            to {background:yellow}

        }

        @-webkit-keyframes myfirst /* Safari and Chrome */

        {

            from {background:red}

            to {background:yellow}

        }

        @-o-keyframes myfirst /* Opera */

        {

            from {background:red}

            to {background:yellow}

        }

        #main{

            width:100px

            height:100px

            background:red

        }

        #cgbt{

            width: 100px

            margin: 20px 0 0 0

            text-align: center

            cursor: pointer

        }

        #cgbt:hover{

            background-color: #2D93CA

        }

    </style>

</head>

<body>

<div id="main">

    我会变么?

</div>

<div id="cgbt">

    点我让上面的变颜色

</div>

<script src="jquery-3.2.1.min.js" type="application/javascript"></script>

<script>

    $(document).ready(function(){

        $("#cgbt").click(function(){

            $("#main").attr("class","colorchange")

        })

    })

</script>

</body>

</html>

CSS3的动画的优点:

1.在性能上会稍微好一些,浏览器会对CSS3的动画做一些优化(比如专门新建一个图层用来跑动画)

2.代码相对简单

但其缺点也很明显:

1.在动画控制上不够灵活

2.兼容性不好

3.部分动画功能无法实现(如滚动动画,视差滚动等)

JavaScript的动画正好弥补了这两个缺点,控制能力很强,可以单帧的控制、变换,同时写得好完全可以兼容IE6,并且功能强大。但想想CSS动画的transform矩阵是C++级的计算,必然要比javascript级的计算要快。另外对库的依赖也是一个很让人头疼的问题。

所以,对于一些复杂控制的动画,使用javascript会比较靠谱。而在实现一些小的交互动效的时候,就多考虑考虑CSS吧。