輪播圖的展示效果是顯而易見:
HTML代碼如下
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <div id="container"> <div id="list" style="left: -600px;"> <img src="img/5.jpg" alt="1" /> <img src="img/1.jpg" alt="1" /> <img src="img/2.jpg" alt="2" /> <img src="img/3.jpg" alt="3" /> <img src="img/4.jpg" alt="4" /> <img src="img/5.jpg" alt="5" /> <img src="img/1.jpg" alt="5" /> </div> <div id="buttons"> <span index="1" class="on"></span> <span index="2"></span> <span index="3"></span> <span index="4"></span> <span index="5"></span> </div> <a href="javascript:;" id="prev" class="arrow"><</a> <a href="javascript:;" id="next" class="arrow">></a> </div> </body> </html>
疑問一:為什么用id?
方便獲取被操作的元素
疑問二:為什么輪播圖加類“on”?
為了方便操作,如果加了"on",即說明當前圖片正在輪播
疑問三:
<a href="javascript:;" id="prev" class="arrow"><</a>
//href="javascript:;" 是為了防止多次點擊,並且為了防止跳出鏈接
//id = prev 是為了獲取操作
//class = arrow 是屬於左右移動
//<是左括號 "<" ,屬於web安全符號
疑問四:為什么加了一個單獨的樣式,style = left: -600px;
<div id="list" style="left: -600px;"> <img src="img/5.jpg" alt="1" /> <img src="img/1.jpg" alt="1" /> <img src="img/2.jpg" alt="2" /> <img src="img/3.jpg" alt="3" /> <img src="img/4.jpg" alt="4" /> <img src="img/5.jpg" alt="5" /> <img src="img/1.jpg" alt="5" /> </div>
為了實現輪播圖偏移量。
CSS代碼如下
* { margin: 0; padding: 0; text-decoration: none; } body { padding: 20px; } #container { position: relative; width: 600px; height: 400px; border: 3px solid #333; overflow: hidden; } #list { position: absolute; z-index: 1; width: 4200px; height: 400px; } #list img { float: left; width: 600px; height: 400px; } #buttons { position: absolute; left: 250px; bottom: 20px; z-index: 2; height: 10px; } #buttons span { float: left; margin-right: 5px; width: 10px; height: 10px; border: 1px solid #fff; border-radius: 50%; background: #333; cursor: pointer; } #buttons .on { background: orangered; } .arrow { position: absolute; top: 180px; z-index: 2; display: none; width: 40px; height: 40px; font-size: 36px; font-weight: bold; line-height: 39px; text-align: center; color: #fff; background-color: RGBA(0, 0, 0, .3); cursor: pointer; } .arrow:hover { background-color: RGBA(0, 0, 0, .7); } #container:hover .arrow { display: block; } #prev { left: 20px; } #next { right: 20px; }
疑問一:談論一下絕對定位和相對定位?
position: relative 是相對定位
position: absolute是絕對定位
如果父元素沒有被賦予相對定位,那么子元素的絕對定位是基於網頁的!
疑問二:為什么用.7? 而不是0.7
.arrow:hover { background-color: RGBA(0, 0, 0, .7); }
為了方便書寫,可以用.7替代0.7。
疑問三:父元素用相對定位,子元素為什么一定要用絕對定位?
父元素:
#container { position: relative; width: 600px; height: 400px; border: 3px solid #333; overflow: hidden; }
子元素:
#list { position: absolute; z-index: 1; width: 4200px; height: 400px; }
如何區分父元素和子元素 , 即包括起來的,如下圖所示

所有元素的父元素就是 container
所有絕對定位都是相對於父元素 container
JS代碼如下
window.onload = function() { var container = document.getElementById('container'); var list = document.getElementById('list');
var imgLen= document.getElementTagName('img'); var buttons = document.getElementById('buttons').getElementsByTagName('span'); var prev = document.getElementById('prev'); var next = document.getElementById('next'); var index = 1; var timer; function animate(offset) { //獲取的是style.left,是相對左邊獲取距離,所以第一張圖后style.left都為負值, //且style.left獲取的是字符串,需要用parseInt()取整轉化為數字。 var newLeft = parseInt(list.style.left) + offset; list.style.left = newLeft + 'px'; //無限滾動判斷 if (newLeft > -600) { list.style.left = -3000 + 'px'; } if (newLeft < -3000) { list.style.left = -600 + 'px'; } } function play() { //重復執行的定時器 timer = setInterval(function() { next.onclick(); }, 2000) } function stop() { clearInterval(timer); } function buttonsShow() { //將之前的小圓點的樣式清除 for (var i = 0; i < buttons.length; i++) { if (buttons[i].className == "on") { buttons[i].className = ""; } } //數組從0開始,故index需要-1 buttons[index - 1].className = "on"; } prev.onclick = function() { index -= 1; if (index < 1) { index = 5 } buttonsShow(); animate(600); }; next.onclick = function() { //由於上邊定時器的作用,index會一直遞增下去,我們只有5個小圓點,所以需要做出判斷 index += 1; if (index > 5) { index = 1 } animate(-600); buttonsShow(); }; for (var i = 0; i < buttons.length; i++) { (function(i) { buttons[i].onclick = function() { /* 這里獲得鼠標移動到小圓點的位置,用this把index綁定到對象buttons[i]上,去谷歌this的用法 */ /* 由於這里的index是自定義屬性,需要用到getAttribute()這個DOM2級方法,去獲取自定義index的屬性*/ var clickIndex = parseInt(this.getAttribute('index')); var offset = 600 * (index - clickIndex); //這個index是當前圖片停留時的index animate(offset); index = clickIndex; //存放鼠標點擊后的位置,用於小圓點的正常顯示 buttonsShow(); } })(i) } container.onmouseover = stop; container.onmouseout = play; play(); }
疑問一:為什么用window.onload?
window.onload = function() { //是為了方便在圖片等必須的加載完以后再執行 }
疑問二:為什么不把功能合在一起呢?
奉行:單一職責
疑問三:為什么要首先獲取元素?
window.onload = function() { var container = document.getElementById('container'); var list = document.getElementById('list'); var buttons = document.getElementById('buttons').getElementsByTagName('span'); var prev = document.getElementById('prev'); var next = document.getElementById('next'); var index = 1; var timer; }
獲取完元素以后,才可以操作,所以你首先要想好要用到的。
疑問四:為什么那么多功能函數呢,用一個也可以的,不是嗎?
window.onload = function() { function animate(offset) { //動畫 } function play() { //開始 } function stop() { //結束 } function buttonsShow() { //顯示按鈕(小圓點) } }
單一職責,即功能單一,與其它功能不耦合。
function animate(offset) { //獲取的是style.left,是相對左邊獲取距離,所以第一張圖后style.left都為負值, //且style.left獲取的是字符串,需要用parseInt()取整轉化為數字。 var newLeft = parseInt(list.style.left) + offset; list.style.left = newLeft + 'px'; //無限滾動判斷 if (newLeft > -600) { list.style.left = -3000 + 'px'; } if (newLeft < -3000) { list.style.left = -600 + 'px'; } } function play() { //重復執行的定時器 timer = setInterval(function() { next.onclick(); }, 2000) } function stop() { clearInterval(timer); } function buttonsShow() { //將之前的小圓點的樣式清除 for (var i = 0; i < buttons.length; i++) { if (buttons[i].className == "on") { buttons[i].className = ""; } } //數組從0開始,故index需要-1 buttons[index - 1].className = "on"; }
疑問五:prev(上),next(下)
prev.onclick = function() { index -= 1; if (index < 1) { index = 5 } buttonsShow(); animate(600); }; next.onclick = function() { //由於上邊定時器的作用,index會一直遞增下去,我們只有5個小圓點,所以需要做出判斷 index += 1; if (index > 5) { index = 1 } animate(-600); buttonsShow(); };
點擊后圖片會向左移或者右移...
疑問四:閉包?
for (var i = 0; i < buttons.length; i++) { (function(i) { buttons[i].onclick = function() { /* 這里獲得鼠標移動到小圓點的位置,用this把index綁定到對象buttons[i]上,去谷歌this的用法 */ /* 由於這里的index是自定義屬性,需要用到getAttribute()這個DOM2級方法,去獲取自定義index的屬性*/ var clickIndex = parseInt(this.getAttribute('index')); var offset = 600 * (index - clickIndex); //這個index是當前圖片停留時的index animate(offset); index = clickIndex; //存放鼠標點擊后的位置,用於小圓點的正常顯示 buttonsShow(); } })(i) }
閉包格式:
(function(i) {
//這里放代碼 })(i)
為什么需要閉包?
為了不讓代碼被全局污染,和普通的代碼沒有什么區別。
疑問五:container.onmouseover = stop / container.onmouseout = play 什么意思?
container.onmouseover = stop;
container.onmouseout = play;
為了放在圖片上,輪播不再繼續,暫停在某張圖片上。
疑問六:
play();
為什么最后放了一個 play() ,play() 函數如果想要被執行,就必須調用!
優化
一.
IE9以下可以用 setInterval

IE9以上可以用 requestAnimactionFrame

你可以判斷瀏覽器內核再決定用setInterval 或 requestAnimactionFrame !
二.

這里可以修改一下,改成,再添加一個輪播圖更簡單

三.

這里可以用js改為動態的增加,根據圖片的數量

四.

任意圖片上傳,配合后台或者node進行自動重命名,實現拖入圖片自動添加標簽以及重命名圖片。
之后,大家有什么優化意見以及想法,可以提出來,大家交流交流。
