å¾çåæ¢ç¹æå®ç°å¾ç®åï¼èä¸å
¼å®¹æ§å¾å¥½ã
html页é¢å¦ä¸
å¤å¶ä»£ç 代ç å¦ä¸:
<div class="wrapper">
<div id="focus">
<ul>
<li><a href="
http://www.lanrentuku.com/" target="_blank"><img src="img/01.jpg" alt="QQååç¦ç¹å¾ææä¸è½½" /></a></li>
<li><a href="
http://www.lanrentuku.com/" target="_blank"><img src="img/02.jpg" alt="QQååç¦ç¹å¾æææç¨" /></a></li>
<li><a href="
http://www.lanrentuku.com/" target="_blank"><img src="img/03.jpg" alt="jqueryååç¦ç¹å¾ææ" /></a></li>
<li><a href="
http://www.lanrentuku.com/" target="_blank"><img src="img/04.jpg" alt="jqueryååç¦ç¹å¾ä»£ç " /></a></li>
<li><a href="
http://www.lanrentuku.com/" target="_blank"><img src="img/05.jpg" alt="jqueryååç¦ç¹å¾æºç " /></a></li>
</ul>
</div>
</div><!-- wrapper end -->
</body>
cssæ ·å¼
å¤å¶ä»£ç 代ç å¦ä¸:
<style type="text/css">
* {margin:0; padding:0;}
body {font-size:12px; color:#222; font-family:Verdana,Arial,Helvetica,sans-serif; background:#f0f0f0;}
.clearfix:after {content: "."; display: block; height: 0; clear: both; visibility: hidden;}
.clearfix {zoom:1;}
ul,li {list-style:none;}
img {border:0;}
.wrapper {width:800px; margin:0 auto; padding-bottom:50px;}
/* qqshop focus */
#focus {width:800px; height:280px; overflow:hidden; position:relative;}
#focus ul {height:380px; position:absolute;}
#focus ul li {float:left; width:800px; height:280px; overflow:hidden; position:relative; background:#000;}
#focus ul li div {position:absolute; overflow:hidden;}
#focus .btnBg {position:absolute; width:800px; height:20px; left:0; bottom:0; background:#000;}
#focus .btn {position:absolute; width:780px; height:10px; padding:5px 10px; right:0; bottom:0; text-align:right;}
#focus .btn span {display:inline-block; _display:inline; _zoom:1; width:25px; height:10px; _font-size:0; margin-left:5px; cursor:pointer; background:#fff;}
#focus .btn span.on {background:#fff;}
#focus .preNext {width:45px; height:100px; position:absolute; top:90px; background:url(img/sprite.png) no-repeat 0 0; cursor:pointer;}
#focus .pre {left:0;}
#focus .next {right:0; background-position:right top;}
</style>
jsèæ¬
å¤å¶ä»£ç 代ç å¦ä¸:
$(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.5);
//为å°æé®æ·»å é¼ æ æ»å
¥äºä»¶ï¼ä»¥æ¾ç¤ºç¸åºçå
容
$("#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.2).hover(function() {
$(this).stop(true,false).animate({"opacity":"0.5"},300);
},function() {
$(this).stop(true,false).animate({"opacity":"0.2"},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);
});
//æ¬ä¾ä¸ºå·¦å³æ»å¨ï¼å³ææliå
ç´ é½æ¯å¨åä¸æå左浮å¨ï¼æ以è¿ééè¦è®¡ç®åºå¤å´ulå
ç´ ç宽度
$("#focus ul").css("width",sWidth * (len));
//é¼ æ æ»ä¸ç¦ç¹å¾æ¶åæ¢èªå¨ææ¾ï¼æ»åºæ¶å¼å§èªå¨ææ¾
$("#focus").hover(function() {
clearInterval(picTimer);
},function() {
picTimer = setInterval(function() {
showPics(index);
index++;
if(index == len) {index = 0;}
},4000); //æ¤4000代表èªå¨ææ¾çé´éï¼åä½ï¼æ¯«ç§
}).trigger("mouseleave");
//æ¾ç¤ºå¾çå½æ°ï¼æ ¹æ®æ¥æ¶çindexå¼æ¾ç¤ºç¸åºçå
容
function showPics(index) { //æ®éåæ¢
var nowLeft = -index*sWidth; //æ ¹æ®indexå¼è®¡ç®ulå
ç´ çleftå¼
$("#focus ul").stop(true,false).animate({"left":nowLeft},300); //éè¿animate()è°æ´ulå
ç´ æ»å¨å°è®¡ç®åºçposition
//$("#focus .btn span").removeClass("on").eq(index).addClass("on"); //为å½åçæé®åæ¢å°éä¸çææ
$("#focus .btn span").stop(true,false).animate({"opacity":"0.4"},300).eq(index).stop(true,false).animate({"opacity":"1"},300); //为å½åçæé®åæ¢å°éä¸çææ
}
});
ç¨å°çå·¦å³åæ¢å¾ç