<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
/* css代碼部分 */
*{margin:0;padding:0;}
ol,ul,li{list-style: none;}
.wrap{
position: relative;
width:750px;
height:300px;
margin:200px auto;
outline:4px dotted red;
}
.slide{
position: absolute;
}
.slide>li{
position: absolute;
left:0;
top:0;
width:750px;
height:300px;
display:none;
}
.slide img{
width:100%;
height:100%;
}
.nav{
width:100%;
position: absolute;
top:50%;
margin-top:-35px;
display: flex;
justify-content: space-between;
}
.nav a{
display:block;
text-decoration: none;
padding:8px 4px;
background-color: #bbb;
color:white;
font-size: 40px;
opacity: 0.2;
}
.bottom-nav{
position: absolute;
bottom:16px;
left:50%;
margin-left:-100px;
display:flex;
justify-content: space-evenly;
width:200px;
}
.bottom-nav li{
width:24px;
height:24px;
text-align: center;
background-color: #fff;
line-height: 24px;
border-radius: 50%;
cursor: pointer;
}
.bottom-nav .active{
background-color: red;
}
.slide .show{
display: block;
}
</style>
</head>
<body>
<!-- HTML代碼部分 -->
<div class="wrap">
<ul class="slide">
<li class="show"><img src="./images/1.png" alt=""></li>
<li><img src="./images/2.png" alt=""></li>
<li><img src="./images/3.png" alt=""></li>
<li><img src="./images/4.png" alt=""></li>
<li><img src="./images/5.png" alt=""></li>
</ul>
<!-- 左右導(dǎo)航 -->
<div class="nav">
<a href="#" class="left"><</a>
<a href="#" class="right">></a>
</div>
<!-- 底部導(dǎo)航 -->
<ol class="bottom-nav">
<li class="active">1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ol>
</div>
<script>
// js代碼部分
var wrap = document.querySelector('.wrap');
var li = document.querySelectorAll('.slide>li');
var leftBtn = document.querySelector('.nav>.left');
var rightBtn = document.querySelector('.nav>.right');
var bottomBtn = document.querySelectorAll('.bottom-nav>li');
// 1.自動(dòng)輪播功能
//設(shè)置自動(dòng)輪播計(jì)時(shí)器
var timer = setInterval(slideFun,2000);
var cutIndex=0; //當(dāng)前圖片顯示的索引
function slideFun(){
cutIndex++;
if(cutIndex>li.length-1){
cutIndex=0;
}
show(cutIndex)
}
// 根據(jù)索引顯示隱藏圖片
function show(cutIndex){
//設(shè)置全部圖片隱藏 底部導(dǎo)航背景顏色白色
for(var i=0;i<li.length;i++){
li[i].className='';
bottomBtn[i].className='';
}
//設(shè)置當(dāng)前展示的導(dǎo)航效果和當(dāng)前顯示圖片
li[cutIndex].className='show';
bottomBtn[cutIndex].className='active';
}
// 2.鼠標(biāo)移入 輪播停止
wrap.onmouseover=function(){
leftBtn.style.opacity=0.8;
rightBtn.style.opacity=0.8;
clearInterval(timer);
}
wrap.onmouseout=function(){
leftBtn.style.opacity=0.2;
rightBtn.style.opacity=0.2;
timer=setInterval(slideFun,2000)
}
// 3.左右導(dǎo)航功能
leftBtn.onclick=function(){
cutIndex--;
if(cutIndex<0){
cutIndex=li.length-1;
}
show(cutIndex);
}
rightBtn.onclick=function(){
cutIndex++;
if(cutIndex>li.length-1){
cutIndex=0;
}
show(cutIndex);
}
// 4.底部導(dǎo)航功能
for(var i=0;i<bottomBtn.length;i++){
bottomBtn[i].index=i;
bottomBtn[i].onclick=function(){
cutIndex = this.index;
show(cutIndex);
}
}
</script>
</body>
</html>
原生js實(shí)現(xiàn)輪播圖效果
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
禁止轉(zhuǎn)載猾骡,如需轉(zhuǎn)載請(qǐng)通過(guò)簡(jiǎn)信或評(píng)論聯(lián)系作者。
- 文/潘曉璐 我一進(jìn)店門作媚,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)攘滩,“玉大人,你說(shuō)我怎么就攤上這事纸泡∑剩” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵女揭,是天一觀的道長(zhǎng)蚤假。 經(jīng)常有香客問(wèn)我,道長(zhǎng)吧兔,這世上最難降的妖魔是什么磷仰? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮境蔼,結(jié)果婚禮上灶平,老公的妹妹穿的比我還像新娘。我一直安慰自己箍土,他們只是感情好逢享,可當(dāng)我...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著吴藻,像睡著了一般瞒爬。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上调缨,一...
- 那天疮鲫,我揣著相機(jī)與錄音吆你,去河邊找鬼弦叶。 笑死,一個(gè)胖子當(dāng)著我的面吹牛妇多,可吹牛的內(nèi)容都是我干的伤哺。 我是一名探鬼主播,決...
- 文/蒼蘭香墨 我猛地睜開眼者祖,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼立莉!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起七问,我...
- 序言:老撾萬(wàn)榮一對(duì)情侶失蹤蜓耻,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后械巡,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體刹淌,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡饶氏,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了有勾。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片疹启。...
- 正文 年R本政府宣布荤懂,位于F島的核電站,受9級(jí)特大地震影響塘砸,放射性物質(zhì)發(fā)生泄漏势誊。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一谣蠢、第九天 我趴在偏房一處隱蔽的房頂上張望粟耻。 院中可真熱鬧,春花似錦眉踱、人聲如沸挤忙。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)册烈。三九已至,卻和暖如春婿禽,著一層夾襖步出監(jiān)牢的瞬間赏僧,已是汗流浹背。 一陣腳步聲響...
- 正文 我出身青樓膛壹,卻偏偏與公主長(zhǎng)得像驾中,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子模聋,可洞房花燭夜當(dāng)晚...