面向對象編程---圖片輪播

来源:http://www.cnblogs.com/starryqian/archive/2017/06/07/6959378.html
-Advertisement-
Play Games

今天複習了一下麵向對象編程,下麵是我的效果圖 看著這個圖片你會覺得這沒有什麼,那麼上代碼: html部分: 必要的css代碼 js部分: 輪播的js 希望能幫到大家,還有就是這個不是基於jQuery的,我自己封裝了一個簡單的方法。 以後我們圖片輪播可以使用 這樣使用於比較多的輪播組件的網站,當然我們 ...


今天複習了一下麵向對象編程,下麵是我的效果圖

看著這個圖片你會覺得這沒有什麼,那麼上代碼:

html部分:

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Document</title>
 6     <style>
 7         ul,ol,li{padding:0;margin:0;list-style: none;}
 8         .container1{
 9             width: 600px;
10             height: 400px;
11             margin:50px auto;
12             position: relative;
13         }
14 
15         .container2{
16             width: 500px;
17             height: 300px;
18             margin:50px auto;
19             position: relative;
20         }
21         
22     </style>
23     <link rel="stylesheet" href="slider.css">
24     <script src="Tq.js"></script>
25     <script src="sliderIIFE.js"></script>
26 </head>
27 <body>
28     <div class="container1 slider" id="slide1">
29         <ul>
30             <li><a href=""><img src="img/1.jpg" alt="2016年6月 英國出走歐盟"></a></li>
31             <li><a href=""><img src="img/2.jpg" alt="2016年11月 特朗普當選美國總統"></a></li>
32             <li><a href=""><img src="img/1.jpg" alt="2016年6月 英國出走歐盟"></a></li>
33 
34             <li><a href=""><img src="img/3.jpg" alt="雙11"></a></li>
35             <li><a href=""><img src="img/1.jpg" alt="2016年6月 英國出走歐盟"></a></li>
36 
37             <li><a href=""><img src="img/4.jpg" alt="G20峰會"></a></li>
38         </ul>
39     </div>
40 
41 
42     <div class="container2 slider" id="slide2">
43         <ul>
44             <li><a href=""><img src="images/1.png" alt="2016年6月 英國出走歐盟"></a></li>
45             <li><a href=""><img src="images/2.png" alt="2016年11月 特朗普當選美國總統"></a></li>
46             <li><a href=""><img src="images/3.png" alt="雙11"></a></li>
47 
48         </ul>
49     </div>
50 
51 
52     <script>
53 
54         var s1 = new Slider("slide1",{startIndex:0,speed:50});
55         var s2 = new Slider("slide2",{startIndex:2});
56         console.info(s1);
57         console.info(s2);
58 
59     </script>
60 </body>
61 </html>

必要的css代碼

.slider ul li a{display: block;width: 100%;height: 100%;}
.slider ul,.slider ul li,.slider ul img{width: 100%;height: 100%;}


.slider ol{position: absolute;bottom:10px;background: rgba(255,255,255,.5);height: 20px;border-radius: 5px;right:20px;z-index:999;}
.slider ol li{margin:0 5px;width:10px;height:10px;border-radius:5px;display: inline-block;line-height:10px;background: red;font-size:0;vertical-align: middle;position:relative;top:-3px;}
.slider .mask{
	position: absolute;
	bottom:10px;
	width: 100%;
	height: 50px;
	line-height: 50px;
	text-align: center;
	background: rgba(0,0,0,.5);
	color:#fff;
}
.slider span{
	width:50px;
	height:50px;
	border-radius : 25px;
	position : absolute;
	top : 50%;
	transform: translateY(-50%);
	transition: background-color .5s;
	background-color: rgba(255,255,255,0);
}
.slider span.btnright{
	right:10px;
}
.slider span.btnleft{
	left:10px;
}
.slider span:hover{
	background-color: rgba(255,255,255,1);
}
.slider span.btnleft:after,span.btnright:after{
	transition: transform .5s;
	position: absolute;
	top:24px;
	left:15px;
	transform-origin: left top;
	transform: rotate(30deg);
	content: "";display: block;width: 20px;height: 2px;background-color: red;
}
.slider span.btnright:after{
	left:auto;
	right:15px;
	transform-origin: right top;

}
.slider .btnleft:before,.btnright:before{
	transition: transform .5s;
	position: absolute;
	top:24px;
	left:15px;
	transform-origin: left top;
	transform: rotate(-30deg);
	content: "";display: block;width: 20px;height: 2px;background-color: red;
}
.slider .btnright:before{
	transform-origin: right top;

	left: auto;
	right:15px;
	transform: rotate(-30deg);
}
.slider .btnleft:hover:after{
	transform: rotate(45deg);
}
.slider .btnleft:hover:before{
	transform: rotate(-45deg);
}
.slider .mask{
	position:absolute;
	bottom:0;
	height:50px;
	line-height:50px;
	color:#fff;
	text-align: left;
	text-indent: 2em;
	background-color:rgba(0,0,0,.5);
}

  js部分:

function $(id){
	return document.getElementById(id);
}

/**
 * 2.0
 * 在指定的容器中去找某一個TagName的html標簽集合
 * containerId 可以是一個字元串,也可以是一個 dom對象。
 * @param  {[type]} containerId [description]
 * @param  {[type]} tagName     [description]
 * @return {[type]}             [description]
 * 
 */
function $get(containerId,tagName){
	if(typeof containerId =="string" && $(containerId)){
		return $(containerId).getElementsByTagName(tagName);
	}
	else if(typeof containerId=="object")
	{
		return containerId.getElementsByTagName(tagName);
	}
	else{
		throw ("你寫的第一個參數不是一個ID");
	}
}

//<span id="span" className="leftBtn" stlye="width:100px;height:100px;">按鈕</span>
function $create(tagName,attr,style){
	var dom = document.createElement(tagName);
	for(var pName in attr){
		dom[pName] = attr[pName];
	}

	for(var pName in style){
		dom.style[pName] = style[pName];
	}
	return dom;
}
function info(obj){
	console.info(obj);
}

function getH(obj){
	//debugger;
	if(obj.currentStyle){
		getH = function(obj){
			return parseInt ( obj.currentStyle.height );
		}
	}else{
		getH=function(obj){
			return parseInt ( window.getComputedStyle(obj, null).height );
		}
	}
	getH(obj) ;


}

//對對象進行拓展
function extend(defaultConf,userConf){
	//對於在defaultConf這個對象中的每一個屬性,
	//如果這個屬性名在userConf中的也存在,則使用userConf中的那個屬性值

	for(var pName in defaultConf){
		if(userConf.hasOwnProperty(pName)){
			defaultConf[pName] = userConf[pName];
		}
	}
}

  輪播的js

  1 ;(function(window){
  2 
  3     //有問題,請自行改正!!!! 參考slider.js
  4     var defaultConf = {
  5         "isAuto":true,
  6         "speed":"normal",
  7         "startIndex":0,
  8         "isClickable":true
  9     };
 10 
 11     var speedList={
 12         "faster":2000,
 13         "slower":4000,
 14         "normal":3000
 15     };
 16 
 17     function Slider(containerId,conf){
 18         
 19         // if(conf)
 20         //     extend(defaultConf,conf); //這一句後,defaultConf這個對象中就會包含用戶的設置
 21 
 22         conf && extend(defaultConf,conf);
 23 
 24         this.container = $(containerId);
 25         this.slideList = $get($get(this.container,"ul")[0],"li");//
 26         //this.indexList = $get($get(this.container,"ol")[0],"li");//數字指示條
 27         this.num = this.slideList.length;
 28         this.indexList = createIndexList.call(this);//數字指示條
 29 
 30         this.leftBtn = createLinkBtn.call(this,"left"); //創建按鈕
 31         this.rightBtn = createLinkBtn.call(this,"right"); //創建按鈕
 32     
 33         this.txtDiv = createTxtDiv.call(this);//創建文字說明的區域
 34         
 35         this.currentIndex = defaultConf.startIndex;        //當前整個輪播圖中顯示的 第幾 張
 36 
 37         init.call(this,this.currentIndex);                //初始化 把其它的li都隱藏,把第一個顯示出來
 38 
 39         this.timer ;    
 40 
 41         if(typeof defaultConf.speed =="string")
 42             this.speed = speedList[defaultConf.speed] || 2000;
 43         else if (typeof defaultConf.speed =="number") {
 44             this.speed = defaultConf.speed;
 45         }
 46         if( defaultConf.isAuto )
 47             this.auto();
 48 
 49         bindEvent.call(this);           //綁定事件
 50     }
 51 
 52     var createTxtDiv = function (){
 53 
 54         console.info(this);
 55 
 56         //1.創建div,設置基本屬性及樣式
 57         var div = $create("div",{className:"mask"},{})
 58         div.innerHTML = "";
 59         //2.添加到容器中
 60         this.container.appendChild(div);
 61         //3.返回
 62         return div;
 63     }
 64 
 65     Slider.prototype.auto = function(){
 66         var that = this;
 67         this.timer = setInterval(function(){
 68             that.rightBtn.onclick();
 69             //that.rightBtn.click();
 70 
 71         },that.speed);
 72 
 73         console.info("定時器"+this.timer+"開動");
 74     }
 75     var createLinkBtn = function(dir){
 76         //1.創建一個dom元素 設置基本的屬性,同時設置樣式。
 77         var span = $create("span",{className:"btn"+dir},{
 78             
 79         });
 80         
 81         //3.把它添加到容器中
 82         this.container.appendChild(span);
 83         //4.返回
 84         
 85         return span;
 86     }
 87     var createIndexList = function(){    //創建ol li結構
 88         //ol>li
 89         var ol = $create("ol"); //創建ol
 90 
 91         var lis=[];                //保存所有的ol中的li
 92         for (var i = 0; i < this.num; i++) {
 93             var li = $create("li");
 94             ol.appendChild(li);
 95             lis.push(li);        //加到數組中
 96         }
 97         this.container.appendChild(ol);
 98         return lis;
 99     }
100 
101     Slider.prototype.moveto = function(indexabc){
102         for(var i = 0; i< this.num; i++){
103             this.slideList[i].style.display = "none";
104             this.indexList[i].style.backgroundColor="red";
105         }
106         this.slideList[indexabc].style.display = "block";
107         this.indexList[indexabc].style.backgroundColor="blue";
108 
109         //更新當前的索引
110         this.currentIndex = indexabc;
111 
112         //更新文字說明
113         var txt = $get(this.slideList[indexabc],"img")[0].alt ;
114         this.txtDiv.innerHTML = txt;
115     //    console.info("當前是:"+this.currentIndex);
116     }
117     var bindEvent = function(){
118         var that = this; /*var 能不能省*/
119         //綁定指示條的事件
120         
121         if( defaultConf.isClickable){
122             for (var i = 0; i < this.num; i++) {
123                 this.indexList[i].index = i;
124                 this.indexList[i].onmouseenter = function(){
125                     that.moveto(this.index);
126                 }
127             } 
128         }
129         
130         this.rightBtn.onclick = function(){
131             //向右,顯示下一張 當前的索引值要+1
132             var i = that.currentIndex + 1;
133             // i = (i== that.num) ? 0 : i; 
134             i = i % that.num;
135             that.moveto(i);
136         }
137 
138         //綁定按鈕的事件
139         this.leftBtn.onclick = function(){
140             //btnLeft 向左,顯示上一張
141             var i = that.currentIndex - 1;  //上一張的編號
142             //如果它等於-1,應該要顯示最後一張,最後一張的索引值是that.num-1。
143             //i = (i==-1) ? that.num-1 : i;
144             i = (i+that.num) % that.num;
145             //i = i < 0 ? that.num+i : i;
146             that.moveto(i);
147         }
148         if(defaultConf.isAuto) {
149             this.container.onmouseenter = function(){
150                 // console.info("stop");
151                 clearInterval(that.timer);
152 
153             }
154             this.container.onmouseleave = function(){
155                 // console.info("go on");
156 
157                 that.auto();
158             }
159         }
160     }
161     var init = function(yourlikeIndex){
162         if( yourlikeIndex)
163             this.moveto(yourlikeIndex);
164         else
165             this.moveto(0);
166     }
167 
168     window.Slider = Slider;
169 
170 })(window);

希望能幫到大家,還有就是這個不是基於jQuery的,我自己封裝了一個簡單的方法。

以後我們圖片輪播可以使用

 1 <div class="container1 slider" id="slide1">
 2         <ul>
 3             <li><a href=""><img src="img/1.jpg" alt="2016年6月 英國出走歐盟"></a></li>
 4             <li><a href=""><img src="img/2.jpg" alt="2016年11月 特朗普當選美國總統"></a></li>
 5             <li><a href=""><img src="img/1.jpg" alt="2016年6月 英國出走歐盟"></a></li>
 6 
 7             <li><a href=""><img src="img/3.jpg" alt="雙11"></a></li>
 8             <li><a href=""><img src="img/1.jpg" alt="2016年6月 英國出走歐盟"></a></li>
 9 
10             <li><a href=""><img src="img/4.jpg" alt="G20峰會"></a></li>
11         </ul>
12     </div>
13 <script>
14 
15         var s1 = new Slider("slide1",{startIndex:0,speed:50});
16         
17 
18     </script>

這樣使用於比較多的輪播組件的網站,當然我們這個方法的js可以進一步去封裝的,使它更強大,如果這裡你最後再壓縮一下,變數名再簡單。那麼效果會更好的。


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 在項目中,經常會用到ajax,比如實現局部刷新,比如需要前後端交互等,這裡呢分享局部刷新的兩種方法,主要用的是ajax裡面的.load()。 第一種: 當某幾個頁面都有相同的頭部、導航、底部的時候,點擊導航鏈接可以在幾個頁面中切換,此時想要的效果是點擊鏈接後只切換內容部分,其他不再重新載入。上代碼。 ...
  • [1]元素相關 [2]數據相關 [3]類型檢測 [4]數組相關 [5]其他 ...
  • 在JavaScript中,函數是作為一等成員而存在的,由此,非常有必要掌握JavaScript中函數的知識,最近幾天閱讀了JavaScript忍者的第三章和第四章前面的部分,做一個總結。 JavaScript函數聲明: JavaScript函數是使用 函數字面量 進行聲明 從而創建函數的。 形如 函 ...
  • [1]數據操作 [2]隊列操作 [3]集合操作 [4]索引過濾 [5]內容過濾 ...
  • 概念 webpack是一個現代javascript應用程式的模塊打包器。 當webpack處理你的應用程式時,它會遞歸構建一個依賴圖(包含了你的應用程式所需要每個模塊),然後把這些模塊打包到少數幾個budle文件中(通常是只有一個,會被瀏覽器載入,根據項目情況而定)。 這是令人難以置信的配置,但開始 ...
  • 第一次學習Node.js爬蟲,所以這時一個簡單的爬蟲,Node.js的好處就是可以併發的執行 這個爬蟲主要就是獲取慕課網的課程信息,並把獲得的信息存儲到一個文件中,其中要用到cheerio庫,它可以讓我們方便的操作HTML,就像是用jQ一樣 開始前,記得 為了能夠併發的進行爬取,用到了Promise ...
  • v model的神奇 html js 一旦我們輸入的值發生變化,data中的poin值也會發生變化。 理論上data中的值發生變化是會出發事件的,但是我們沒看見? 其實在vue的文檔中有說明: 是下麵的語法糖 每次我們輸入的時候觸發了 事件,input綁定了內聯函數,從而改變了 的值。 你好奇inp ...
  • 層疊性 所謂層疊性是指多種CSS樣式的疊加,也就是說後面設置的樣式會層疊(覆蓋)之前的樣式,層疊性的前提是CSS的選擇器的優先順序相同,例如,當使用內嵌式CSS樣式表定義<p>標記字型大小大小為12像素,鏈入式定義<p>標記顏色為紅色,那麼段落文本將顯示為12像素紅色,即這兩種樣式產生了疊加。 瀏覽器顯示 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...