夜光序言:

 

 

 

再好的东西,都有失去的一天。

再深的记忆,也有淡忘的一天。

再爱的人,也有远走的一天。

再美的梦,也有苏醒的一天。

该放弃的决不挽留。

 

 

 

 

夜光带你走进 前端工程师(二十)

 

 

 

正文:Canvas绘制动画

 

如何让小方块动起来,嘿嘿

夜光带你走进 前端工程师(二十)

动起来:如何设置

夜光带你走进 前端工程师(二十)

/**
 * Created by Maibenben on 2018/4/9.
 */
function draw(id){
    var canvas = document.getElementById(id);
    context = canvas.getContext('2d');
    setInterval(painting,100);
    i=0;
}
function painting(){
    context.fillStyle = "green";
    context.fillRect(i,0,10,10);
    i=i+20;
}

 

画布大小决定了以上代码动画效果,改进一下


夜光带你走进 前端工程师(二十)

setInterval(painting,100);

//100是毫秒

夜光带你走进 前端工程师(二十)

//变成进度条效果

I=i+10 【i++】

夜光带你走进 前端工程师(二十)

实现下面这个效果:

/**
 * Created by Maibenben on 2018/4/9.
 */
function draw(id){
    var canvas = document.getElementById(id);
    context = canvas.getContext('2d');
    setInterval(painting,100);
    i=0;
}
function painting(){
    context.fillStyle = "green";
    context.fillRect(i,i,10,10);
    i++;
}

 


夜光带你走进 前端工程师(二十)

  context.fillRect(i,i,10,10);      //这个是关键,控制动画效果

夜光带你走进 前端工程师(二十)

 Css3中的动画

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>transition</title>
    <style>
        div{
            width: 200px;
            height: 200px;
            background-color: aquamarine;
        }
        div:hover{
            background-color: aqua;
        }
    </style>
</head>
<body>
<h1>transition属性</h1>
<div>

</div>
</body>
</html>


夜光带你走进 前端工程师(二十)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>transition</title>
    <style>
        div{
            width: 200px;
            height: 200px;
            background-color: aquamarine;
            transition: all 1s linear ;  //关键之处
        }
        div:hover{
            background-color: aqua;
        }
    </style>
</head>
<body>
<h1>transition属性</h1>
<div>

</div>
</body>
</html>

 

 

 

 

<head>
    <meta charset="UTF-8">
    <title>transition</title>
    <style>
        div{
            width: 200px;
            height: 200px;
            background-color: aquamarine;
            transition: all 1s linear ;
            -moz-transition: all 1s linear;
            -webkit-transition: all 1s linear;
            -o-transition:all 1s linear;
        ;
        }
        div:hover{
            background-color: aqua;
        }
    </style>
</head>

夜光带你走进 前端工程师(二十)

//实现平滑的过渡效果

夜光带你走进 前端工程师(二十)

<style>
    div{
        width:200px;
        height:200px;
        background-color: aquamarine;
    }
    @-webkit-keyframes mycolor {
        0%{
            background-color: #ffed60;
        }
        10%{
            background-color: bisque;
        }
        20%{
            background-color: cornflowerblue;
        }
        80%{
            background-color: darkseagreen;
        }
        100%{
            background-color: aquamarine;
        }
    }
</style>

 

 

//没有指定调用方法,所以还需要制定调用方法

夜光带你走进 前端工程师(二十)

不断变换颜色的动画制作完毕:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>animations属性</title>
    <style>
        div{
            width:200px;
            height:200px;
            background-color: aquamarine;
        }
        @-webkit-keyframes mycolor {            //animations属性必须用这种
            0%{
                background-color: #ffed60;
            }
            10%{
                background-color: bisque;
            }
            20%{
                background-color: cornflowerblue;
            }
            80%{
                background-color: darkseagreen;
            }
            100%{
                background-color: aquamarine;
            }
        }
        div:hover{      //hover:鼠标移动上去的时候
            -webkit-animation-name: mycolor;
            -webkit-animation-duration: 5s;
            -webkit-animation-timing-function: linear;
            -webkit-animation-iteration-count: infinite;
        }                //这里必须写:调用方法
    </style>
</head>
<body>
<h1>animations</h1>
<div></div>
</body>
</html>

 


夜光带你走进 前端工程师(二十)

 

这是会移动的变换颜色:

【夜光】

<style>
    div{
        width:100px;
        height:200px;
        background-color: aquamarine;
    }
    @-webkit-keyframes mycolor {
        0%{
            background-color: #ffed60;
        }
        10%{
            background-color: bisque;
            width: 200px;
        }
        20%{
            background-color: cornflowerblue;
            width:400px;
        }
        80%{
            background-color: darkseagreen;
            width: 600px;;
        }
        100%{
            background-color: aquamarine;
            width:800px;
        }
    }
    div:hover{
        -webkit-animation-name: mycolor;
        -webkit-animation-duration: 5s;
        -webkit-animation-timing-function: linear;
        -webkit-animation-iteration-count: infinite;
    }
</style>

再来一个更加复杂的动画style[animations]

<style>
    div{
        width:100px;
        height:200px;
        background-color: aquamarine;
    }
    @-webkit-keyframes mycolor {
        0%{
            background-color: #ffed60;
            -webkit-transform: rotate(0deg);
        }
        10%{
            background-color: bisque;
            width: 200px;
            -webkit-transform: rotate(10deg);
        }
        20%{
            background-color: cornflowerblue;
            width:400px;
            -webkit-transform: rotate(40deg);
        }
        80%{
            background-color: darkseagreen;
            width: 600px;;
            -webkit-transform: rotate(80deg);
        }
        100%{
            background-color: aquamarine;
            width:800px;
            -webkit-transform: rotate(100deg);
        }
    }
    div:hover{
        -webkit-animation-name: mycolor;
        -webkit-animation-duration: 5s;
        -webkit-animation-timing-function: linear;
        -webkit-animation-iteration-count: infinite;
    }
</style>

//动画效果一部分如下图所显示:

夜光带你走进 前端工程师(二十)

 

 

相关文章:

  • 2021-10-20
  • 2021-10-17
  • 2021-08-26
  • 2021-04-08
  • 2021-12-30
  • 2021-07-05
  • 2021-05-18
猜你喜欢
  • 2021-12-16
  • 2021-04-23
  • 2021-11-07
  • 2021-09-19
  • 2021-09-04
  • 2021-12-23
  • 2022-01-18
相关资源
相似解决方案