整理一些项目中常用的模块
常见的轮播图,具有自动切换 和 上下按钮切换功能, 包含三个部分, html css 和 js
html:
<div id="focus-banner">
<ul id="focus-banner-list">
<li>
<a href="#" class="focus-banner-img">
<img src="img/index/partner1.png" alt="">
</a>
</li>
<li>
<a href="#" class="focus-banner-img">
<img src="img/index/partner2.png" alt="">
</a>
</li>
</ul>
<a href="javascript:;" id="next-img" class="focus-handle"></a>
<a href="javascript:;" id="prev-img" class="focus-handle"></a>
<ul id="focus-bubble"></ul>
</div>
css:
/*轮播图样式*/
#focus-banner{
position: relative;
width: 100%;
overflow: hidden;
height: 420px;
}
#focus-banner-list{
position: relative;
}
#focus-banner-list li{
position: absolute;
left: 0;
top: 0;
width: 100%;
}
#focus-banner-list li img{
width: 100%;
}
.focus-banner-img{
display: block;
}
#focus-banner-list .focus-banner-text{
position: absolute;
left: 50%;
top: 0;
width: 1000px;
height: 100%;
margin-left: -500px;
text-align: center;
color: #fff;
font-size: 16px;
text-align: center;
}
#focus-banner .focus-handle{
position: absolute;
top: 50%;
margin-top: -40px;
z-index: 100;
display: block;
width: 80px;
height: 80px;
}
#next-img{
left: 0;
}
#next-img{
left: 50px;
background-position: 0 0;
}
#next-img:hover{
background-position: 0 -80px;
}
#prev-img{
right: 0;
}
#prev-img{
right: 50px;
background-position: -80px 0;
}
#prev-img:hover{
background-position: -80px -80px;
}
#focus-bubble{
position: absolute;
left: 50%;
bottom: 10px;
z-index: 999;
}
#focus-bubble li{
float: left;
width: 10px;
height: 10px;
margin: 0 5px;
border-radius: 50%;
-webkit-border-radius: 50%;
-moz-border-radius: 50%;
-o-border-radius: 50%;
-ms-border-radius: 50%;
border:1px #1137a5 solid;
cursor: pointer;
behavior: url(css/PIE.htc); /* 通知IE浏览器调用脚本作用于'box'类 */
}
#focus-bubble li.current{
background-color: #1137a5;
}
js:
<script>
$(function(){
var focusBanner=function(){
var x=30;
var $focusBanner=$("#focus-banner"),
$bannerList=$("#focus-banner-list li"),
$focusHandle=$(".focus-handle"),
$bannerImg=$(".focus-banner-img"),
$nextBnt=$("#next-img"),
$prevBnt=$("#prev-img"),
$focusBubble=$("#focus-bubble"),
bannerLength=$bannerList.length,
_index=0,
_timer="";
var _height=$(".focus-banner-img").find("img").height();
// $focusBanner.height(_height+x+"px");
$bannerImg.height(_height);
$(window).resize(function(){
window.location.reload()
}); // 浏览器窗口改变时 强制刷新当前窗口
for(var i=0; i<bannerLength; i++){
$bannerList.eq(i).css("zIndex",bannerLength-i);
$focusBubble.append("<li></li>");
}
//zIndex是js的z-index写法 默认图片是重叠堆放的 动态加载图片层级显示
$focusBubble.find("li").eq(0).addClass("current");
var bubbleLength=$focusBubble.find("li").length;
$focusBubble.css({
"width":bubbleLength*30,
"marginLeft":-bubbleLength*11,
});//初始化
$focusBubble.on("click","li",function(){
$(this).addClass("current").siblings().removeClass("current");
_index=$(this).index();
changeImg(_index);
});//点击轮换
$nextBnt.on("click",function(){
_index--
if(_index<0){
_index=bannerLength-1;
}
changeImg(_index);
});//下一张
$prevBnt.on("click",function(){
_index++;
if(_index>bannerLength-1){
_index=0;
}
changeImg(_index);
});//上一张
function changeImg(_index){
$bannerList.eq(_index).fadeIn(250);
$bannerList.eq(_index).siblings().fadeOut(200);
$focusBubble.find("li").removeClass("current");
$focusBubble.find("li").eq(_index).addClass("current");
clearInterval(_timer);//取消注释即可自动轮播
_timer=setInterval(function(){$nextBnt.click()},5000)//取消注释即可自动轮播
}//切换主函数
_timer=setInterval(function(){$nextBnt.click()},5000)//取消注释即可自动轮播
}();
});
</script>