jQuery实现选项联动轮播效果【附实例】
<!doctypehtml> <htmllang="en"> <head> <metacharset="UTF-8"> <metaname="Generator"content="EditPlus®"> <metaname="Author"content=""> <metaname="Keywords"content=""> <metaname="Description"content=""> <linkrel="stylesheet"href="css/baner.css"> <scriptsrc="js/jquery-1.11.3.js"></script> <scriptsrc="js/index.js"></script> <title>JQ轮播三级连锁</title> </head> <body> <divclass="parent"> <divclass="top"> <p> <ahref="">创意空间</a> <ahref="">永恒的爱</a> <ahref="">浪漫真情</a> <ahref="">珍贵独特</a> </p> </div> <divclass="fours"> <ahref=""><imgsrc="img/1.jpg"alt=""></a> <ahref=""><imgsrc="img/2.jpg"alt=""></a> <ahref=""><imgsrc="img/3.jpg"alt=""></a> <ahref=""><imgsrc="img/4.jpg"alt=""></a> </div> </div> </body> </html>
CSS
*{
border:none;
margin:0;
padding:0;
list-style:none;
outline:none;
}
html,body{
width:100%;
height:100%;
}
/*方法二*/
body{
display:flex;
align-items:center;/****水平居中****/
justify-content:center;/*垂直居中*/
}
.parent{
width:750px;
height:400px;
/*方法一*/
/*margin:0auto;*/
/*position:relative;*/
/*top:50%;*/
/*margin-top:-200px;*/
}
/*轮播*/
.topp{
width:90%;
margin:0auto;
}
.toppa{
display:inline-block;
line-height:30px;
width:23%;
padding:10px0;
text-align:center;
text-decoration:none;
border:2pxsolidtransparent;
color:slategray;
}
.toppa.selected{
border:2pxsolid#e4393c;
color:#e4393c;
}
/*图片*/
.fours{
width:650px;
margin:0auto;
height:300px;
position:relative;
margin-top:30px;
}
.foursa{
position:absolute;
}
JS
定义变量和定时器,变量等价于eq(index)中index,自动轮播是可以的,关键在于,鼠标进入选项卡区域时候,怎么对应想要的轮播画面,
方法:停止定时器,获取当前选项卡下标,匹配对应的轮播画面显示出来。
vara=0;
vart=null;
$(function(){
$('.fours>a:not(:first-child)').hide();
t=setInterval("autoMove()",2000);
//鼠标进入轮播停止
$('.parent').hover(function(){clearInterval(t)},function(){t=setInterval("autoMove()",2000);});
//当鼠标进去对应选项时候图片对应变化
$(".topp>a").hover(function(){
clearInterval(t);
varnum=$(this).index();
showThis(num);
//console.log(num);
})
});
functionautoMove(){
a++;
if(a>=4){
a=0;
}
play(a);
}
functionplay(a){
$('.fours>a').filter(":visible").fadeOut(500).parent().children().eq(a).fadeIn(1000);
$('.toppa').eq(a).addClass("selected").siblings().removeClass("selected");
}
//鼠标进入的情况
functionshowThis(sum){
$(".fours>a").eq(sum).fadeIn(1000).siblings().fadeOut(500);
$(".toppa").eq(sum).addClass("selected").siblings().removeClass("selected");
}
以上这篇jQuery实现选项联动轮播效果【附实例】就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。