JS工廠模式開發(fā)實(shí)踐案例分析
本文實(shí)例講述了JS工廠模式開發(fā)。分享給大家供大家參考,具體如下:
基于JS工廠模式的H5應(yīng)用,實(shí)現(xiàn)了輪播圖功能與滑屏功能,并且實(shí)現(xiàn)了文字大小的自適應(yīng)功能,基于SASS樣式開發(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)設(shè)備屏幕的寬度
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í)的坐標(biāo)
if(this == banner){
timer = clearInterval(timer);
}
},
touchMove : function(event){ //滑動(dòng)過程
var touch = event.touches[0];
x = startX - touch.pageX; //滑動(dòng)的距離
this.style.left = left - x + 'px';
},
touchEnd : function(event){ //手指離開屏幕
self.move(this);
self.moveAdjust(this);
self.indicate(this);
if(this == banner){
self.shiftBanner(banner);
}
},
move : function(elem){
var leftTmp = left; //緩存touchMove結(jié)束時(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();
}
};
});
可以在瀏覽器中打開: https://iove1123.github.io/policy

項(xiàng)目源碼見GitHub:https://github.com/iove1123/policy
更多關(guān)于JavaScript相關(guān)內(nèi)容還可查看本站專題:《javascript面向?qū)ο笕腴T教程》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
- JS面向?qū)ο蠡A(chǔ)講解(工廠模式、構(gòu)造函數(shù)模式、原型模式、混合模式、動(dòng)態(tài)原型模式)
- JavaScript 模式之工廠模式(Factory)應(yīng)用介紹
- javascript 模式設(shè)計(jì)之工廠模式學(xué)習(xí)心得
- js簡單工廠模式用法實(shí)例
- 淺析JS抽象工廠模式
- JavaScript設(shè)計(jì)模式之工廠模式和構(gòu)造器模式
- Javascript設(shè)計(jì)模式理論與編程實(shí)戰(zhàn)之簡單工廠模式
- javascript抽象工廠模式詳細(xì)說明
- JavaWeb實(shí)現(xiàn)用戶登錄注冊(cè)功能實(shí)例代碼(基于Servlet+JSP+JavaBean模式)
- js面向?qū)ο笾R妱?chuàng)建對(duì)象的幾種方式(工廠模式、構(gòu)造函數(shù)模式、原型模式)
- js正則表達(dá)式中的單行模式與多行模式實(shí)例分析
相關(guān)文章
JS基于onclick事件實(shí)現(xiàn)單個(gè)按鈕的編輯與保存功能示例
這篇文章主要介紹了JS基于onclick事件實(shí)現(xiàn)單個(gè)按鈕的編輯與保存功能,結(jié)合實(shí)例形式分析了JS實(shí)現(xiàn)onclick響應(yīng)事件的轉(zhuǎn)換相關(guān)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02
點(diǎn)擊按鈕或鏈接不跳轉(zhuǎn)只刷新頁面的腳本整理
點(diǎn)擊按鈕或鏈接時(shí)不跳轉(zhuǎn)只刷新頁面,在某些情況下還是比較實(shí)用的,下面整理些不錯(cuò)的示例,感興趣的朋友可以參考下2013-10-10
javascript?ES6中set集合、map集合使用方法詳解與源碼實(shí)例
這篇文章主要介紹了javascript?ES6中set、map使用方法詳解與源碼實(shí)例,需要的朋友可以參考下2022-12-12
JavaScript使用簡單正則表達(dá)式的數(shù)據(jù)驗(yàn)證功能示例
這篇文章主要介紹了JavaScript使用簡單正則表達(dá)式的數(shù)據(jù)驗(yàn)證功能,結(jié)合實(shí)例形式分析了JS針對(duì)表單輸入內(nèi)容的簡單正則驗(yàn)證操作技巧,需要的朋友可以參考下2017-01-01
JS獲取地址欄參數(shù)的兩種方法(簡單實(shí)用)
這篇文章主要介紹了JS獲取地址欄參數(shù)的兩種方法(簡單實(shí)用),小編給大家推薦使用第一種采用正則表達(dá)式獲取地址欄參數(shù)的方法,此方法簡單實(shí)用,對(duì)js獲取地址欄參數(shù)相關(guān)知識(shí)感興趣的朋友,一起學(xué)習(xí)吧2016-06-06
JS判斷字符串變量是否含有某個(gè)字串的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄狫S判斷字符串變量是否含有某個(gè)字串的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-06-06
php register_shutdown_function函數(shù)詳解
register_shutdown_function() 函數(shù)可實(shí)現(xiàn)當(dāng)程序執(zhí)行完成后執(zhí)行的函數(shù),其功能為可實(shí)現(xiàn)程序執(zhí)行完成的后續(xù)操作,需要的朋友可以參考下2017-07-07
基于javascript實(shí)現(xiàn)頁面加載loading效果
這篇文章主要為大家詳細(xì)介紹了基于javascript實(shí)現(xiàn)頁面加載loading效果的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-03-03
JS實(shí)現(xiàn)查找數(shù)組中對(duì)象的屬性值是否存在示例
這篇文章主要介紹了JS實(shí)現(xiàn)查找數(shù)組中對(duì)象的屬性值是否存在,涉及javascript針對(duì)json數(shù)組的遍歷、查找相關(guān)操作技巧,需要的朋友可以參考下2019-05-05
js統(tǒng)計(jì)錄入文本框中字符的個(gè)數(shù)并加以限制不超過多少
為了更直觀的體現(xiàn)用戶在文本框輸入文本時(shí)能看到自己輸入了多少字,并且有些特殊的要求字?jǐn)?shù)不超過多少,本文給出了具體的實(shí)現(xiàn)2014-05-05

