- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- JS中工廠(chǎng)模式的示例分析
這篇文章主要介紹了JS中工廠(chǎng)模式的示例分析,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著(zhù)大家一起了解一下。
基于JS工廠(chǎng)模式的H5應用,實(shí)現了輪播圖功能與滑屏功能,并且實(shí)現了文字大小的自適應功能,基于SASS樣式開(kāi)發(fā)。
核心的JS代碼如下:
index.js
define(function(){ var self = null, start = null, move = null, end = null, handle = null, timer = null, left = 0, x = 0, startX = 0, baseWidth = window.screen.width, // 移動(dòng)設備屏幕的寬度 baseSize = baseWidth * 0.2, // 滑動(dòng)切換閾值 banner = document.getElementById("banner"), // 獲取Banner center = document.getElementById("center"), // 獲取center ulList = document.getElementsByTagName("ul"), incBanner = document.getElementById('incBanner'), incCenter = document.getElementById('incCenter'); var app = { init : function(){ self = this; start = self.touchStart; move = self.touchMove; end = self.touchEnd; handle = self.addHandler; self.pageInit(); self.bindTouch(banner, start, move, end); self.bindTouch(center, start, move, end); self.shiftBanner(banner); }, pageInit : function(){ for (var i=0; i < ulList.length; i++) { ulList[i].style.left = 0 + 'px'; ulList[i].style.width = 3 * baseWidth + 'px'; } }, bindTouch : function(elem, handler1, handler2, handler3){ handle(elem, "touchstart", handler1); handle(elem, "touchmove", handler2); handle(elem, "touchend", handler3); }, touchStart : function(event){ var touch = event.touches[0]; left = parseInt(this.style.left); x = 0; startX = 0; startX = touch.pageX; //剛觸摸時(shí)的坐標 if(this == banner){ timer = clearInterval(timer); } }, touchMove : function(event){ //滑動(dòng)過(guò)程 var touch = event.touches[0]; x = startX - touch.pageX; //滑動(dòng)的距離 this.style.left = left - x + 'px'; }, touchEnd : function(event){ //手指離開(kāi)屏幕 self.move(this); self.moveAdjust(this); self.indicate(this); if(this == banner){ self.shiftBanner(banner); } }, move : function(elem){ var leftTmp = left; //緩存touchMove結束時(shí)的滑動(dòng)位置 elem.style.left = left; if (x > baseSize) { elem.style.left = left - baseWidth + 'px'; } else if (x < -baseSize) { elem.style.left = left + baseWidth + 'px'; } else { elem.style.left = leftTmp + 'px'; } }, moveAdjust : function(elem){ left = parseInt(elem.style.left) if (left < -baseWidth * 2) { left = -baseWidth * 2; elem.style.left = left + 'px'; } if (left > 0) { left = 0; elem.style.left = left + 'px'; } x = 0; }, indicate : function(elem){ if (elem == banner) { self.activeClass(incBanner); }else if (elem == center) { self.activeClass(incCenter); } }, activeClass : function(elem){ var len = elem.children.length; for (var i = 0; i < len; i++) { elem.children[i].className=" "; } if (left == 0) { elem.children[0].className = "active"; } else if (left == (-baseWidth)) { elem.children[1].className = "active"; }else if (left == (-2*baseWidth)) { elem.children[2].className = "active"; } }, shiftBanner : function(elem){ var t = new Date(); var tmpLeft = parseInt(elem.style.left); timer = setInterval(function(){ if ((tmpLeft == 0) || (tmpLeft == -baseWidth)) { elem.style.left = tmpLeft - baseWidth + 'px'; } else if (tmpLeft == -2*baseWidth) { elem.style.left = 0 + 'px'; } tmpLeft = parseInt(elem.style.left); left = tmpLeft; // console.log(elem.style.left); // console.log(t); self.indicate(banner); },4000); }, addHandler : function(element, type, handler){ if (element.addEventListener) { element.addEventListener(type, handler, true); } else if (element.attachEvent) { element.attachEvent("on" + type, handler); } else { element["on" + type] = handler; } } }; return { init : function(){ app.init(); } }; });
可以在瀏覽器中打開(kāi): https://iove1123.github.io/policy
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站