DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> jQuery入門知識 >> JQuery特效代碼 >> Jquery實現圖片左右自動滾動示例
Jquery實現圖片左右自動滾動示例
編輯:JQuery特效代碼     
. 代碼如下:
<!DOCTYPE HTML>
<html>
<head>
<title>基於jQuery的控制左右滾動效果_自動滾動版本</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<style>

body{font-size:0.8em;letter-spacing:1px;font-family:"微軟雅黑"; line-height:1.8em}
div,ul,li,ol,img{margin:0;padding:0}
h1{font-size:1em; font-weight:normal;}
h1 a{background:#CFF; padding:2px 3px; text-decoration:none;}
h1 a:hover{background:#eee; text-decoration:underline}
h2,h3{font-size:1em; font-weight:normal; color:#a40000; margin:1em auto; position:relative}
h3{color:#888; font-weight:bold}
ul,li,ol{list-style:none}

.ibox{width:850px; height:210px; background:#ddd; margin:1em auto; border:1em solid #ddd; position:relative; overflow:hidden}
.ibox .showbox{position:absolute;left:0;width:2550px; height:170px; background:#fff}
.ibox .showbox ul li{float:left;margin:7px}
.ibox .showbox ul li img{width:150px; height:150px; border:3px solid #a40000}
.ibox span{position:absolute;padding:2px 5px; background:#fff; cursor:pointer;}
.ibox span.pre{left:10px; bottom:0}
.ibox span.next{right:10px; bottom:0}
.ibox span.num{right:45%; bottom:0; background:#ddd;line-height:18px;}
.ibox span.num li{display:inline;margin:0 5px; padding:1px 5px; line-height:18px}
.ibox span.num .numcur{background:#a40000; color:#fff}
</style>
<script>
$(function(){
//@Mr.Think***變量
var $cur = 1;//初始化顯示的版面
var $i = 5;//每版顯示數
var $len = $('.showbox>ul>li').length;//計算列表總長度(個數)
var $pages = Math.ceil($len / $i);//計算展示版面數量
var $w = $('.ibox').width();//取得展示區外圍寬度
var $showbox = $('.showbox');
var $num = $('span.num li')
var $pre = $('span.pre')
var $next = $('span.next');
var $autoFun;
//@Mr.Think***調用自動滾動
autoSlide();
//@Mr.Think***向前滾動
$pre.click(function(){
if (!$showbox.is(':animated')) { //判斷展示區是否動畫
if ($cur == 1) { //在第一個版面時,再向前滾動到最後一個版面
$showbox.animate({
left: '-=' + $w * ($pages - 1)
}, 500); //改變left值,切換顯示版面,500(ms)為滾動時間,下同
$cur = $pages; //初始化版面為最後一個版面
}
else {
$showbox.animate({
left: '+=' + $w
}, 500); //改變left值,切換顯示版面
$cur--; //版面累減
}
$num.eq($cur - 1).addClass('numcur').siblings().removeClass('numcur'); //為對應的版面數字加上高亮樣式,並移除同級元素的高亮樣式
}
});
//@Mr.Think***向後滾動
$next.click(function(){
if (!$showbox.is(':animated')) { //判斷展示區是否動畫
if ($cur == $pages) { //在最後一個版面時,再向後滾動到第一個版面
$showbox.animate({
left: 0
}, 500); //改變left值,切換顯示版面,500(ms)為滾動時間,下同
$cur = 1; //初始化版面為第一個版面
}
else {
$showbox.animate({
left: '-=' + $w
}, 500);//改變left值,切換顯示版面
$cur++; //版面數累加
}
$num.eq($cur - 1).addClass('numcur').siblings().removeClass('numcur'); //為對應的版面數字加上高亮樣式,並移除同級元素的高亮樣式
}
});
//@Mr.Think***數字點擊事件
$num.click(function(){
if (!$showbox.is(':animated')) { //判斷展示區是否動畫
var $index = $num.index(this); //索引出當前點擊在列表中的位置值
$showbox.animate({
left: '-' + ($w * $index)
}, 500); //改變left值,切換顯示版面,500(ms)為滾動時間
$cur = $index + 1; //初始化版面值,這一句可避免當滾動到第三版時,點擊向後按鈕,出面空白版.index()取值是從0開始的,故加1
$(this).addClass('numcur').siblings().removeClass('numcur'); //為當前點擊加上高亮樣式,並移除同級元素的高亮樣式
}
});
//@Mr.Think***停止滾動
clearFun($showbox);
clearFun($pre);
clearFun($next);
clearFun($num);
//@Mr.Think***事件劃入時停止自動滾動
function clearFun(elem){
elem.hover(function(){
clearAuto();
}, function(){
autoSlide();
});
}
//@Mr.Think***自動滾動
function autoSlide(){
$next.trigger('click');
$autoFun = setTimeout(autoSlide, 4000);//此處不可使用setInterval,setInterval是重復執行傳入函數,這會引起第二次劃入時停止失效
}
//@Mr.Think***清除自動滾動
function clearAuto(){
clearTimeout($autoFun);
}
});
</script>
</head>

<body>
<div class="ibox">
<div class="showbox">
<ul>
<li><img src="http://www.poluoluo.com/jzxy/UploadFiles_333/201309/20130926193733985.jpg" alt="demo" title="demo" /></li>
<li><img src="http://www.poluoluo.com/jzxy/UploadFiles_333/201309/20130926193733149.jpg" alt="demo" title="demo" /></li>
<li><img src="http://www.poluoluo.com/jzxy/UploadFiles_333/201309/20130926193734354.jpg" alt="demo" title="demo" /></li>
<li><img src="http://www.poluoluo.com/jzxy/UploadFiles_333/201309/20130926193734597.jpg" alt="demo" title="demo" /></li>
<li><img src="http://www.poluoluo.com/jzxy/UploadFiles_333/201309/20130926193734597.jpg" alt="demo" title="demo" /></li>
<li><img src="http://www.poluoluo.com/jzxy/UploadFiles_333/201309/20130926193734767.jpg" alt="demo" title="demo" /></li>
<li><img src="http://www.poluoluo.com/jzxy/UploadFiles_333/201309/20130926193734775.jpg" alt="demo" title="demo" /></li>
<li><img src="http://www.poluoluo.com/jzxy/UploadFiles_333/201309/20130926193734903.jpg" alt="demo" title="demo" /></li>
<li><img src="http://www.poluoluo.com/jzxy/UploadFiles_333/201309/20130926193734519.jpg" alt="demo" title="demo" /></li>
<li><img src="http://www.poluoluo.com/jzxy/UploadFiles_333/201309/20130926193733149.jpg" alt="demo" title="demo" /></li>
<li><img src="http://www.poluoluo.com/jzxy/UploadFiles_333/201309/20130926193734597.jpg" alt="demo" title="demo" /></li>
<li><img src="http://www.poluoluo.com/jzxy/UploadFiles_333/201309/20130926193733149.jpg" alt="demo" title="demo" /></li>
<li><img src="http://www.poluoluo.com/jzxy/UploadFiles_333/201309/20130926193734531.jpg" alt="demo" title="demo" /></li>
</ul>
</div>
<span class="pre">«向左滾動</span>
<span class="next">向右滾動»</span>

</div>
</body>
</html>
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved