jQuery實現(xiàn)的無縫廣告圖片左右滾動功能詳解
本文實例講述了jQuery實現(xiàn)的無縫廣告圖片左右滾動功能。分享給大家供大家參考,具體如下:
先是寫了一個此功能的jQuery插件,但是一時寫不出如何使用鼠標(biāo)進行滾動方向的切換,于是又寫了另一個可以實現(xiàn)切換的版本...
原理:
1.把滾動的內(nèi)容復(fù)制2份放到原內(nèi)容左右,這樣無論向左右滾動都不會出現(xiàn)斷掉的情況
2.改變內(nèi)容樣式的left值實現(xiàn)滾動效果,向左或向右滾動一個內(nèi)容的長度后,還原并繼續(xù)滾動,這樣看起來就像無縫一直滾動的樣子了(視覺上的效果,^_^)
3.鼠標(biāo)放上去則clearInterval,移開后繼續(xù)setInterval
4.移動效果寫成一個方法,切換方向時調(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擴展技巧總結(jié)》、《jQuery常見經(jīng)典特效匯總》、《jQuery動畫與特效用法總結(jié)》、《jquery選擇器用法總結(jié)》及《jQuery常用插件及用法總結(jié)》
希望本文所述對大家jQuery程序設(shè)計有所幫助。
相關(guān)文章
jquery實現(xiàn)的回旋滾動效果完整實例【附demo源碼下載】
這篇文章主要介紹了jquery實現(xiàn)的回旋滾動效果,可實現(xiàn)點擊后側(cè)圖片呈現(xiàn)立體翻轉(zhuǎn)切換的功能,涉及jQuery插件roundabout.js的使用,并附帶了完整實例demo源碼供讀者下載參考,需要的朋友可以參考下2016-09-09基于jQuery的為attr添加id title等效果的實現(xiàn)代碼
下面的例子是通過jquery為連接增加title描述的代碼,在當(dāng)前頁的連接上寫上,你好,現(xiàn)在在試驗連接文字的簡單描述。2011-04-04JQuery的Ajax請求實現(xiàn)局部刷新的簡單實例
本篇文章只要是對JQuery的Ajax請求實現(xiàn)局部刷新的簡單實例進行了詳細(xì)的介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-02-02jQuery加PHP實現(xiàn)圖片上傳并提交的示例代碼
這篇文章主要介紹了jQuery加PHP實現(xiàn)圖片上傳并提交的實例,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07jquery的ajaxSubmit()異步上傳圖片并保存表單數(shù)據(jù)演示代碼
使用jquery的ajaxSubmit()異步上傳圖片的捅死實現(xiàn)保存表單數(shù)據(jù),具體演示代碼如下,感興趣的朋友可以參考下哈,希望對大家有所幫助2013-06-06