美文网首页
13_JS缓动动画

13_JS缓动动画

作者: 对方不想理你并向你抛出一个异常 | 来源:发表于2017-09-10 14:35 被阅读0次

三个取整函数

这三个函数都是数学函数, Math

  • Math.ceil() 向上取整 天花板
    console.log(Math.ceil(1.01)) 结果 是 2
    console.log(Math.ceil(1.9)) 结果 2
    console.log(Math.ceil(-1.3)) 结果 是 -1
  • Math.floor() 向下取整 地板
    console.log(Math.floor(1.01)) 结果 是 1
    console.log(Math.floor(1.9)) 结果 1
    console.log(Math.floor(-1.3)) 结果 是 -2
  • Math.round() 四舍五入函数
    console.log(Math.round(1.01)) 结果 是 1
    console.log(Math.round(1.9)) 结果 是 2

缓动动画

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

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        div{
            width: 100px;
            height: 100px;
            position: absolute;
            background-color: pink;
            left: 0;
        }
    </style>
    <script type="text/javascript">
        function $(id){return document.getElementById(id);}
        window.onload = function () {
            var box = $("box");
            $("btn400").onclick = function () {
                animate(box,400);
            }
            $("btn800").onclick = function () {
                animate(box,800);
            }
        }
        function animate(obj,target){
            clearInterval(obj.timer);
            obj.timer = setInterval(function () {
                var step = (target - obj.offsetLeft)/10;
                step = step>0?Math.ceil(step):Math.floor(step);
                obj.style.left = obj.offsetLeft+step+"px";
                if(obj.offsetLeft == target){
                    clearInterval(obj.timer);
                }
            },20);
        }
    </script>
</head>
<body>
<button id="btn400">400</button>
<button id="btn800">800</button>
<div id="box"></div>
</body>
</html>

JS常用访问 CSS 属性

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

  • 利用点语法

box.style.width
box.style.top

如上点语法可以得到width属性和top属性,带有单位的,如:100px
但是这个语法有非常大的缺陷, 不变的。 后面的widthtop没有办法传递参数的。
不能这样写:var w = width;box.style.w

  • 利用 [] 访问属性

box.style["left"]);
元素.style[“属性”];

语法格式: box.style[“width”]
最大的优点 : 可以给属性传递参数

得到css 样式

我们想要获得css 的样式, box.style.leftbox.style.backgorundColor
但是它只能得到行内的样式。 但是我们工作最多用的是内嵌式或者外链式
怎么办? 核心: 我们怎么才能得到内嵌或者外链的样式呢?

  • obj.currentStyle IE、opera 常用
    外部(使用<link>)和内嵌(使用<style>)样式表中的样式(IE和opera常用)
  • window.getComputedStyle("元素", "伪类") w3c
    两个选项是必须的, 没有伪类 用 null 替代
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        div{
            width: 100px;
            height: 100px;
            background-color: pink;
            position: absolute;
            left: 10px;
            top: 0;
        }
    </style>
</head>
<body>
    <div id="box"></div>
    <script type="text/javascript">
        var box = document.getElementById("box");
        //IE和Opera支持
        //IE所有版本中:100px,chrome中报错
        console.log(box.currentStyle["width"]);
        //W3C支持的
        //chrome和IE9+中:10px,IE6、7、8中报错
        console.log(window.getComputedStyle(box,null)["left"]);
    </script>
</body>
</html>
  • 兼容写法 :
    我们这个元素里面的属性很多, left top width ===
    我们想要某个属性, 就应该 返回改属性,所有继续封装返回当前样式的 函数。
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        div{
            width: 100px;
            height: 100px;
            background-color: pink;
            position: absolute;
            left: 20px;
            top: 10px;
            z-index: 2;
            opacity: 0.4;
        }
    </style>
</head>
<body>
    <div id="box"></div>
    <script type="text/javascript">
        var box = document.getElementById("box");
        function getStyle(obj,attr){
            if(obj.currentStyle){
                return obj.currentStyle[attr];
            }else{
                return window.getComputedStyle(obj,null)[attr];
            }
        }
        console.log(getStyle(box,"width"));//100px
        console.log(getStyle(box,"background-color"));//IE中:pink,chrome中:rgb(255, 192, 203)
        console.log(getStyle(box,"position"));//absolute
        console.log(getStyle(box,"opacity"));//0.4
        console.log(getStyle(box,"left"));//20px
        console.log(getStyle(box,"z-index"));2
    </script>
</body>
</html>

例:运动框架的基本函数(单个属性)的封装

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        div{
            width: 100px;
            height: 100px;
            position: absolute;
            background-color: pink;
            left: 0;
            top: 60px;
        }
    </style>
    <script type="text/javascript">
        function getStyle(obj,attr){
            if(obj.currentStyle){
                return obj.currentStyle[attr];
            }else{
                return window.getComputedStyle(obj,null)[attr];
            }
        }
        function $(id){return document.getElementById(id);}
        window.onload = function () {
            var box = $("box");
            $("btn400").onclick = function () {
                animate(box,"height",600);
            }
            $("btn800").onclick = function () {
                animate(box,"left",400);
            }
        }
        function animate(obj, attr, target){
            clearInterval(obj.timer);
            obj.timer = setInterval(function () {
                var current = parseInt(getStyle(obj,attr));
                var step = (target - current)/10;
                step = step>0?Math.ceil(step):Math.floor(step);
                obj.style[attr] = current+step+"px";
                if(current == target){
                    clearInterval(obj.timer);
                }
            },20)
        }
    </script>
</head>
<body>
<button id="btn400">600</button>
<button id="btn800">400</button>
<div id="box"></div>
</body>
</html>

JSON遍历:for in遍历

for in 关键字

for (变量 in 对象)
{ 执行语句; }

例:运动框架的基本函数(多个属性)的封装

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        div{
            width: 100px;
            height: 100px;
            position: absolute;
            background-color: pink;
            left: 0;
            top: 60px;
        }
    </style>
    <script type="text/javascript">
        function getStyle(obj,attr){
            if(obj.currentStyle){
                return obj.currentStyle[attr];
            }else{
                return window.getComputedStyle(obj,null)[attr];
            }
        }
        function $(id){return document.getElementById(id);}
        window.onload = function () {
            var box = $("box");
            $("btn400").onclick = function () {
                animate(box,{left:200,top:200});
            }
            $("btn800").onclick = function () {
                animate(box,{width:200,left:200,top:200});
            }
            function animate(obj , json){
                clearInterval(obj.timer);
                obj.timer = setInterval(function () {
                    /*
                        关闭定时器的思路,不用判断多个属性都到达target,
                        只要判断有一个属性未到target,定时器就不能停止
                     */
                    //用户判断是否可以关闭定时器
                    var flag  = true;
                    //开始遍历
                    for(var attr in json){
                        //当前位置
                        var current = parseInt(getStyle(obj,attr));
                        //计算步长
                        //target = json[attr]
                        var step = (json[attr] - current)/10;
                        step = step>0?Math.ceil(step):Math.floor(step);

                        obj.style[attr] = current+step+"px";
                        if(current != json[attr]){
                            flag = false;
                        }
                    }
                    if(flag){//如果flag=true,说明多个属性都=target了
                        clearInterval(obj.timer);
                        alert("动画完成");
                    }

                },20);
            }
        }

    </script>
</head>
<body>
<button id="btn400">600</button>
<button id="btn800">400</button>
<div id="box"></div>
</body>
</html>

回调函数

例:仿360开机弹窗的动画

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        .box{
            position: fixed;
            right: 0;
            bottom: 0;
        }
        img{
            display: block;
        }
        span{
            position: absolute;
            /*background-color: pink;*/
            width: 30px;
            height: 30px;
            right: 0;
            top: 0;
            cursor: pointer;
        }
    </style>
    <script type="text/javascript">
        function getStyle(obj,attr){
            if(obj.currentStyle){
                return obj.currentStyle[attr];
            }else{
                return window.getComputedStyle(obj,null)[attr];
            }
        }
        function animate(obj , json , fn){
            clearInterval(obj.timer);
            obj.timer = setInterval(function () {
                /*
                 关闭定时器的思路,不用判断多个属性都到达target,
                 只要判断有一个属性未到target,定时器就不能停止
                 */
                //用户判断是否可以关闭定时器
                var flag  = true;
                //开始遍历
                for(var attr in json){
                    //当前位置
                    var current = parseInt(getStyle(obj,attr));
                    //计算步长
                    //target = json[attr]
                    var step = (json[attr] - current)/10;
                    step = step>0?Math.ceil(step):Math.floor(step);

                    obj.style[attr] = current+step+"px";
                    if(current != json[attr]){
                        flag = false;
                    }
                }
                if(flag){//如果flag=true,说明多个属性都=target了
                    clearInterval(obj.timer);
                    if(fn) fn();
                }

            },20);
        }
        
        window.onload = function () {
            function $(id){return document.getElementById(id);}
            var box = $("box");
            var hd = box.getElementsByTagName("div")[0];
            var bd = box.getElementsByTagName("div")[1];
            var close = $("close");
            close.onclick = function () {
                animate(bd,{height:0}, function () {
                   animate(hd.parentNode,{width:0});
                });
            }
        }
    </script>
</head>
<body>
    <div class="box" id="box">
        <span id="close"></span>
        <div class="hd">
            ![](images/t.jpg)
        </div>
        <div class="bd">
            ![](images/b.jpg)
        </div>
    </div>
</body>
</html>

in 运算符

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

<script type="text/javascript">
        var json = {name:"李德华",age:"45"};
        if("name" in json){
            alert("YES");
        }else{
            alert("NO");
        }
    </script>

例:运动框架的函数封装(带透明度的)

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        div{
            width: 100px;
            height: 100px;
            position: absolute;
            background-color: pink;
            left: 0;
            top: 60px;
        }
    </style>
    <script type="text/javascript">
        function getStyle(obj,attr){
            if(obj.currentStyle){
                return obj.currentStyle[attr];
            }else{
                return window.getComputedStyle(obj,null)[attr];
            }
        }
        function $(id){return document.getElementById(id);}
        window.onload = function () {
            var box = $("box");
            $("btn400").onclick = function () {
                animate(box,{left:200,top:200,opacity:40});
            }
            $("btn800").onclick = function () {
                animate(box,{width:200,left:200,top:200});
            }
            function animate(obj , json){
                clearInterval(obj.timer);
                obj.timer = setInterval(function () {
                    /*
                        关闭定时器的思路,不用判断多个属性都到达target,
                        只要判断有一个属性未到target,定时器就不能停止
                     */
                    //用户判断是否可以关闭定时器
                    var flag  = true;
                    //开始遍历
                    for(var attr in json){
                        //当前位置
                        var current;
                        if(attr == "opacity"){
                            if("opacity" in obj.style){
                                current = parseInt(getStyle(obj,attr)*100);
                            }else{//IE6、7、8
                                var alphaStr = getStyle(obj,"filter");
                                var cs = alphaStr.substring(alphaStr.lastIndexOf("=")+1 , alphaStr.lastIndexOf(")"));
                                current = isNaN(parseInt(cs))?100:parseInt(cs);
                            }

                        }else{
                            current = parseInt(getStyle(obj,attr));
                        }

                        //计算步长
                        //target = json[attr]
                        var step = (json[attr] - current)/10;
                        step = step>0?Math.ceil(step):Math.floor(step);
                        if(attr == "opacity"){//判断用户是否要修改opacity属性
                            if("opacity" in obj.style){//判断浏览器是否支持opacity属性
                                obj.style.opacity = (current+step)/100;
                            }else{
                                obj.style.filter = "alpha(opacity="+(current+step)+")";
                            }
                        }else{
                            obj.style[attr] = current+step+"px";
                        }
                        if(current != json[attr]){
                            flag = false;
                        }
                    }
                    if(flag){//如果flag=true,说明多个属性都=target了
                        clearInterval(obj.timer);
                        alert("动画完成");
                    }

                },20);
            }
        }

    </script>
</head>
<body>
<button id="btn400">600</button>
<button id="btn800">400</button>
<div id="box"></div>
</body>
</html>

例:手风琴效果

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        *{
            margin: 0;
            padding: 0;

        }
        ul,li{
            list-style: none;
        }
        .box{
            width: 1200px;
            height: 400px;
            margin: 50px auto;
            border: 1px solid red;
            overflow: hidden;
        }
        .box ul{
            width: 120%;
            height: 100%;
        }
        .box li{
            float: left;
            width: 240px;
            height: 400px;
            /*border: 1px solid red;*/
        }
    </style>
    <script type="text/javascript" src="animate.js"></script>
    <script type="text/javascript">
        function $(id){return document.getElementById(id);}
        window.onload = function () {
            var lis = $("ul").children;
            for(var i = 0;i<lis.length ; i++){
                lis[i].style.backgroundImage = "url(images/"+(i+1)+".jpg)";
                lis[i].onmouseover = function () {
                    for(var j = 0;j<lis.length ;j++){
                        animate(lis[j],{width:75});
                    }
                    animate(this,{width:900});
                }
            }
            $("ul").onmouseout = function () {
                for(var i = 0;i<lis.length ; i++){
                    animate(lis[i],{width:240});
                }
            }
        }
    </script>
</head>
<body>
    <div class="box">
        <ul id="ul">
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div>
</body>
</html>

相关文章

网友评论

      本文标题:13_JS缓动动画

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