JS 怎么动态设置CSS3动画的样式

html-css016

JS 怎么动态设置CSS3动画的样式,第1张

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

简单的不用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>

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

盒子 原来的位置 0+ 10 盒子现在的offsetLeft 10

|-5| = 5

这三个函数都是 数学函数

Math

比如说 console.log(Math.ceil(1.01)) 结果 是 2

console.log(Math.ceil(1.9))结果 2

console.log(Math.ceil(-1.3)) 结果 是 -1

比如说 console.log(Math.floor(1.01)) 结果 是 1

console.log(Math.floor(1.9)) 结果 1

console.log(Math.floor(-1.3)) 结果 是 -2

console.log(Math.round(1.01)) 结果 是 1

console.log(Math.round(1.9)) 结果 是 2

匀速动画的原理: 盒子本身的位置 + 步长

缓动动画的原理:盒子本身的位置 + 步长 (不断变化的)

( 缺陷:只能水平方向!随后的“封装运动框架单个属性会进一步改进” )

我们访问得到css 属性,比较常用的有两种:

点语法可以得到 width 属性 和 top属性 ** 带有单位的 。 100px

但是这个语法有非常大的 缺陷**, 不变的。

后面的width 和 top 没有办法传递参数的。

var w = width

box.style.w

最大的优点 : 可以给属性传递参数

我们想要获得css 的样式, box.style.left 和 box.style.backgorundColor

但是它只能得到 行内的样式。

但是我们工作最多用的是 内嵌式 或者 外链式 。

怎么办?

核心: 我们怎么才能得到内嵌或者外链的样式呢?

外部(使用<link>)和内嵌(使用<style>)样式表中的样式(ie和opera)

两个选项是必须的, 没有伪类 用 null 替代

我们这个元素里面的属性很多, left top width ===

我们想要某个属性, 就应该 返回该属性,所有继续封装 返回当前样式的 函数。

千万要记得 每个 的意思 : 那是相当重要

flag在js中一般作为开关,进行判断。

等动画执行完毕再去执行的函数 回调函数

我们怎么知道动画就执行完毕了呢?

很简单 当定时器停止了。 动画就结束了

案例源码:

in运算符也是一个二元运算符,但是对运算符左右两个操作数的要求比较严格。in运算符要求第1个(左边的)操作数必须是字符串类型或可以转换为字符串类型的其他类型,而第2个(右边的)操作数必须是数组或对象。只有第1个操作数的值是第2个操作数的属性名,才会返回true,否则返回false

案例源码:

链接: http://pan.baidu.com/s/1miEvqoo

密码:7fv8