1.模仿fade in(),fade out().
原理:setInterval ( "opacity++透明度「函數,時間間隔)
var alpha = 0; function play(){ timer = setInterval(function(){ alpha += 2; alpha > 100 && (alpha = 100); aImg[index].style.opacity = alpha / 100; aImg[index].style.filter = "alpha(opacity = " + alpha/100 + ")"; alpha == 100 && clearInterval(timer); },40) }
2.取得並設定元素物件的屬性值:
關鍵點:obj.currentStyle[attr] ; getComputedStyle(obj,null)[attr] ;
function css(obj,attr,val) { switch(arguments.length) { case 2: if(typeof arguments[1] == "string"){ return obj.currentStyle ? obj.currentStyle[attr] : getComputedStyle(obj,null)[attr]; }else{ for(var i in attr) { obj.style[i] = attr[i]; } } break; case 3: obj.style[attr] = val; break; default: alert("错误参数"); } }
以上所述是小編跟大家介紹的JS功能代碼集錦的相關內容,希望對大家有幫助!