欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

js实现幻灯片轮播图

程序员文章站 2022-06-24 23:10:08
本文实例为大家分享了js实现幻灯片轮播图的具体代码,供大家参考,具体内容如下1.html选取了五张图片 放入div中,然后是左右箭头,以及按钮,代码如下<...

本文实例为大家分享了js实现幻灯片轮播图的具体代码,供大家参考,具体内容如下

1.html

选取了五张图片 放入div中,然后是左右箭头,以及按钮,代码如下

<!doctype html>
<html lang="en">
<head>
 <meta charset="utf-8">
 <title>轮播图</title>
 <link href="../css/轮播图.css" rel="stylesheet">
</head>
<body>
<div id="box">
 <img src="../image/car-1.jpg">
 <img src="../image/car-2.jpg">
 <img src="../image/car-3.jpg">
 <img src="../image/car-4.jpg">
 <img src="../image/car-5.jpg">
 <div class="arrow">
 <a class="left" href="javacript:void(0);" ><</a>
 <a class="right" href="javacript:void(0);" >></a>
 </div>
 <ul class="btn">
 <li class="on" slideindex="1">1</li>
 <li slideindex="2">2</li>
 <li slideindex="3">3</li>
 <li slideindex="4">4</li>
 <li slideindex="5">5</li>
 </ul>
</div>
<script src="../js/轮播图.js">
 </script>
</body>
</html>

2.css给div设置居中

将所有图片隐藏,设置箭头和小圆点的样式
代码如下

*{
 margin:0;
 padding:0;
 text-decoration: none;
 list-style: none;
}
#box{
 width:800px;
 height: 500px;
 margin:50px auto 0px;
 position: relative;
}
#box img{
 width:800px;
 height: 500px;
 display: none;
 animation:fade 3s;
}
#box .arrow{
 width:800px;
 height: 80px;
 position: absolute;
 top:50%;
 margin-top: -40px;
}
#box .arrow .left,.right{
 display: inline-block;
 line-height: 80px;
 width: 50px;
 height:80px;

}
#box .arrow .left:hover{
 background:rgba(0,0,0,0.8);
}
#box .arrow .right:hover{
 background:rgba(0,0,0,0.8);
}
#box .arrow .right{
 text-align: right;
 position: absolute;
 right:0;
}
#box .arrow a{
 font-size: 50px;
 color: #ffffff;
}
#box .btn{
 position: absolute;
 bottom: 10px;
 left:50%;
 margin-left: -98.47px;
 text-align: center;
}
#box .btn li{
 text-align: center;
 margin:0 5px;
 padding: 10px;
 float:left;
 background:white;
 border-radius:20%;
 cursor: pointer;

 transition: background 2s ease;
}
#box .btn .on{
 background: #000;
 color:white;
}
@keyframes fade{
 from{
 opacity:.4;
 }
 to{
 opacity:1;
 }
}

3.js部分

js设定让当前图片显示display:block,其他图片隐藏display:none;

js代码如下

window.onload=function () {
 var left=document.getelementsbyclassname("left")[0];
 var right=document.getelementsbyclassname("right")[0];
 var btn=document.getelementsbyclassname("btn")[0].getelementsbytagname("li");
 var box=document.getelementbyid("box");
 var slideindex=1;
 var index=1;
 var timer;
 //图片切换函数
 showslides(slideindex);
 function showslides(n) {
 var slides=document.getelementbyid("box").getelementsbytagname("img");
 for(var i=0;i<slides.length;i++){
  slides[i].style.display="none";
  btn[i].classname="";
 }
 slides[slideindex-1].style.display="block";
  btn[slideindex-1].classname="on"
 }
 //箭头切换
 left.onclick=function () {
 if(slideindex>1) {
  slideindex--;
  showslides(slideindex);
 }else {
  slideindex=5;
  showslides(slideindex);
 }
 }
  right.onclick=function () {
  if(slideindex<5) {
   slideindex++;
   showslides(slideindex);
  }else {
   slideindex=1;
   showslides(slideindex);
  }
  }
  //btn切换
 for(var i=0;i<btn.length;i++){
 btn[i].onclick=function () {
  var myslideindex=this.getattribute('slideindex');
  // var myindex=parseint(this.getattribute("index"));
  slideindex=myslideindex;
  showslides(slideindex);
 }
 }
 //自动播放
 function play() {
 timer=setinterval(function () {
  right.onclick();
 },4000);
 }
 function stop() {
 clearinterval(timer);
 }
box.onmouseout=play;
 box.onmouseover=stop;
 play();
}

精彩专题分享:jquery图片轮播 javascript图片轮播 bootstrap图片轮播

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。