ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
## 伪数组正常化 ``` //伪数组 var ss = {0: 'Marvin', 1: 'lili'} Array.prototype.slice.call(ss) => [Marvin , lili] ``` ***** ***** ## 数组最大值 ``` function getMax(arr){ var arrLen=arr.length; for(var i=0,ret=arr[0];i<arrLen;i++){ ret=Math.max(ret,arr[i]); } return ret; } ``` ***** ***** ## 数组合并 ``` var array1 = [1 , 2 , 3, 5]; var array2 = ["xie" , "li" , "qun" , "tsrot"]; Array.prototype.push.apply(array1, array2); ``` ***** ***** ## 闭包权限收敛 ``` function isFirstLoad(){ var _list=[]; return function(id){ if(_list.indexOf(id)>=0){ return false; }else{ _list.push(id); return true; } } } ``` ## 轮播 ``` /*轮播*/ var AutoPlay = function (id) {this.initialize(id)}; AutoPlay.prototype = { initialize: function (id) { var oThis = this; this.oBox = $(id); this.oUl = $$("ul", this.oBox)[0]; this.aImg = $$("img", this.oBox); this.timer = null; this.autoTimer = null; this.iNow = 0; this.creatBtn(); this.aBtn = $$("li", this.oCount); this.toggle(); this.autoTimer = setInterval(function () { oThis.next() }, 3000); this.oBox.onmouseover = function () { clearInterval(oThis.autoTimer) }; this.oBox.onmouseout = function () { oThis.autoTimer = setInterval(function () { oThis.next() }, 3000) }; for (var i = 0; i < this.aBtn.length; i++) { this.aBtn[i].index = i; this.aBtn[i].onmouseover = function () { oThis.iNow = this.index; oThis.toggle() } } }, creatBtn: function () { this.oCount = document.createElement("ul"); this.oFrag = document.createDocumentFragment(); this.oCount.className = "count"; for (var i = 0; i < this.aImg.length; i++) { var oLi = document.createElement("li"); oLi.innerHTML = i + 1; this.oFrag.appendChild(oLi) } this.oCount.appendChild(this.oFrag); this.oBox.appendChild(this.oCount) }, toggle: function () { for (var i = 0; i < this.aBtn.length; i++) this.aBtn[i].className = ""; this.aBtn[this.iNow].className = "current"; this.doMove(-(this.iNow * this.aImg[0].offsetHeight)) }, next: function () { this.iNow++; this.iNow == this.aBtn.length && (this.iNow = 0); this.toggle() }, doMove: function (iTarget) { var oThis = this; clearInterval(oThis.timer); oThis.timer = setInterval(function () { var iSpeed = (iTarget - oThis.oUl.offsetTop) / 5; iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed); oThis.oUl.offsetTop == iTarget ? clearInterval(oThis.timer) : (oThis.oUl.style.top = oThis.oUl.offsetTop + iSpeed + "px") }, 30) } }; //面向对象版运动框架 var Animate = function (oElement, options, callback) {this.initialize.apply(this, arguments)}; Animate.prototype = { initialize: function (oElement, options, callback) { var oThis = this; this.options = options; this.callback = callback; this.oElement = typeof oElement === "string" ? document.getElementById(oElement) : oElement; clearInterval(this.timer); this.timer = setInterval(function () { oThis.doMove() }, 30) }, css: function (attr, value) { if (arguments.length == 1) { return parseFloat(this.oElement.currentStyle ? this.oElement.currentStyle[attr] : getComputedStyle(this.oElement, null)[attr]) } else if (arguments.length == 2) { attr == "opacity" ? (this.oElement.style.filter = "alpha(opacity=" + value + ")", this.oElement.style.opacity = value / 100) : this.oElement.style[attr] = value + "px" } }, doMove: function () { var opt = this.options; var bComplete = true; for (var p in opt) { var iCur = p == "opacity" ? parseInt(this.css(p).toFixed(2) * 100) : this.css(p); var iSpeed = (opt[p] - iCur) / 5; iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed); opt[p] == iCur || (bComplete = false, this.css(p, iCur + iSpeed)) } bComplete && (clearInterval(this.timer), this.callback && this.callback.call(this)) } }; ```