• JS完美运动框架


    多个值同时变化

    setStyle同时设置多个属性

    参数传递

    json的使用

    for  in遍历属性

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            #div{
                width: 200px;
                height: 200px;
                background: red;
                margin: 20px;
                filter: alpha(30);
                opacity: 0.3;
            }
        </style>
       
        <script>
            window.onload = function(){
                var oDiv = document.getElementById('div');
                oDiv.onmouseover = function () {
                    startMove(oDiv, {202,height:300,opacity:100},function () {
                        console.log(123)//判断定时器有没有停止
                    })
                }
            }
        </script>
    </head>
    <body>
        <div id="div"></div>
    </body>
    <script src="move2.js"></script>
    </html>

    move2.js

    function getStyle(obj,name){
        if (obj.currentStyle) {
            return obj.currentStyle[name];
        } else {
            return getComputedStyle(obj,false)[name]
        }
    }
    //var alpha = 30;所有的东西都不能公用
    function startMove (obj,json,fnEnd) {
        clearInterval(obj.time);
        obj.time = setInterval(function(){
            var bStop=true;//假设所有的值都到了
            for(var attr in json){
                var cur = 0;
                if (attr == 'opacity') {
                    cur = Math.round(parseFloat(getStyle(obj,attr))*100);
                } else {
                    cur = parseInt(getStyle(obj,attr));
                }
                var speed = (json[attr]-cur)/6;
                speed = speed>0?Math.ceil(speed):Math.floor(speed);
    
                if(cur !== json[attr]){//如果有一个值没到
                    bStop=false;//设置为fslse
                }
                if (attr == 'opacity') {
                    obj.style.filter = 'alpha(opacity:'+(cur+speed)+')';
                    obj.style.opacity = (cur+speed)/100;
                } 
                else {
                    obj.style[attr] = cur+speed+'px';
                }
            }
                if (bStop) {
                    clearInterval(obj.time);
                    if (fnEnd) fnEnd();
                }
           
        },30)
    }
  • 相关阅读:
    js上传照片本地预览
    2020年6月23日第一次面试题(外派PA)
    笔记
    2020VUE系统回顾与学习
    2019最全前端面试问题及答案总结
    常见的浏览器兼容性问题总结
    Vue咖啡app项目总结
    跨域问题研究总结
    Class.forName()用法及与new区别
    反射
  • 原文地址:https://www.cnblogs.com/520yh/p/12894953.html
Copyright © 2020-2023  润新知