三种样式的轮播图
一、100%比例轮播图
1.jpg
HTML代码
CSS样式
*{
margin: 0;
padding: 0;
box-sizing: border-box;
list-style: none;
}
a{
text-decoration: none;
color: #666;
}
img{
display: block;
}
body{
font-size: 12px;
color: #666;
background-color: #fff;
}
.content{
width: 1200px;
margin: 0 auto;
}
.container{
height: 3000px;
position: relative;
}
/* swiper_bg开始 */
.swiper_bg{
width: 100%;
height: 491px;
position: relative;
margin-top: 30px;
border-top: 1px solid #dd0207;
}
.swiper_bg>a.active{
opacity: 1;
}
.swiper_bg>a{
display: block;
opacity: 0;
width: 100%;
height: 491px;
transition: 1.5s;
position: absolute;
top: 0;
left: 0;
}
.swiper_bg>a:nth-child(1){
background: url(../image/swiper_01.jpg) center center no-repeat;
}
.swiper_bg>a:nth-child(2){
background: url(../image/swiper_02.jpg) center center no-repeat;
}
.swiper_bg>a:nth-child(3){
background: url(../image/swiper_03.jpg) center center no-repeat;
}
.swiper_bg>a:nth-child(4){
background: url(../image/swiper_04.jpg) center center no-repeat;
background-size: 100% 100%;
}
.swiper_bg>a:nth-child(5){
background: url(../image/swiper_05.jpg) center center no-repeat;
background-size: 100% 100%;
}
.swiper_bg>.circle{
position: absolute;
top: 450px;
left: 50%;
transform: translateX(-50%);
display: flex;
z-index: 6;
}
.swiper_bg>.circle>span{
display: block;
width: 12px;
height: 12px;
border-radius: 50%;
background-color: #fff;
margin: 0 5px;
cursor: pointer;
}
.swiper_bg>.circle>span.active{
background-color: transparent;
border: 3px solid #fff;
transform: calc(1.5);
}
/* swiper_bg结束 */
js代码
window.onload = function(){
// 封装获取标签函数
function $(selector){
return document.querySelectorAll(selector)
}
// container swiper_bg
function swiperBg(){
var lis = $('.swiper_bg a')
var circles = $('.circle span')
var indexCir = 0
var timer = setInterval(play,3000)
// 轮播
function play(){
indexCir++
if(indexCir>=lis.length){
indexCir = 0
}
for(var i=0;i lis[i].className = '' lis[i].style.zIndex = '0' circles[i].className = '' } lis[indexCir].className = 'active' lis[indexCir].style.zIndex = '2' circles[indexCir].className = 'active' } // circles circles.forEach(function(item,index){ item.onmouseenter = function(){ clearInterval(timer) for(var i=0;i circles[i].className = '' lis[i].className = '' lis[i].style.zIndex = '0' } this.className = 'active' lis[index].className = 'active' lis[index].style.zIndex = '2' indexCir = index } item.onmouseleave = function(){ timer = setInterval(play, 3000) } }) // 浏览器优化 document.addEventListener("visibilitychange", function() { if(document.hidden) { clearInterval(timer) // 浏览器隐藏,清除计时器 } else { timer = setInterval(play, 3000) // 浏览器显示,正常轮播 } }) } swiperBg() } 二、手动箭头轮播图 2.jpg
*{
margin: 0;
padding: 0;
}
.a{
position: relative;
width: 600px;
height: 400px;
margin: 100px auto 0 auto;
overflow: hidden;
box-shadow: dimgrey;
}
.b{
position: absolute;
width: 4200px;
height: 400px;
display: flex;
z-index: 1;
}
.b img{
float:left;
width: 600px;
height: 400px;
}
/* 图片 */
.c{
position: absolute;
left: 30px;
top: 150px;
width: 100px;
height: 100px;
z-index: 10;
}
.cc{
position: absolute;
left: 450px;
top: 20px;
width: 100px;
height: 100px;
z-index: 10;
}
/* 箭头 */
.d{
position: absolute;
bottom: 10px;
left: 250px;
width: 100px;
height: 50px;
z-index: 2;
}
.d span{
margin-left: 5px;
display: inline-block;
width: 10px;
height: 10px;
color:darkgray;
border-radius: 50%;
text-align: center;
cursor: pointer;
background-color: #000;
}
.on{
background-color: brown!important;
}
/* 小圆点 */









var b=document.querySelector('.b');
var f=document.querySelector('.ccc');
var g=document.querySelector('.cc');
var n=document.getElementsByTagName("span");
var k=0;
f.onclick=function(){
f_pic();
}
g.onclick=function(){
g_pic();
}
// 左右箭头,手动轮播
function g_pic(){
var e;
if(b.style.left==="-3600px"){
e=-1200;
}else{
e=parseInt(b.style.left)-600;
}
b.style.left=e+"px";
k++;
if(k>4){
k=0;
}
m();
}
function f_pic(){
var e;
if(b.style.left==="0px"){
e=-2400;
}else{
e=parseInt(b.style.left)+600;
}
b.style.left=e+"px";
k--;
if(k<0){
k=4;
}
m();
}
// 自动播放
var h=null;
function autoPlay(){
h=setInterval(function(){
f_pic();
},1000)
}
autoPlay();
var a=document.querySelector('.a');
a.onmouseenter=function(){
clearInterval(h);
}
a.onmouseleave=function(){
autoPlay();
}
// 定时器
function m(){
for(var i=0; i n[i].className=""; // console.log(i); } n[k].className="on"; } for(var i=0; i (function(i){ n[i].onclick=function(){ k=i; b.style.left=(k*-600)-600+'px' m() } })(i) } 三、简易轮播图 3.jpg
*{margin: 0; padding: 0; box-sizing: border-box;}
#banner{
width: 1000px;
height:300px;
position: relative;
margin: auto;
overflow: hidden;
border:solid 1px #000;
}
#imgs{
width:4020px;
height:300px;
left: 0;
position: absolute;
}
#banner img{
width:1000px;
height: 300px;
}
window.onload = function() {
var _imgs = document.querySelector("#imgs")
var _width = document.querySelector("#imgs img").offsetWidth
var bannerTimer = setInterval(play, 3000)
var _currLeft = 0; // 当前div#imgs 左侧距离
var _currIndex = 0; // 当前正在显示的图片
function play() {
// 通过计时器,修改图片索引和位置
var timer = setInterval(function() {
// 每20ms 向左移动20像素
_currLeft -= 20
if(_currLeft <= -3000) {
_currLeft = 0
// 直接显示第一张图片
_currIndex = 0
_imgs.style.left = 0
}
// 判断动画停止条件
if(_currLeft < -_currIndex * 1000) {
clearInterval(timer)
_currIndex++
if(_currIndex >= _imgs.length) {
_currIndex = 0
}
} else {
_imgs.style.left = _currLeft + "px"
}
}, 20)
}
}


