move.js

作者: 若榴花开 | 来源:发表于2018-03-12 10:45 被阅读0次
            function getStyle(obj,attr) {
                return obj.currentStyle ? obj.currentStyle[attr] : getComputedStyle(obj)[attr];
            }
            function startMove(obj,attr,target) {
                clearInterval(obj.timer);
                obj.timer = setInterval(function () {
                    // 返回的字符串,需转换成number类型
                    // var curAttr = parseInt(getStyle(obj,attr));
                    // var curAttr = (attr == 'opacity') ? parseFloat(getStyle(obj,attr))*100 : parseInt(getStyle(obj,attr));
                    var curAttr = (attr == 'opacity') ? parseInt(parseFloat(getStyle(obj,attr))*100) : parseInt(getStyle(obj,attr));
                    var speed = (target - curAttr)/8;
                    speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
                    if (curAttr == target) {    //是否到达终点
                        clearInterval(obj.timer);
                    } else {
                        if (attr == 'opacity') {
                            curAttr += speed;
                            obj.style.opacity = (curAttr + speed)/100;
                            obj.style.filter = 'alpha(opacity:' + (curAttr + speed) + ')';
                            document.getElementById('txt').value = obj.style.opacity;
                        } else {
                            obj.style[attr] = curAttr + speed + 'px';
                        }
                    }
                },300);
            }

    相关文章

      网友评论

          本文标题:move.js

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