欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

簡(jiǎn)單的JS輪播圖代碼

 更新時(shí)間:2016年07月18日 08:56:18   作者:劉彥佐  
這篇文章主要介紹了簡(jiǎn)單的JS輪播圖實(shí)現(xiàn)方法,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下

在團(tuán)隊(duì)帶人,突然被人問(wèn)到輪播圖如何實(shí)現(xiàn),進(jìn)入前端領(lǐng)域有一年多了,但很久沒(méi)自己寫(xiě)過(guò),一直是用大牛寫(xiě)的插件,今天就寫(xiě)個(gè)簡(jiǎn)單的適合入門者學(xué)習(xí)的小教程。當(dāng)然,輪播圖的實(shí)現(xiàn)原理與設(shè)計(jì)模式有很多種,我這里講的是用面向過(guò)程函數(shù)式編程去實(shí)現(xiàn),相對(duì)于面向?qū)ο笤O(shè)計(jì)模式,代碼難免會(huì)顯得臃腫冗余。但沒(méi)有面向?qū)ο蟮某橄髤s很適合新手理解與學(xué)習(xí)。已經(jīng)在BAT的同學(xué)看到希望少噴點(diǎn)。另外可以多提意見(jiàn)。

輪播圖的原理:

一系列的大小相等的圖片平鋪,利用CSS布局只顯示一張圖片,其余隱藏。通過(guò)計(jì)算偏移量利用定時(shí)器實(shí)現(xiàn)自動(dòng)播放,或通過(guò)手動(dòng)點(diǎn)擊事件切換圖片。

Html布局

首先父容器container存放所有內(nèi)容,子容器list存在圖片。子容器buttons存放按鈕小圓點(diǎn)。

<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">&lt;</a>
    <a href="javascript:;" id="next" class="arrow">&gt;</a>
  </div>

優(yōu)化,無(wú)縫滾動(dòng)。

當(dāng)你從最后一張圖切換回第一張圖時(shí),有很大空白,利用兩張輔助圖來(lái)填補(bǔ)這個(gè)空白。

這里補(bǔ)充下無(wú)縫滾動(dòng),直接看代碼,復(fù)制最后一張圖片放置第一張圖片前,同時(shí)復(fù)制第一張圖片放置最后一張圖片的后面。并且,將第一張圖片輔助圖(實(shí)際上是實(shí)際顯示的第5張圖片隱藏起來(lái),故設(shè)置style="left: -600px;")

CSS修飾

1、對(duì)盒子模型,文檔流的理解,絕對(duì)定位問(wèn)題。

2、注意list的overflow:hidden;只顯示窗口的一張圖片,把左右兩邊的都隱藏起來(lái)。

3、確保buttons中每個(gè)span所在層置頂,將其設(shè)置為最頂端。(z-index:999)

* {
margin: 0;
padding: 0;
text-decoration: none;
}
body {
padding: 20px;
}
#container {
width: 600px;
height: 400px;
border: 3px solid #333;
overflow: hidden;
position: relative;
}
#list {
width: 4200px;
height: 400px;
position: absolute;
z-index: 1;
}
#list img {
width: 600px;
height: 400px;
float: left;
}
#buttons {
position: absolute;
height: 10px;
width: 100px;
z-index: 2;
bottom: 20px;
left: 250px;
}
#buttons span {
cursor: pointer;
float: left;
border: 1px solid #fff;
width: 10px;
height: 10px;
border-radius: 50%;
background: #333;
margin-right: 5px;
}
#buttons .on {
background: orangered;
}
.arrow {
cursor: pointer;
display: none;
line-height: 39px;
text-align: center;
font-size: 36px;
font-weight: bold;
width: 40px;
height: 40px;
position: absolute;
z-index: 2;
top: 180px;
background-color: RGBA(0, 0, 0, .3);
color: #fff;
}
.arrow:hover {
background-color: RGBA(0, 0, 0, .7);
}
#container:hover .arrow {
display: block;
}
#prev {
left: 20px;
}
#next {
right: 20px;
}

Js

首先我們先實(shí)現(xiàn)出手動(dòng)點(diǎn)擊左右兩個(gè)箭頭切換圖片的效果:

window.onload = function() {
var list = document.getElementById('list');var prev = document.getElementById('prev');
var next = document.getElementById('next');
function animate(offset) {
//獲取的是style.left,是相對(duì)左邊獲取距離,所以第一張圖后style.left都為負(fù)值,
//且style.left獲取的是字符串,需要用parseInt()取整轉(zhuǎn)化為數(shù)字。
var newLeft = parseInt(list.style.left) + offset;
list.style.left = newLeft + 'px';
}
prev.onclick = function() { 
animate(600);
}
next.onclick = function() { 
animate(-600);
}
}

運(yùn)行后我們會(huì)發(fā)現(xiàn),一直點(diǎn)擊右箭頭 ,會(huì)出現(xiàn)空白,而且,不能回到第一張圖片。要點(diǎn)擊左箭頭才能回到第一張圖片。

利用谷歌瀏覽器F12,原因是我們利用偏移量left來(lái)獲取圖片,當(dāng)看到left值小于3600時(shí),因?yàn)闆](méi)有第8張圖片就出現(xiàn)空白,所以這里我們需要對(duì)偏移量做一個(gè)判斷。

在animate函數(shù)里加上這么一段:

if(newLeft<-3000){
list.style.left = -600 + 'px';
}
if(newLeft>-600){
list.style.left = -3000 + 'px';
}

好,運(yùn)行一下,沒(méi)問(wèn)題了。輪播圖,顧名思義,是自己會(huì)動(dòng)的圖片,這個(gè)時(shí)候我們需要用到瀏覽器的內(nèi)置對(duì)象定時(shí)器。

對(duì)于定時(shí)器,有必要說(shuō)明一下setInterval()跟setTimeout的區(qū)別了。簡(jiǎn)單來(lái)說(shuō),setInterval()執(zhí)行多次,setTimeout()只執(zhí)行一次。

更具體的用法可以點(diǎn)擊鏈接查看區(qū)別:window.setInterval window.setTimeout 。

這里我們是用setInterval(),因?yàn)槲覀兊膱D片需要循環(huán)滾動(dòng)。插入下面

var timer;
function play() {
timer = setInterval(function () {
prev.onclick()
}, 1500)
}
play();

運(yùn)行,ok!

但是,當(dāng)我們想仔細(xì)看某一張圖片時(shí)候,要把圖片停住,我們清楚定時(shí)器就可以了,這里用到window.clearInterval 這個(gè)方法。

這里,我們需要對(duì)其DOM操作,需要獲取整個(gè)輪播圖區(qū)域;

var container = document.getElementById('container');
function stop() {
clearInterval(timer);
}
container.onmouseover = stop;
container.onmouseout = play;

但這里,一個(gè)輪播圖基本算完成了,有同學(xué)·會(huì)問(wèn),那么簡(jiǎn)單。看到圖片下面的那一排小圓點(diǎn)沒(méi)。我給你加功能了。

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

這里是升級(jí)版:

var buttons = document.getElementById('buttons').getElementsByTagName('span');
var index = 1;
function buttonsShow() {
//這里需要清除之前的樣式
for (var i = 0; i < buttons.length; i++) {
if (buttons[i].className == 'on') {
buttons[i].className = '';
}
}
//數(shù)組從0開(kāi)始,故index需要-1
buttons[index - 1].className = 'on';
}
prev.onclick = function() {
index -= 1;
if (index < 1) {
index = 5;
}
buttonsShow();
animate(600);
}
next.onclick = function() {
//由于上邊定時(shí)器的作用,index會(huì)一直遞增下去,我們只有5個(gè)小圓點(diǎn),所以需要做出判斷
index += 1;
if (index > 5) {
index = 1;
}
buttonsShow();
animate(-600);
}

現(xiàn)在看起來(lái)正常多了吧,但我們想實(shí)現(xiàn)通過(guò)鼠標(biāo)任意點(diǎn)擊其中一個(gè)小圓點(diǎn),切換到相應(yīng)的圖片,原理同樣,我們還是需要通過(guò)偏移量去找到對(duì)應(yīng)的圖片。

for (var i = 0; i < buttons.length; i++) {
buttons[i].onclick = function() {
//優(yōu)化,當(dāng)前圖片點(diǎn)擊當(dāng)前的小圓點(diǎn)不執(zhí)行以下代碼。
if (this.className == "on") {
return;
}
/* 偏移量獲取:這里獲得鼠標(biāo)移動(dòng)到小圓點(diǎn)的位置,用this把index綁定到對(duì)象buttons[i]上,去谷歌this的用法 */
/* 由于這里的index是自定義屬性,需要用到getAttribute()這個(gè)DOM2級(jí)方法,去獲取自定義index的屬性*/
var clickIndex = parseInt(this.getAttribute('index'));
var offset = 600 * (clickIndex - index);
animate(offset);
//存放鼠標(biāo)點(diǎn)擊后的位置,用于小圓點(diǎn)的正常顯示
index = clickIndex;
buttonsShow();
}
}

大家,可能發(fā)現(xiàn)了,這個(gè)輪播圖有點(diǎn)奇怪,不中規(guī)中矩,它是向左切換的,改寫(xiě)一下:

function play() {
//將輪播圖換成向右切換圖片
timer = setInterval(function () {
next.onclick();
}, 2000)
} 
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
* {
margin: 0;
padding: 0;
text-decoration: none;
}
body {
padding: 20px;
}
#container {
width: 600px;
height: 400px;
border: 3px solid #333;
overflow: hidden;
position: relative;
}
#list {
width: 4200px;
height: 400px;
position: absolute;
z-index: 1;
}
#list img {
width: 600px;
height: 400px;
float: left;
}
#buttons {
position: absolute;
height: 10px;
width: 100px;
z-index: 2;
bottom: 20px;
left: 250px;
}
#buttons span {
cursor: pointer;
float: left;
border: 1px solid #fff;
width: 10px;
height: 10px;
border-radius: 50%;
background: #333;
margin-right: 5px;
}
#buttons .on {
background: orangered;
}
.arrow {
cursor: pointer;
display: none;
line-height: 39px;
text-align: center;
font-size: 36px;
font-weight: bold;
width: 40px;
height: 40px;
position: absolute;
z-index: 2;
top: 180px;
background-color: RGBA(0, 0, 0, .3);
color: #fff;
}
.arrow:hover {
background-color: RGBA(0, 0, 0, .7);
}
#container:hover .arrow {
display: block;
}
#prev {
left: 20px;
}
#next {
right: 20px;
}
</style>
<script type="text/javascript">
/* 知識(shí)點(diǎn): */
/* this用法 */
/* DOM事件 */
/* 定時(shí)器 */
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;
function animate(offset) {
//獲取的是style.left,是相對(duì)左邊獲取距離,所以第一張圖后style.left都為負(fù)值,
//且style.left獲取的是字符串,需要用parseInt()取整轉(zhuǎn)化為數(shù)字。
var newLeft = parseInt(list.style.left) + offset;
list.style.left = newLeft + 'px';
//無(wú)限滾動(dòng)判斷
if (newLeft > -600) {
list.style.left = -3000 + 'px';
}
if (newLeft < -3000) {
list.style.left = -600 + 'px';
}
}
function play() {
//重復(fù)執(zhí)行的定時(shí)器
timer = setInterval(function () {
next.onclick();
}, 2000)
}
function stop() {
clearInterval(timer);
}
function buttonsShow() {
//將之前的小圓點(diǎn)的樣式清除
for (var i = 0; i < buttons.length; i++) {
if (buttons[i].className == "on") {
buttons[i].className = "";
}
}
//數(shù)組從0開(kāi)始,故index需要-1
buttons[index - 1].className = "on";
}
prev.onclick = function () {
index -= 1;
if (index < 1) {
index = 5
}
buttonsShow();
animate(600);
};
next.onclick = function () {
//由于上邊定時(shí)器的作用,index會(huì)一直遞增下去,我們只有5個(gè)小圓點(diǎn),所以需要做出判斷
index += 1;
if (index > 5) {
index = 1
}
animate(-600);
buttonsShow();
};
for (var i = 0; i < buttons.length; i++) {
buttons[i].onclick = function () {
//優(yōu)化,當(dāng)前圖片點(diǎn)擊當(dāng)前的小圓點(diǎn)不執(zhí)行以下代碼。
if (this.className == "on") {
return;
}
/* 這里獲得鼠標(biāo)移動(dòng)到小圓點(diǎn)的位置,用this把index綁定到對(duì)象buttons[i]上,去谷歌this的用法 */
/* 由于這里的index是自定義屬性,需要用到getAttribute()這個(gè)DOM2級(jí)方法,去獲取自定義index的屬性*/
var clickIndex = parseInt(this.getAttribute('index'));
var offset = 600 * (clickIndex - index); //這個(gè)index是當(dāng)前圖片停留時(shí)的index
animate(offset);
index = clickIndex; //存放鼠標(biāo)點(diǎn)擊后的位置,用于小圓點(diǎn)的正常顯示
buttonsShow();
}
}
container.onmouseover = stop;
container.onmouseout = play;
play();
}
</script>
</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">&lt;</a>
<a href="javascript:;" id="next" class="arrow">&gt;</a>
</div>
</body>
</html>

以上所述是小編給大家介紹的簡(jiǎn)單的JS輪播圖代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論