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

html-css08

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>

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

1. js动画代码相对复杂一些

2. 动画运行时,对动画的控制程度上,js能够让动画暂停、取消、终止,css动画不能添加事件

3.动画性能看,js动画多了一个js解析的过程,性能不如css动画好

引入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>