使用JavaScript實(shí)現(xiàn)輪播圖特效
更新時間:2021年09月02日 13:13:46 作者:風(fēng)筱默染
這篇文章主要為大家詳細(xì)介紹了使用JavaScript實(shí)現(xiàn)輪播圖特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實(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;/*相對定位*/
margin: 50px auto;/*離頂部50px,并且居中*/
}
.picture img {
position: absolute;/*絕對定位*/
}
.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è)置索引號變量
picture[index].style.opacity = 1 //第一張圖片顯示出來
//右按鈕換圖
right.onclick = function() {
fn("+")
}
//左按鈕換圖
left.onclick = function() {
fn("-")
}
//定時器換圖,每隔3000毫秒換圖
var timer = setInterval(function() {
fn("+")
}, 3000)
//鼠標(biāo)進(jìn)入暫停
aaa.onmouseover = function() {
clearInterval(timer)
}
//鼠標(biāo)離開繼續(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>
效果如圖所示:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ionic中的$ionicPlatform.ready事件中的通用設(shè)置
$ionicPlatform.ready事件是用于檢測當(dāng)前的平臺是否就緒的事件,相當(dāng)于基于document的deviceready事件, 在app中一些通用關(guān)于設(shè)備的設(shè)置必須在這個事件中處理2017-06-06
js canvas實(shí)現(xiàn)簡單的圖像擴(kuò)散效果
這篇文章主要為大家詳細(xì)介紹了js canvas實(shí)現(xiàn)簡單的圖像擴(kuò)散效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-09-09
JavaScript實(shí)現(xiàn)獲取某個元素相鄰兄弟節(jié)點(diǎn)的prev與next方法
這篇文章主要介紹了JavaScript實(shí)現(xiàn)獲取某個元素相鄰兄弟節(jié)點(diǎn)的prev與next方法,涉及JavaScript基于函數(shù)的判定及調(diào)用previousSibling與nextSibling的相關(guān)技巧,需要的朋友可以參考下2016-01-01
javascript實(shí)現(xiàn)圖片輪播簡單效果
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)圖片輪播簡單效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-07-07
vue使用vue-quill-editor富文本編輯器且將圖片上傳到服務(wù)器的功能
這篇文章主要介紹了vue使用vue-quill-editor富文本編輯器且將圖片上傳到服務(wù)器的功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01

