...由于我的圖片畫質太高,效果圖傳不上來, 大家自己試一試吧, 在同一個文件夾保存下面的代碼和分別為1,2,3,4,5.jpg的五張圖片
我在這里用的圖片全部都是1920*1080的尺寸
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>輪播圖</title>
<style type="text/css">
*{
margin: 0px;
padding: 0px;
list-style: none;
}
#box{
width: 1920px;
height: 1080px;
margin: 1px auto;
position: relative;
border: 1px soild block;
overflow: hidden;
}
#box #list{
position: absolute;
width: 13440px;//為了讓五張圖片全部在一行表示出來
height: 1080px;
z-index: 1;
}
#box #list li{
float: left;
height: 1080px;
width: 1920px;
}
#buttons {
position: absolute;
left: 910px;
bottom: 50px;
z-index: 2;//保證小圓點在圖片上層
height: 30px;
width: 300px;
}
#buttons li {
float: left;
margin-right: 20px;
width: 20px;
height: 20px;
border: 1px solid #fff;
border-radius: 50%;
background: #333;
cursor: pointer;
list-style: none;
}
#buttons .on {
background: orangered;
}
.arrow {
position: absolute;
top: 520px;
z-index: 2;
display: none;
width: 200px;
height: 200px;
font-size: 100px;
font-weight: bold;
line-height: 200px;//使方塊中的">"水平垂直居中
text-align: center;
color: #fff;
background-color: rgba(0, 0, 0, 0.3);
cursor: pointer;
}
.arrow:hover {
background-color: rgba(0, 0, 0, 0.7);
}
#box:hover .arrow {//鼠標移入圖片時, 無
display: block;
}
#prev {
left: 20px;
}
#next {
right: 20px;
}
</style>
</head>
<body>
<div id="box">
<ul id="list" style="left: -1920px">
這里又為什么要有7張圖片呢, 主要是為了無縫的優化,無縫滾動。
當你從最后一張圖切換回第一張圖時,有很大空白,利用兩張輔助圖來填補這個空白。
這里補充下無縫滾動,直接看代碼,復制最后一張圖片放置第一張圖片前,同時復制第一張圖片放置最后一張圖片的后面。并且,將第一張圖片輔助圖(實際上是實際顯示的第5張圖片隱藏起來,故設置style="left: -600px;")
<li><a href="#"><img src="5.jpg"></a></li>
<li><a href="#"><img src="1.jpg"></a></li>
<li><a href="#"><img src="2.jpg"></a></li>
<li><a href="#"><img src="3.jpg"></a></li>
<li><a href="#"><img src="4.jpg"></a></li>
<li><a href="#"><img src="5.jpg"></a></li>
<li><a href="#"><img src="1.jpg"></a></li>
</ul>
<ul id="buttons">//小圓點
<li index="1" class="on"></li>
<li index="2"></li>
<li index="3"></li>
<li index="4"></li>
<li index="5"></li>
</ul>
<a href="javascript:" id="prev" class="arrow"><</a>
<a href="javascript:" id="next" class="arrow">></a>
</div>
<script>
window.onload = function() {
var list = document.getElementById('list');
var prev = document.getElementById('prev');
var next = document.getElementById('next');
var box = document.getElementById('box');
function animate(offset) {
//獲取的是style.left,是相對左邊獲取距離,所以第一張圖后style.left都為負值,
//且style.left獲取的是字符串,需要用parseInt()取整轉化為數字。
var newLeft = parseInt(list.style.left) + offset;
if(newLeft < -11520){
newLeft = -3840;
}//當到邊界時, 返回圖片的當前位置
if(newLeft > 0){
newLeft = -7680;
}
list.style.left = newLeft + 'px';
}
// prev.onclick = function() {
// animate(1920);
// }
// next.onclick = function() {
// animate(-1920);
// }
var timer;
function play() {//設置定時器
timer = setInterval(function () {
next.onclick()
}, 1500)
}
play();
function stop() {
clearInterval(timer);
}
box.onmouseover = stop;
box.onmouseout = play;
var buttons = document.getElementById('buttons').getElementsByTagName('li');
var index = 1;
function buttonsShow() {
for (var i = 0; i < buttons.length; i++) {
if (buttons[i].className == 'on') {
buttons[i].className = '';
}
}//遍歷找到有class為on的li, 并清除掉
buttons[index-1].className = 'on';
}
prev.onclick = function() {
index--;
if (index < 1) {
index = 5;
}
buttonsShow();
animate(1920);
}
next.onclick = function() {
index++;
if (index > 5) {
index = 1;
}
buttonsShow();
animate(-1920);
}
for (var i = 0; i < buttons.length; i++) {//下面由于產生了一個典型的閉包, 因此要采用立即執行函數的方法
(function(j){
buttons[j].onclick = function () {
console.log(j);
var clickIndex = parseInt(this.getAttribute('index'));
var offset = 1920 * (index - clickIndex);
animate(offset); //存放鼠標點擊后的位置,用于小圓點的正常顯示
index = clickIndex;
buttonsShow();
}
})(i)
}
}
</script>
</body>
</html>