各種Js封裝

來源:互聯網
上載者:User

標籤:gets   return   cti   client   距離   style   運動   filter   else   

擷取ClassName元素

function getClass(classname,id){    if(document.getElementsByClassName){        if(id){            return $(id).getElementsByClassName(classname);        }else{            return document.getElementsByClassName(classname);        }        if(id){            var allHtml = $(id).getElementsByTagName("*");        }else{            var allHtml = document.getElementsByTagName("*");        }        var arr = [];        for(var i = 0; i < allHtml.length; i++){            var allClass = allHtml[i].className.split(" ");            for(var j = 0; j < allClass.length; j++){                if(allClass[j] == classname){                    arr.push(allHtml[i]);                }            }        }        return arr;    }}

 

 

相容IE、Firefox、chrome的可視地區封裝

function client(){if(window.innerWidth){return {width:window.innerWidth,height:window.innerHeight}}else if(document.compatMode == "CSS1Compat"){return {width:document.documentElement.clientWidth;height:document.documentElement.clientHeight;}}else{return {width:document.body.clientWidth;height:document.body.clientHeight;}}}

 

封裝緩速運動架構(多個屬性)

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(){var flag = true;for(var attr in json){var current = 0;if("opacity" == attr){current = parseInt(getStyle(obj,attr)*100) || 0;}else{current = parseInt(getStyle(obj,attr));}var step = (json[attr] - current) / 10;step = step > 0 ? Math.ceil(step) : Math.floor(step);if("opacity" == attr){if("opacity" in obj.style){obj.style[attr] = (current + step) / 100;}else{obj.style.filter = "alpha(opacity =" + (current + step)*10 + ")"}}else if("zindex" == attr){obj.style[attr] = json[attr];}else{obj.style[attr] = current + step + "px";}if(current != json[attr]){flag = false;}}if(flag){clearInterval(obj.timer);if(fn){fn();}}},30)}

 

勻速動畫架構

function animate(obj,target,speed){    clearInterval(obj.timer);    //判斷box的距離使box是前走還是後退    var stepSize = obj.offsetLeft < target ? speed : -speed;     obj.timer = setInterval(function(){    var result = target - obj.offsetLeft;        obj.style.left = obj.offsetLeft + stepSize + "px";        if(Math.abs(result) <= speed){        clearInterval(obj.timer);        obj.style.left = target + "px";        }    },30)}

  

  

各種Js封裝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.