JS輪播圖

看了智能社BLUE老師的視頻,其中介紹了一個JS輪播圖。javascript

多是由於BLUE老師用了不少期視頻,一步一步慢慢來構建本身的運動框架的緣由,對於新手的我來講好複雜。css

轉載一個最近看到的手寫的JS的輪播圖。html

慢慢品味其中的代碼。java

<!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 {
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;
width: 100px;
}

#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;
}
</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;this

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';
}
}spa

function play() {
//重複執行的定時器
timer = setInterval(function() {
next.onclick();
}, 2000)
}視頻

function stop() {
clearInterval(timer);
}htm

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();

}
</script>
</head>

<body>

<div id="container">
<div id="list" style="left: -600px;">
<img src="" alt="1" />
<img src="" alt="1" />
<img src="" alt="2" />
<img src="" alt="3" />
<img src="" alt="4" />
<img src="" alt="5" />
<img src="" 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>

相關文章
相關標籤/搜索