jQuery自動或手動圖片切換效果
學(xué)習(xí)JS的時候本來積攢了很多有趣的小例子,但是苦于沒有找到一些好的平臺來展示這些JS效果。今天發(fā)現(xiàn)了RunJS這個分享代碼的平臺,迫不及待得想跟大家分享。
在瀏覽各大商城網(wǎng)站的時候,或者某些網(wǎng)站的首頁,都會展示與本網(wǎng)站相關(guān)的一些實時切換的圖片, 本文就給大家分享一個用jQuery實現(xiàn)圖片自動切換的例子。
主頁核心代碼(Default.aspx):
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jQuery實現(xiàn)圖片切換</title>
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="js/tupianqiehuan.js"></script>
<link rel="stylesheet" type="text/css" href="css/tupianqiehuan.css">
</head>
<body>
<div class="wrapper">
<h1>jquer實現(xiàn)圖片切換</h1>
<div id="focus">
<ul>
<li><a href="#" target="_blank"><img src="images/01.jpg" alt="" /></a></li>
<li><a href="#" target="_blank"><img src="images/02.jpg" alt="" /></a></li>
<li><a href="#" target="_blank"><img src="images/03.jpg" alt="" /></a></li>
<li><a href="#" target="_blank"><img src="images/04.jpg" alt="" /></a></li><span style="white-space:pre"> </ul>
</div>
</div>
</body>
</html>
CSS代碼(tupianqiehuan.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;}
h1{height:50px;line-height:50px;font-size:22px;font-weight:normal;font-family:"Microsoft YaHei",SimHei;margin-bottom:20px;}
/* 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(http://sandbox.runjs.cn/uploads/rs/475/xaqlrnnr/sprite.png) no-repeat 0 0;cursor:pointer;}
#focus .pre{left:0;}
#focus .next{right:0;background-position:right top;}
JS代碼(tupianqiehuan.js):
$(function() {
var sWidth = $("#focus").width(); //獲取焦點圖的寬度(顯示面積)
var len = $("#focus ul li").length; //獲取焦點圖個數(shù)
var index = 0;
var picTimer;
//以下代碼添加數(shù)字按鈕和按鈕后的半透明條,還有上一頁、下一頁兩個按鈕
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);
//為小按鈕添加鼠標(biāo)滑入事件,以顯示相應(yīng)的內(nèi)容
$("#focus .btn span").css("opacity",0.4).mouseover(function() {
index = $("#focus .btn span").index(this);
showPics(index);
}).eq(0).trigger("mouseover");
//上一頁、下一頁按鈕透明度處理
$("#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);
});
//本例為左右滾動,即所有l(wèi)i元素都是在同一排向左浮動,所以這里需要計算出外圍ul元素的寬度
$("#focus ul").css("width",sWidth * (len));
//鼠標(biāo)滑上焦點圖時停止自動播放,滑出時開始自動播放
$("#focus").hover(function() {
clearInterval(picTimer);
},function() {
picTimer = setInterval(function() {
showPics(index);
index++;
if(index == len) {index = 0;}
},4000); //此4000代表自動播放的間隔,單位:毫秒
}).trigger("mouseleave");
//顯示圖片函數(shù),根據(jù)接收的index值顯示相應(yīng)的內(nèi)容
function showPics(index) { //普通切換
var nowLeft = -index*sWidth; //根據(jù)index值計算ul元素的left值
$("#focus ul").stop(true,false).animate({"left":nowLeft},300); //通過animate()調(diào)整ul元素滾動到計算出的position
$("#focus .btn span").stop(true,false).animate({"opacity":"0.4"},300).eq(index).stop(true,false).animate({"opacity":"1"},300);//為當(dāng)前的按鈕切換到選中的效果
}
});
具體效果可以參看 效果演示
這里先給大家一個開胃菜,后面會抽時間簡單說一下如何在 RunJS上發(fā)布自己的小程序以及使用RunJS的一些小技巧。
相關(guān)文章
JQuery獲取可視區(qū)尺寸和文檔尺寸及制作懸浮菜單示例
這篇文章主要介紹了JQuery獲取可視區(qū)尺寸和文檔尺寸及制作懸浮菜單,涉及jQuery針對頁面元素屬性動態(tài)操作相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2019-05-05
jquery click([data],fn)使用方法實例介紹
大概意思就是觸發(fā)每一個匹配元素的click事件,本文通過一個實例為大家詳細介紹下jquery click([data],fn)的使用方法,感興趣的朋友可以參考下哈,希望對大家有所幫助2013-07-07
用jquery實現(xiàn)學(xué)校的校歷(asp.net+jquery ui 1.72)
學(xué)校日歷有以下幾個特點:1顯示本周是本學(xué)期的第幾周;2顯示今日事件;3特殊顯示有事件的日期,點擊該日期顯示本日事件。2010-01-01

