原生JavaScript實現(xiàn)精美的淘寶輪播圖效果示例【附demo源碼下載】
本文實例講述了原生JavaScript實現(xiàn)的淘寶輪播圖效果。分享給大家供大家參考,具體如下:
輪播圖是我們學習原生js的必經(jīng)之路
它包含很多基本知識的運用,像this的使用,DOM的操作,還有setInterval的使用和清除,浮動與定位等等,很好的考察了我們的基礎(chǔ)知識牢不牢固,
話不多說,直接上圖



HTML代碼如下:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>淘寶輪播圖</title>
<link rel="stylesheet" href="css/initialize.css" rel="external nofollow" />
<link rel="stylesheet" href="css/tblunbotu.css" rel="external nofollow" />
</head>
<body>
<div id="container" class="clearFix">
<div id="list" class="clearFix" style="left: -520px">
<img src="img/5.jpg" alt=""/>
<img src="img/1.jpg" alt=""/>
<img src="img/2.jpg" alt=""/>
<img src="img/3.jpg" alt=""/>
<img src="img/4.jpg" alt=""/>
<img src="img/5.jpg" alt=""/>
<img src="img/1.jpg" alt=""/>
</div>
<div id="buttons" class="clearFix">
<span class="on"></span>
<span></span>
<span></span>
<span></span>
<span></span>
</div>
<a href="javascript:;" rel="external nofollow" rel="external nofollow" id="prev" class="arrow"><</a>
<a href="javascript:;" rel="external nofollow" rel="external nofollow" id="next" class="arrow">></a>
</div>
<script type="text/javascript" src="js/tblunbotu.js"></script>
</body>
</html>
CSS樣式如下:
/*
第一步:設(shè)置外部框的樣式
第二步: 設(shè)置圖片框的樣式
第三步: 設(shè)置箭頭的樣式
第四步: 設(shè)置小圓點的樣式
*/
#container {
margin: 50px auto;
position: relative;
width: 520px;
height: 280px;
overflow: hidden;
}
#list {
position: absolute;
z-index: 1;
width: 3640px;
}
#list img {
float: left;
width: 520px;
height: 280px;
}
#buttons {
height: 10px;
width: 100px;
position: absolute;
left: 0;/*設(shè)置水平垂直居中*/
right: 0;/*設(shè)置水平垂直居中*/
margin: 0 auto;/*設(shè)置水平垂直居中*/
bottom: 20px;
z-index: 2;
}
#buttons span {
float: left;
margin-right: 5px;
width: 10px;
height: 10px;
border: 1px solid #cccccc;
border-radius: 50%;
background: #333;
cursor: pointer;
}
#buttons .on {
background: orangered;
}
.arrow {
width: 40px;
height: 40px;
display: none;
position: absolute;
top: 0; /*設(shè)置水平垂直居中*/
bottom: 0; /*設(shè)置水平垂直居中*/
margin: auto 0; /*設(shè)置水平垂直居中*/
font-size: 36px;
font-weight: bold;
line-height: 39px;
text-align: center;
color: #fff;
background-color: RGBA(0, 0, 0, .3);
cursor: pointer;
z-index: 2;
}
.arrow:hover{
background-color: RGBA(0, 0, 0, .7);
}
#container:hover .arrow {
display: block;
}
#prev{
left: 10px;
}
#next{
right: 10px;
}
javascript代碼如下
/**
* Created by zhm on 17.1.15.
*/
/*
*知識點:
* this使用
* DOM事件
* 定時器
*
* 思路:
* (1)設(shè)置它左右移動
* 問題:傳入數(shù)字為NAN??
* 解決:在頁面中增加屬性style:left:0
* (2)平滑移動(移動時間固定,每次移動的距離不一樣)
* 問題:連續(xù)點擊出現(xiàn)晃動?---設(shè)置標志位
* 出現(xiàn)空白頁??--- 第一張圖片前加上最后一張,最后一張圖片前加上第一張
* 在類list的標簽中增加屬性style:left:-520px;
* 設(shè)置無限滾動判斷
*
* (3)設(shè)置小圓點
* 首先將所有的類置為空,當前類置為on
* 綁定小圓點和圖片
* 綁定小圓點和左右箭頭
* 設(shè)置定時器,鼠標劃上去停止,移開自動輪播
*
* */
//1.獲取元素
var container = document.getElementById("container");
var list = document.getElementById("list");
var prev = document.getElementById("prev");
var next = document.getElementById("next");
var buttons = document.getElementById('buttons').getElementsByTagName('span');
var timer = null;
var timer2 = null;
var flag = true;
var index =0;
//2.設(shè)置函數(shù)
function moveImg(dis) {
var time = 400;//運動的總時間
var eachTime = 10;//每次小移動的時間
var eachDis = dis/(time/eachTime);//每次小移動的距離
var newWeizhi = parseInt(list.style.left) + dis;//新位置
flag = false;
function eachMove(){
if(dis > 0 && parseInt(list.style.left)< newWeizhi || dis < 0 && parseInt(list.style.left)>newWeizhi){
list.style.left = parseInt(list.style.left) + eachDis + 'px';
}else {
flag = true;
clearInterval(timer);
list.style.left = newWeizhi + 'px';
//無限滾動判斷
if (newWeizhi == 0) {
list.style.left = -2600 + 'px';
}
if (newWeizhi == -3120) {
list.style.left = -520 + 'px';
}
}
}
timer = setInterval(eachMove, 10);
}
//3.設(shè)置點擊切換圖片
next.onclick = function () {
if(!flag) return;
moveImg(-520);
//綁定箭頭和小圓點
if (index == 4) {
index = 0;
} else {
index++;
}
buttonsShow();
};
prev.onclick = function () {
if(!flag) return;
moveImg(520);
//綁定箭頭和小圓點
if (index == 0) {
index = 4;
} else {
index--;
}
buttonsShow();
};
//4.設(shè)置小圓點的綁定
function buttonsShow() {
//將之前的小圓點的樣式清除
for (var i = 0; i < buttons.length; i++) {
if (buttons[i].className == "on") {
buttons[i].className = "";
break;
}
}
buttons[index].className = "on";
}
for(var i = 0 ;i<buttons.length;i++){
buttons[i].value = i;
//使用自執(zhí)行函數(shù)解決i的賦值問題
(function(){
buttons[i].onclick = function(){
if(this.value == index) return;
var offset = (this.value - index)* -520;
moveImg(offset);
index = this.value;
buttonsShow();
}
})();
}
//5.設(shè)置自動輪播
timer2 = setInterval(next.onclick,1500);
container.onmouseover = function(){
clearInterval(timer2);
};
container.onmouseout = function(){
timer2 = setInterval(next.onclick,1000);
};
源碼下載:完整實例代碼點擊此處本站下載。
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript切換特效與技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》、《JavaScript動畫特效與技巧匯總》、《JavaScript錯誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學運算用法總結(jié)》
希望本文所述對大家JavaScript程序設(shè)計有所幫助。
相關(guān)文章
JS中如何使用 filter() 方法過濾數(shù)組元素
filter()方法是JavaScript中用于過濾數(shù)組元素的常用方法,它接受一個回調(diào)函數(shù)作為參數(shù),返回一個新數(shù)組,本文總結(jié)了使用filter()方法的最佳實踐,感興趣的朋友一起看看吧2025-01-01
淺析JavaScript的幾種Math函數(shù),random(),ceil(),round(),floor()
本文主要對JavaScript的幾種Math函數(shù),random(),ceil(),round(),floor()的作用進行簡要解析,具有很好的參考價值,需要的朋友一起來看下吧2016-12-12
使用Math.max,Math.min獲取數(shù)組中的最值實例
下面小編就為大家?guī)硪黄褂肕ath.max,Math.min獲取數(shù)組中的最值實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04
Bootstrap基本組件學習筆記之input輸入框組(9)
這篇文章主要為大家詳細介紹了Bootstrap基本組件學習筆記之input輸入框組,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12

