您的位置:首页 > Web前端 > JQuery

jquery控制图片切换

2014-04-03 21:02 519 查看
这种js现在用的很多。同时网上的js代码页很多。我直接从网上当了一个来用;代码如下:

html

<div class="bannerbox">
<div id="focus">
<ul>
<li><a href="#" target="_blank">
<img src="../images/index_18.gif" alt="" /></a></li>
<li><a href="#" target="_blank">
<img src="../images/index_18.gif" alt="" /></a></li>
<li><a href="#" target="_blank">
<img src="../images/index_18.gif" alt="" /></a></li>
<li><a href="#" target="_blank">
<img src="../images/index_18.gif" alt="" /></a></li>
</ul>
</div>
</div>

css:

.bannerbox { width: 970px; height: 447px; overflow: hidden; margin: 0px auto; }
#focus { border:none;width: 970px; height: 447px; clear: both; overflow: hidden; position: relative; float: left;z-index: 10 }
#focus ul { width: 970px; height: 447px; float: left; position: absolute; clear: both; padding: 0px; margin: 0px; border: none}
#focus ul li { float: left; width: 970px; height: 447px; overflow: hidden; position: relative; padding: 0px; margin:0;border: none }
#focus .preNext { width: 550px; height: 440px; position: absolute; top: 2px; cursor: pointer;border: none;}
#focus .pre { left: 0; background: url(../images/sprite.png) no-repeat left center;z-index: 20;border: none; }
#focus .next { right: 0; background: url(../images/sprite1.png) no-repeat right center;z-index: 20;border: none; }

jquery:

$(function () {
var sWidth = $("#focus").width();
var len = $("#focus ul li").length;
var index = 0;
var picTimer;
var btn = "<div class='btnBg'></div><div class='btn'>";
for (var i = 0; i < len; i++) {
btn += "<span></span>";
}
btn += "</div><div class='preNext pre'></div><div class='preNext next'></div>";
$("#focus").append(btn);
$("#focus .btnBg").css("opacity", 0);
$("#focus .btn span").css("opacity", 0.4).mouseenter(function () {
index = $("#focus .btn span").index(this);
showPics(index);
}).eq(0).trigger("mouseenter");
$("#focus .preNext").css("opacity", 0.0).hover(function () {
$(this).stop(true, false).animate({ "opacity": "0.5" }, 300);
}, function () {
$(this).stop(true, false).animate({ "opacity": "0" }, 300);
});
$("#focus .pre").click(function () {
index -= 1;
if (index == -1) { index = len - 1; }
showPics(index);
});
$("#focus .next").click(function () {
index += 1;
if (index == len) { index = 0; }
showPics(index);
});
$("#focus ul").css("width", sWidth * (len));
$("#focus").hover(function () {
clearInterval(picTimer);
}, function () {
picTimer = setInterval(function () {
showPics(index);
index++;
if (index == len) { index = 0; }
}, 2800);
}).trigger("mouseleave");
function showPics(index) {
var nowLeft = -index * sWidth;
$("#focus ul").stop(true, false).animate({ "left": nowLeft }, 300);
$("#focus .btn span").stop(true, false).animate({ "opacity": "0.4" }, 300).eq(index).stop(true, false).animate({ "opacity": "1" }, 300);
}
});

以上代码经测试在ie9以上,ff,chroom下运行正常。代码参考懒人图库
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: