DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> JavaScript綜合知識 >> jQuery bxCarousel實現圖片滾動切換
jQuery bxCarousel實現圖片滾動切換
編輯:JavaScript綜合知識     

 BxCarousel是一個具有眾多配置且易用的Jquery圖片滾動插件,特征主要有:
◆ 可以指定顯示的元素總數
◆ 可以指定每次滾動的元素個數
◆ 自動播放模式
◆ 前一張/後一張按鈕控制圖片流動
參數含義
display_num:顯示元素的數量,幾張圖片
move:單擊左右控制鍵時,移動的元素個數,此處為移動2張圖片
prev_image:上一元素按鈕圖片
next_image:下一元素按鈕圖片
margin:圖片之間的間隙,一般設為10px
auto:自動滾動效果
controls:是否顯示左右控制按鈕,此處為false,表示不顯示左右控制按鈕
auto_hover:鼠標懸停到輪播區域時,是否停止圖片輪播
BxCarousel使用和配置
首先HTML代碼需要符合以下格式

復制代碼 代碼如下:
<ul>
<li>first piece of content</li>
<li>second piece of content</li>
<li>third piece of content</li>
<li>fourth piece of content</li>
<li>bxCarousel can accept an unlimited number of elements</li>
</ul>


jQuery代碼需要符合以下格式

 代碼如下:
$(document).ready(function(){
$('ul').bxCarousel({
display_num: 4, // number of elements to be visible
move: 4, // number of elements to the shift the slides
speed: 500, // number in milliseconds it takes to finish slide animation
margin:0, // right margin to be applied to each <li> element (in pixels, although do not include "px")
auto: false, // automatically play slides without a user click
auto_interval: 2000, // the amount of time in milliseconds between each auto animation
auto_dir: 'next', // direction of auto slideshow (options: 'next', 'prev')
auto_hover: false, // determines if the slideshow will stop when user hovers over slideshow
next_text: 'next', // text to be used for the 'next' control
next_image: '', // image to be used for the 'next' control
prev_text: 'prev', // text to be used for the 'prev' control
prev_image: '', // image to be used for the 'prev' control
controls: true // determines if controls will be displayed
});
});


div.bx_container和div.bx_wrap等html元素是js生成加上的,在使用bxCarousel插件時,為了達到視覺美感,要記得為div.bx_container和div.bx_wrap及其內部子元素設定需要的CSS Style。
還有注意的是,
bxCarousel是個無限循環機制,不停的點擊next按鈕看看html發生了什麼變化!!
bxCarousel不只針對圖片,對其他任何html元素均起作用。
如果開啟auto屬性,注意一定要保證speed屬性值小於持續時間。
實例代碼

代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" >
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="js/bxCarousel.js"></script>
<script type="text/javascript">
jQuery(function(){
jQuery('#demo1').bxCarousel({
display_num: 4, //
move: 1,
auto: true,
controls: false, //此處為false,表示不顯示左右箭頭控制按鈕
margin: 10,
auto_hover: true //鼠標懸停到輪播區域時,是否停止圖片輪播
});
jQuery('#demo2').bxCarousel({
display_num: 4,
move: 4,
margin: 10
});
});
</script>
<script type="text/javascript" src="./video/js/mootools.js"></script>
<script type="text/javascript" src="./video/js/swfobject.js"></script>
<script type="text/javascript" src="./video/js/videobox.js"></script>
<link rel="stylesheet" type="text/css" href="./video/css/videobox.css" >
<style type="text/css">
.pic, .vd{
width:200px;
height:200px;
margin:0 auto;
}
.pic a, .vd a{
display:block;
width:200px;
height:200px;
text-align:center;
margin:0 auto;
}
.pic{
background:url("http://i0.sinaimg.cn/home/2013/0513/U2727P30DT20130513082202.jpg") no-repeat scroll 0 0 transparent;
}
.vd{
background:url("http://i0.sinaimg.cn/home/2013/0512/U1345P30DT20130512114119.jpg") no-repeat scroll 0 0 transparent;
}
.nav,.main,.imgcollc ,.footer{
width:910px;
margin:0 auto;
text-align:center;
}
/*如果要使用方向按鈕導航,則需要設置.bx_wrap a.prev和.bx_wrap a.next的樣式。*/
a {color: #424242;text-decoration: none;}
ul,li,ol{padding:0;margin:0;list-style:none;}
.bx_wrap {margin-left: 30px; margin-top:10px;}
.bx_wrap ul img { border: 2px solid #ddd; }
.bx_wrap ul li{text-align:center;float:left;width:140px;height:119px;overflow:hidden;}
.bx_wrap ul li a:hover{text-decoration:none; color:#f30;}
.bx_wrap a.prev {width:20px;height:24px;line-height:24px;outline-style:none;outline-width: 0; position:absolute; top:45px; left:5px; text-indent:-999em; background: url(img/arr_left.gif) no-repeat;}
.bx_wrap a.next {width:20px;height:24px;line-height:24px; left:623px;position: absolute; top:45px; text-indent:-999em; background:url('img/arr_right.gif') no-repeat;}
.demo {width: 650px;height: 134px;margin: 40px auto;position: relative;border: 1px solid #d3d3d3;overflow:hidden;
}
</style>
</head>
<body>
<div class="nav">

</div>
<div class="main">
<h2 class="top_title"><a href="http://www.feiliu.com/">jQuery實現的視頻窗口伸縮、圖片滾動切換效果</a></h2>
<div class="pic">
<a onfocus="blur()" id="img" href="http://imgsrc.baidu.com/forum/pic/item/ed178044ad345982578b3fa90cf431adcaef84d7.jpg" rel="vidbox 640 376" title="test jpg"></a>
</div>
<div class="vd">
<a onfocus="blur()" id="vd" href="http://www.novelstudios.com/media/Visions_CellPhone.mov" rel="vidbox 640 376" title="test video"></a>
</div>
</div>
<div class="imgcollc">
<div class="demo">
<ul id="demo1">
<li><a href="#"><img alt="#" width="140" height="94" src="img/s1.jpg"><br/>圖片1</a></li>
<li><a href="#"><img alt="#" width="140" height="94" src="img/s2.jpg"><br/>圖片2</a></li>
<li><a href="#"><img alt="#" width="140" height="94" src="img/s3.jpg"><br/>圖片3</a></li>
<li><a href="#"><img alt="#" width="140" height="94" src="img/s4.jpg"><br/>圖片4</a></li>
<li><a href="#"><img alt="#" width="140" height="94" src="img/s5.jpg"><br/>圖片5</a></li>
<li><a href="#"><img alt="#" width="140" height="94" src="img/s6.jpg"><br/>圖片6</a></li>
<li><a href="#"><img alt="#" width="140" height="94" src="img/s7.jpg"><br/>圖片7</a></li>
<li><a href="#"><img alt="#" width="140" height="94" src="img/s1.jpg"><br/>圖片1</a></li>
<li><a href="#"><img alt="#" width="140" height="94" src="img/s2.jpg"><br/>圖片2</a></li>
<li><a href="#"><img alt="#" width="140" height="94" src="img/s3.jpg"><br/>圖片3</a></li>
<li><a href="#"><img alt="#" width="140" height="94" src="img/s4.jpg"><br/>圖片4</a></li>
<li><a href="#"><img alt="#" width="140" height="94" src="img/s5.jpg"><br/>圖片5</a></li>
<li><a href="#"><img alt="#" width="140" height="94" src="img/s6.jpg"><br/>圖片6</a></li>
<li><a href="#"><img alt="#" width="140" height="94" src="img/s7.jpg"><br/>圖片7</a></li>
</ul>
</div>
<div class="demo">
<ul id="demo2">
<li><a href="#"><img alt="#" width="140" height="94" src="img/s1.jpg"><br/>圖片1</a></li>
<li><a href="#"><img alt="#" width="140" height="94" src="img/s2.jpg"><br/>圖片2</a></li>
<li><a href="#"><img alt="#" width="140" height="94" src="img/s3.jpg"><br/>圖片3</a></li>
<li><a href="#"><img alt="#" width="140" height="94" src="img/s4.jpg"><br/>圖片4</a></li>
<li><a href="#"><img alt="#" width="140" height="94" src="img/s5.jpg"><br/>圖片5</a></li>
<li><a href="#"><img alt="#" width="140" height="94" src="img/s6.jpg"><br/>圖片6</a></li>
</ul>
</div>
</div>
<div class="footer">Mobile</div>
</body>
</html>


本實例中同時實現了燈箱效果。由於采用videobox插件,對於圖片和視頻都支持。很多燈箱效果的腳本和插件,比如jQuery Lightbox Plugin,Videobox, MooslideBox,,Shadowbox和 LightWindow 等等。
Videobox是一個只有6k大小的腳本,用於在頁面中顯示視頻。Videobox使用swfobject來嵌入Flash。視頻可以來自Youtube、Metacafe、Google Video、iFilm和自己設置的Flash。如果你要達到效果,必須使用videobox.js,mootools.js和swfobject腳.js這個三個腳本。

類似滾動滑動的插件還有bxSlider 等等,bxSlider是一個 jQuery 的插件,它可以實現 Slider 和滾動效果。這個插件使用非常簡單,並且大小只有 8kb,非常輕量級,所以非常適合在站點和博客中使用。

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