jQuery實(shí)現(xiàn)的無(wú)縫廣告圖片左右滾動(dòng)功能詳解
本文實(shí)例講述了jQuery實(shí)現(xiàn)的無(wú)縫廣告圖片左右滾動(dòng)功能。分享給大家供大家參考,具體如下:
先是寫(xiě)了一個(gè)此功能的jQuery插件,但是一時(shí)寫(xiě)不出如何使用鼠標(biāo)進(jìn)行滾動(dòng)方向的切換,于是又寫(xiě)了另一個(gè)可以實(shí)現(xiàn)切換的版本...
原理:
1.把滾動(dòng)的內(nèi)容復(fù)制2份放到原內(nèi)容左右,這樣無(wú)論向左右滾動(dòng)都不會(huì)出現(xiàn)斷掉的情況
2.改變內(nèi)容樣式的left值實(shí)現(xiàn)滾動(dòng)效果,向左或向右滾動(dòng)一個(gè)內(nèi)容的長(zhǎng)度后,還原并繼續(xù)滾動(dòng),這樣看起來(lái)就像無(wú)縫一直滾動(dòng)的樣子了(視覺(jué)上的效果,^_^)
3.鼠標(biāo)放上去則clearInterval,移開(kāi)后繼續(xù)setInterval
4.移動(dòng)效果寫(xiě)成一個(gè)方法,切換方向時(shí)調(diào)用一次即可
<style>
* { margin:0; padding:0;}
ul { list-style:none; margin:0; padding:0;}
img { border:none;}
.bar {
margin:0 auto;
width:800px; height:48px; overflow:hidden;
line-height:48px; border:2px solid #EEE;}
.bar a.a_left,
.bar a.a_right{
float:left;
width:11px; height:48px;
background:url(a_left.png) no-repeat left center;}
.bar a.a_right { float:right; background-image:url(a_right.png);}
.bar_wrap { float:left; position:relative; width:776px; height:48px; white-space:nowrap; overflow:hidden;}
.bar_inner { position:relative; height:48px; line-height:48px; left:0; width:2880px; white-space:nowrap;}
.bar_inner ul { height:48px; overflow:hidden; float:left; width:960px;}
.bar_inner ul li{ float:left;}
.bar_inner ul li a{ padding:0 16px; display:block; height:48px; line-height:48px;}
</style>
<body>
<div class="bar">
<a href="#" class="a_left"></a>
<div class="bar_wrap">
<div class="bar_inner">
<ul>
<li><a href="#" title="" ><img src="pic.png" alt="" /></a></li>
<li><a href="#" title="" ><img src="pic.png" alt="" /></a></li>
<li><a href="#" title="" ><img src="pic.png" alt="" /></a></li>
<li><a href="#" title="" ><img src="pic.png" alt="" /></a></li>
<li><a href="#" title="" ><img src="pic.png" alt="" /></a></li>
<li><a href="#" title="" ><img src="pic.png" alt="" /></a></li>
<li><a href="#" title="" ><img src="pic.png" alt="" /></a></li>
<li><a href="#" title="" ><img src="pic.png" alt="" /></a></li>
<li><a href="#" title="" ><img src="pic.png" alt="" /></a></li>
<li><a href="#" title="" ><img src="pic.png" alt="" /></a></li>
</ul> </div>
</div>
<a href="#" class="a_right"></a>
</div>
var scrollBar = function(){
this.step = 14;
this.speed = 100;
this.inner = $(".bar_inner");
this.wrap = $(".bar_wrap");
this.ini = 0;
this.pos = "l";
this.s ;
}
scrollBar.prototype = {
check : function(){
return this.inner.width() < this.wrap.width() ? false : true;
} ,
init : function(){
if( this.check() ){
this.inner
.html( this.inner.html() + this.inner.html() + this.inner.html() )
.css("left",- this.inner.width()/3 + "px");
}
},
run : function(pos){
if (! this.check()){ return;}
if( this.ini == 0) {this.init();}
this.ini = 1;
this.pos = pos;
var that = this;
var f = function(){
if(that.pos == "l"){
var l = parseInt( that.inner.css("left") ) - that.step;
that.inner.css("left",l + "px");
//console.log(l);
if ( parseInt(that.inner.css("left")) <= -( that.inner.width()/ 3 * 2) ){
that.inner.css("left",- that.inner.width() /3 + "px");
}
}
else {
var l = parseInt( that.inner.css("left") ) + that.step;
that.inner.css("left",l + "px");
//console.log( l );
if( parseInt(that.inner.css("left")) >= 0 ){
that.inner.css("left", - that.inner.width()/3 + "px");
}
}
}
if(this.s) {clearInterval(that.s);};
this.s = setInterval( f ,that.speed);
that.inner.hover(
function(){ clearInterval(that.s);},
function(){clearInterval(that.s); that.s = setInterval( f ,that.speed); }
)
}
}
var s = new scrollBar();
s.run("r");
$(".a_left").mouseover(function(){
clearInterval( s.s);
s.run("l");
})
$(".a_right").mouseover(function(){
clearInterval( s.s);
s.run("r");
})
更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jQuery圖片操作技巧大全》、《jQuery切換特效與技巧總結(jié)》、《jQuery拖拽特效與技巧總結(jié)》、《jQuery擴(kuò)展技巧總結(jié)》、《jQuery常見(jiàn)經(jīng)典特效匯總》、《jQuery動(dòng)畫(huà)與特效用法總結(jié)》、《jquery選擇器用法總結(jié)》及《jQuery常用插件及用法總結(jié)》
希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。
相關(guān)文章
jquery實(shí)現(xiàn)的回旋滾動(dòng)效果完整實(shí)例【附demo源碼下載】
這篇文章主要介紹了jquery實(shí)現(xiàn)的回旋滾動(dòng)效果,可實(shí)現(xiàn)點(diǎn)擊后側(cè)圖片呈現(xiàn)立體翻轉(zhuǎn)切換的功能,涉及jQuery插件roundabout.js的使用,并附帶了完整實(shí)例demo源碼供讀者下載參考,需要的朋友可以參考下2016-09-09
基于jQuery的為attr添加id title等效果的實(shí)現(xiàn)代碼
下面的例子是通過(guò)jquery為連接增加title描述的代碼,在當(dāng)前頁(yè)的連接上寫(xiě)上,你好,現(xiàn)在在試驗(yàn)連接文字的簡(jiǎn)單描述。2011-04-04
JQuery的Ajax請(qǐng)求實(shí)現(xiàn)局部刷新的簡(jiǎn)單實(shí)例
本篇文章只要是對(duì)JQuery的Ajax請(qǐng)求實(shí)現(xiàn)局部刷新的簡(jiǎn)單實(shí)例進(jìn)行了詳細(xì)的介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-02-02
jQuery加PHP實(shí)現(xiàn)圖片上傳并提交的示例代碼
這篇文章主要介紹了jQuery加PHP實(shí)現(xiàn)圖片上傳并提交的實(shí)例,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07
jquery的ajaxSubmit()異步上傳圖片并保存表單數(shù)據(jù)演示代碼
使用jquery的ajaxSubmit()異步上傳圖片的捅死實(shí)現(xiàn)保存表單數(shù)據(jù),具體演示代碼如下,感興趣的朋友可以參考下哈,希望對(duì)大家有所幫助2013-06-06

