使用JavaScript實(shí)現(xiàn)輪播圖特效
本文實(shí)例為大家分享了JavaScript實(shí)現(xiàn)輪播圖特效的具體代碼,供大家參考,具體內(nèi)容如下
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
.aaa {
width: 600px;
height: 350px;
position: relative;/*相對(duì)定位*/
margin: 50px auto;/*離頂部50px,并且居中*/
}
.picture img {
position: absolute;/*絕對(duì)定位*/
}
.dot {
position: absolute;
bottom: 5px;
}
.dot li {
float: left;
width: 16px;
height: 16px;
background-color: #e8e8e8;
border-radius: 50%;
list-style: none;/*清除列表樣式*/
margin-right: 10px;
cursor: pointer;/*光標(biāo)呈現(xiàn)為指示鏈接的指針(一只手)*/
}
.left {
width: 30px;
height: 30px;
position: absolute;
top: 169px;
text-align: center;
background-color: #000000;
line-height: 30px;
color: #FFFFFF;
cursor: pointer;/*光標(biāo)呈現(xiàn)為指示鏈接的指針(一只手)*/
}
.right {
width: 30px;
height: 30px;
position: absolute;
top: 169px;
right: 0;
text-align: center;
background-color: #000000;
line-height: 30px;
color: #FFFFFF;
cursor: pointer;/*光標(biāo)呈現(xiàn)為指示鏈接的指針(一只手)*/
}
.aaa .spot {
background-color: #FF0000;
}
</style>
</head>
<body>
<div class="aaa">
<div class="picture">
<img src="images/1.jpg" style="width: 600px;height: 350px;">
<img src="images/2.jpg" style="width: 600px;height: 350px;">
<img src="images/3.jpg" style="width: 600px;height: 350px;">
<img src="images/4.jpg" style="width: 600px;height: 350px;">
<img src="images/5.jpg" style="width: 600px;height: 350px;">
</div>
<ul class="dot">
<li class="spot"></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
<div class="left"><</div><!--< 轉(zhuǎn)義字符 -->
<div class="right">></div><!--> 轉(zhuǎn)義字符 -->
</div>
<script>
var lis = document.querySelectorAll(".dot li")
var picture = document.querySelectorAll(".picture img")
var left = document.querySelector(".left")
var right = document.querySelector(".right")
var aaa = document.querySelector(".aaa")
var index = 0 //設(shè)置索引號(hào)變量
picture[index].style.opacity = 1 //第一張圖片顯示出來(lái)
//右按鈕換圖
right.onclick = function() {
fn("+")
}
//左按鈕換圖
left.onclick = function() {
fn("-")
}
//定時(shí)器換圖,每隔3000毫秒換圖
var timer = setInterval(function() {
fn("+")
}, 3000)
//鼠標(biāo)進(jìn)入暫停
aaa.onmouseover = function() {
clearInterval(timer)
}
//鼠標(biāo)離開(kāi)繼續(xù)
aaa.onmouseout = function() {
timer = setInterval(function() {
fn("+")
}, 3000)
}
//鼠標(biāo)觸碰小點(diǎn)換圖
for (var i = 0; i < lis.length; i++) {
lis[i].in = i
lis[i].onmouseover = function() {
fn(this.in)
}
}
//函數(shù)
function fn(ope) {
picture[index].style.opacity = 0 //上一張圖片隱藏
lis[index].className = "" //清除小點(diǎn)樣式
//判斷ope
if (typeof ope === 'number') {
index = ope
} else if (ope === '+') { //判斷是否右按鈕
if (index === 4) {
index = 0
} else {
index++
}
} else {
if (index === 0) { //判斷是否左按鈕
index = 4
} else {
index--
}
}
picture[index].style.opacity = 1 //當(dāng)前圖片顯示
lis[index].className = "spot" //給小點(diǎn)加上樣式
}
</script>
</body>
</html>
效果如圖所示:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript使用類似break機(jī)制中斷forEach循環(huán)的方法
這篇文章主要介紹了JavaScript使用類似break機(jī)制中斷forEach循環(huán)的方法,需要的朋友可以參考下2018-11-11
JavaScript使用lodash實(shí)現(xiàn)命名轉(zhuǎn)換和函數(shù)封裝
Lodash?是一個(gè)?JavaScript?的工具庫(kù),它提供了一系列的函數(shù)來(lái)簡(jiǎn)化代碼編寫,本文主要為大家介紹了如何使用lodash實(shí)現(xiàn)命名轉(zhuǎn)換和函數(shù)封裝,感興趣的小伙伴可以了解下2023-11-11
分享一個(gè)原生的JavaScript拖動(dòng)方法
本文給大家分享的是基于JavaScript的setCapture方法實(shí)現(xiàn)的拖動(dòng)效果,非常的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下2016-09-09
JavaScript面試必備技巧之手寫一個(gè)Promise
很多同學(xué)在面試的時(shí)候都會(huì)被要求手寫一個(gè)Promise,那么今天我總結(jié)了一些手寫Promise的方法,可以跟著我的思路一起來(lái)實(shí)現(xiàn)一個(gè)Promise,讓我們的面試更有把握2023-02-02
javascript多物體運(yùn)動(dòng)實(shí)現(xiàn)方法分析
這篇文章主要介紹了javascript多物體運(yùn)動(dòng)實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了JavaScript多物體運(yùn)動(dòng)的相關(guān)注意事項(xiàng)與具體實(shí)現(xiàn)代碼,包含四個(gè)div塊的橫向、豎向移動(dòng),顏色與邊框漸變效果,需要的朋友可以參考下2016-01-01
JavaScript Memoization 讓函數(shù)也有記憶功能
函數(shù)可以用對(duì)象去記住先前操作的結(jié)果,從而能避免無(wú)謂的運(yùn)算,這種優(yōu)化被稱為記憶(Memoization)。JavaScript 的對(duì)象和數(shù)組要實(shí)現(xiàn)這種優(yōu)化是非常方便的。2011-10-10
《JavaScript高級(jí)編程》學(xué)習(xí)筆記之object和array引用類型
本文給大家分享我的javascript高級(jí)編程學(xué)習(xí)筆記之object和array引用類型,涉及到j(luò)avascript引用類型相關(guān)知識(shí),對(duì)javascript引用類型感興趣的朋友可以參考下本文2015-11-11
JavaScript操作選擇對(duì)象的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇JavaScript操作選擇對(duì)象的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧2016-05-05
layer彈出層倒計(jì)時(shí)關(guān)閉的實(shí)現(xiàn)方法
今天小編就為大家分享一篇layer彈出層倒計(jì)時(shí)關(guān)閉的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09

