原生JavaScript實(shí)現(xiàn)輪播圖
本文實(shí)例為大家分享了JavaScript實(shí)現(xiàn)輪播圖的具體代碼,供大家參考,具體內(nèi)容如下
效果:

代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
* {
margin: 0;
padding: 0;
}
ul,
li {
list-style: none;
}
.banner {
width: 1200px;
height: 535px;
border: 1px solid red;
margin: 0 auto;
position: relative;
}
.slider li {
position: absolute;
left: 0;
top: 0;
}
a {
width: 40px;
height: 50px;
background-color: rgba(0, 0, 0, 0.1);
font-size: 50px;
text-align: center;
line-height: 50px;
position: absolute;
text-decoration: none;
color: gray;
}
.btnl {
left: 0;
top: 50%;
margin-top: -15px;
}
.btnr {
right: 0;
top: 50%;
margin-top: -25px;
}
.tabs {
position: absolute;
bottom: 20px;
left: 50%;
margin-left: -75px;
}
.tabs li {
width: 15px;
height: 15px;
background-color: #ccc;
border-radius: 50%;
float: left;
margin-right: 10px;
}
</style>
</head>
<body>
<div class="banner">
<ul class="slider">
<li><img src="img/b1.jpg" alt="" /></li>
<li><img src="img/b2.jpg" alt="" /></li>
<li><img src="img/b3.jpg" alt="" /></li>
<li><img src="img/b4.jpg" alt="" /></li>
<li><img src="img/b5.jpg" alt="" /></li>
<li><img src="img/b6.jpg" alt="" /></li>
</ul>
<a href="javascript:void(0);" class="btnl">
<</a>
<a href="javascript:void(0);" class="btnr">></a>
<ul class="tabs">
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
<script type="text/javascript">
var banner = document.getElementsByClassName("banner")[0];
var slider = document.getElementsByClassName("slider")[0];
var li = slider.getElementsByTagName("li");
var btnl = document.getElementsByClassName("btnl")[0];
var btnr = document.getElementsByClassName("btnr")[0];
var tabs = document.getElementsByClassName("tabs")[0];
var btns = tabs.getElementsByTagName("li");
//初始化
btns[0].style.backgroundColor = "red";
for(var i = 0; i < li.length; i++) {
if(i == 0) {
continue;
} else {
li[i].style.opacity = 0;
}
}
var timer = setInterval(mover, 1000);
//聲明一個(gè)變量,代表當(dāng)前圖片的下標(biāo)
var num = 0;
function mover() {
num++;
if(num == li.length) {
num = 0;
}
for(var i = 0; i < li.length; i++) {
li[i].style.opacity = 0;
btns[i].style.backgroundColor = "#ccc";
}
li[num].style.opacity = 1;
btns[num].style.backgroundColor = "red";
}
function movel() {
num--;
if(num == -1) {
num = li.length - 1;
}
for(var i = 0; i < li.length; i++) {
li[i].style.opacity = 0;
btns[i].style.backgroundColor = "#ccc";
}
li[num].style.opacity = 1;
btns[num].style.backgroundColor = "red";
}
banner.onmouseover = function() {
clearInterval(timer)
}
banner.onmouseout = function() {
timer = setInterval(mover, 1000)
}
btnl.onclick = function(e) {
movel();
}
btnr.onclick = function(e) {
mover();
}
// 小圓點(diǎn)效果
for(var i = 0; i < btns.length; i++) {
btns[i].index = i;
btns[i].onmouseover = function() {
if(this.index == num) {
return;
} else {
for(var i = 0; i < li.length; i++) {
li[i].style.opacity = 0;
btns[i].style.backgroundColor = "#ccc";
}
li[this.index].style.opacity = 1;
btns[this.index].style.background="red";
num=this.index;
}
}
}
</script>
</body>
</html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- js實(shí)現(xiàn)輪播圖的完整代碼
- 原生js實(shí)現(xiàn)輪播圖的示例代碼
- js實(shí)現(xiàn)支持手機(jī)滑動(dòng)切換的輪播圖片效果實(shí)例
- js實(shí)現(xiàn)點(diǎn)擊左右按鈕輪播圖片效果實(shí)例
- JS輪播圖實(shí)現(xiàn)簡(jiǎn)單代碼
- JS實(shí)現(xiàn)左右無(wú)縫輪播圖代碼
- JS實(shí)現(xiàn)自動(dòng)輪播圖效果(自適應(yīng)屏幕寬度+手機(jī)觸屏滑動(dòng))
- 原生js實(shí)現(xiàn)無(wú)限循環(huán)輪播圖效果
- 使用html+js+css 實(shí)現(xiàn)頁(yè)面輪播圖效果(實(shí)例講解)
- 基于vue.js輪播組件vue-awesome-swiper實(shí)現(xiàn)輪播圖
相關(guān)文章
詳解ES6中的 Set Map 數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)總結(jié)
這篇文章主要介紹了詳解ES6中的 Set Map 數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)總結(jié),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
javascript設(shè)計(jì)模式之Adapter模式【適配器模式】實(shí)現(xiàn)方法示例
這篇文章主要介紹了javascript設(shè)計(jì)模式之Adapter模式,結(jié)合實(shí)例形式分析了JS適配器模式的原理與具體實(shí)現(xiàn)方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2017-01-01
JavaScript使用Promise實(shí)現(xiàn)并發(fā)請(qǐng)求數(shù)限制
本文主要介紹了JavaScript使用Promise實(shí)現(xiàn)并發(fā)請(qǐng)求數(shù)限制,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
uniapp實(shí)現(xiàn)附近商家定位的示例代碼
本文主要介紹了uniapp實(shí)現(xiàn)附近商家定位的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
JavaScript實(shí)現(xiàn)開關(guān)等效果
本文給大家分享一段簡(jiǎn)單的代碼基于js實(shí)現(xiàn)開關(guān)燈效果,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-09-09

