最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

簡單的JS輪播圖代碼

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

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

輪播圖的原理:

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

Html布局

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

<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)化,無縫滾動。

當你從最后一張圖切換回第一張圖時,有很大空白,利用兩張輔助圖來填補這個空白。

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

CSS修飾

1、對盒子模型,文檔流的理解,絕對定位問題。

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

3、確保buttons中每個span所在層置頂,將其設置為最頂端。(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

首先我們先實現(xiàn)出手動點擊左右兩個箭頭切換圖片的效果:

window.onload = function() {
var list = document.getElementById('list');var prev = document.getElementById('prev');
var next = document.getElementById('next');
function animate(offset) {
//獲取的是style.left,是相對左邊獲取距離,所以第一張圖后style.left都為負值,
//且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);
}
}

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

利用谷歌瀏覽器F12,原因是我們利用偏移量left來獲取圖片,當看到left值小于3600時,因為沒有第8張圖片就出現(xiàn)空白,所以這里我們需要對偏移量做一個判斷。

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

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

好,運行一下,沒問題了。輪播圖,顧名思義,是自己會動的圖片,這個時候我們需要用到瀏覽器的內(nèi)置對象定時器。

對于定時器,有必要說明一下setInterval()跟setTimeout的區(qū)別了。簡單來說,setInterval()執(zhí)行多次,setTimeout()只執(zhí)行一次。

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

這里我們是用setInterval(),因為我們的圖片需要循環(huán)滾動。插入下面

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

運行,ok!

但是,當我們想仔細看某一張圖片時候,要把圖片停住,我們清楚定時器就可以了,這里用到window.clearInterval 這個方法。

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

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

但這里,一個輪播圖基本算完成了,有同學·會問,那么簡單??吹綀D片下面的那一排小圓點沒。我給你加功能了。

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

這里是升級版:

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開始,故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;
}
buttonsShow();
animate(-600);
}

現(xiàn)在看起來正常多了吧,但我們想實現(xiàn)通過鼠標任意點擊其中一個小圓點,切換到相應的圖片,原理同樣,我們還是需要通過偏移量去找到對應的圖片。

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

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

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">
/* 知識點: */
/* this用法 */
/* DOM事件 */
/* 定時器 */
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,是相對左邊獲取距離,所以第一張圖后style.left都為負值,
//且style.left獲取的是字符串,需要用parseInt()取整轉(zhuǎn)化為數(shù)字。
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() {
//重復執(zhí)行的定時器
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 = "";
}
}
//數(shù)組從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++) {
buttons[i].onclick = function () {
//優(yōu)化,當前圖片點擊當前的小圓點不執(zhí)行以下代碼。
if (this.className == "on") {
return;
}
/* 這里獲得鼠標移動到小圓點的位置,用this把index綁定到對象buttons[i]上,去谷歌this的用法 */
/* 由于這里的index是自定義屬性,需要用到getAttribute()這個DOM2級方法,去獲取自定義index的屬性*/
var clickIndex = parseInt(this.getAttribute('index'));
var offset = 600 * (clickIndex - index); //這個index是當前圖片停留時的index
animate(offset);
index = clickIndex; //存放鼠標點擊后的位置,用于小圓點的正常顯示
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>

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

相關文章

  • JavaScript獲取當前cpu使用率的方法

    JavaScript獲取當前cpu使用率的方法

    這篇文章主要介紹了JavaScript獲取當前cpu使用率的方法,涉及JavaScript針對系統(tǒng)硬件操作的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-12-12
  • Save a File Using a File Save Dialog Box

    Save a File Using a File Save Dialog Box

    Save a File Using a File Save Dialog Box...
    2007-06-06
  • JS實現(xiàn)電商放大鏡效果

    JS實現(xiàn)電商放大鏡效果

    這篇文章主要為大家詳細介紹了JS實現(xiàn)電商放大鏡效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • webpack中的模式(mode)使用詳解

    webpack中的模式(mode)使用詳解

    這篇文章主要介紹了webpack中的模式(mode)使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-02-02
  • 在uni-app中踩過的坑及解決

    在uni-app中踩過的坑及解決

    這篇文章主要介紹了在uni-app中踩過的坑及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • 微信小程序progress組件使用詳解

    微信小程序progress組件使用詳解

    這篇文章主要為大家詳細介紹了微信小程序progress組件的使用,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • 使用js判斷TextBox控件值改變?nèi)缓蟪霭l(fā)事件

    使用js判斷TextBox控件值改變?nèi)缓蟪霭l(fā)事件

    這篇文章主要介紹了使用js判斷TextBox控件值改變?nèi)缓蟪霭l(fā)事件。需要的朋友可以過來參考下,希望對大家有所幫助
    2014-03-03
  • javascript中字體浮動效果的簡單實例演示

    javascript中字體浮動效果的簡單實例演示

    這篇文章主要介紹了javascript中字體浮動效果的簡單實例演示,在一些網(wǎng)站上經(jīng)常遇到當鼠標移導航欄的時候,能夠使其彈出下拉選項,現(xiàn)在就演示一下這種功能,感興趣的小伙伴們可以參考一下
    2015-11-11
  • ES2015 Symbol 一種絕不重復的值

    ES2015 Symbol 一種絕不重復的值

    這篇文章主要介紹了Symbol是ES2015新增的一種值類型數(shù)據(jù),表示一種絕不重復的值,需要的朋友可以參考下
    2016-12-12
  • 如何用JS有效的壓縮圖片

    如何用JS有效的壓縮圖片

    JavaScript操作壓縮圖片原理不難,已有成熟 API,然而在實際輸出壓縮后結(jié)果卻總有意外,有些圖片竟會越壓縮越大,加之終端(手機)類型眾多,有些手機壓縮圖片甚至變黑。本文將一步一步解決此類問題
    2021-05-05

最新評論

颍上县| 饶河县| 信阳市| 大兴区| 建昌县| 濮阳市| 云和县| 云和县| 金昌市| 金昌市| 孟津县| 沂源县| 哈尔滨市| 玉山县| 兴化市| 张家川| 工布江达县| 府谷县| 青岛市| 师宗县| 军事| 中方县| 巴里| 长春市| 杭州市| 运城市| 固镇县| 鄱阳县| 都兰县| 岳普湖县| 益阳市| 巫溪县| 阳朔县| 邹城市| 从江县| 岗巴县| 永川市| 冷水江市| 榆树市| 洞口县| 霞浦县|