常用函数封装

// 求一个字符串的字节长度
function retByteslen(target){
    var count,
        len;
    count = len = target.length;
    for(var i=0;i<len;i++){
        if(target.charCodeAt(i) > 255){
            count++;
        }
    }
    console.log(count);
}

// 缓冲运动封装如下:从左往右运动,或从右往左运动均兼容
function startMove(dom, target) {
    clearInterval(dom.timer);
    var iSpeed = null;
    dom.timer = setInterval(function() {
        iSpeed = (target - dom.offsetLeft) / 7;
        iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
        if(dom.offsetLeft == target) {
            clearInterval(dom.timer);
        }else{
            dom.style.left = dom.offsetLeft + iSpeed + 'px';
        }                
    }, 30);
}

// 封装获取当前dom样式的函数
function getStyle(dom, attr){
    if(window.getComputedStyle){
        return window.getComputedStyle(dom, null)[attr];
    }else {
        return dom.currentStyle[attr];
    }
}

猜你喜欢

转载自www.cnblogs.com/zhizhi0810/p/10573992.html