jQuery實(shí)現(xiàn)列表自動滾動循環(huán)滾動展示新聞
需要在頁面中一個小的區(qū)域循環(huán)滾動展示新聞(公告、活動、圖片等等),并且,鼠標(biāo)懸停時停止?jié)L動并提示,離開后,繼續(xù)滾動。
效果圖:
上干貨
html:
<div id="news">
<ul>
<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" title="aaaaaaaaaaaaaaa">aaaaaaaaaaaaaaa</a></li>
<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" title="bbbbbbbbbbbbbbb">bbbbbbbbbbbbbbb</a></li>
<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" title="ccccccccccccccc">ccccccccccccccc</a></li>
<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" title="ddddddddddddddd">ddddddddddddddd</a></li>
<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" title="eeeeeeeeeeeeeee">eeeeeeeeeeeeeee</a></li>
<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" title="fffffffffffffff">fffffffffffffff</a></li>
<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" title="ggggggggggggggg">ggggggggggggggg</a></li>
</ul>
</div>
css:
ui,li {
list-style: none;
}
#news{
height: 75px;
overflow: hidden;
}
關(guān)鍵是js文件:
$(function() {
var $this = $("#news");
var scrollTimer;
$this.hover(function() {
clearInterval(scrollTimer);
}, function() {
scrollTimer = setInterval(function() {
scrollNews($this);
}, 2000);
}).trigger("mouseleave");
function scrollNews(obj) {
var $self = obj.find("ul");
var lineHeight = $self.find("li:first").height();
$self.animate({
"marginTop": -lineHeight + "px"
}, 600, function() {
$self.css({
marginTop: 0
}).find("li:first").appendTo($self);
})
}
})
主要就是對hover、setInterval、clearInterval、animate這些方法以及marginTop屬性(marginLeft、top、left等等)的理解和運(yùn)用,需要注意的是,如果不加.trigger("mouseleave"),在網(wǎng)頁初始化的時候列表不會滾動,還有appendTo能直接移動元素,就這些了。
相關(guān)文章
jQuery 頁面載入進(jìn)度條實(shí)現(xiàn)代碼
頁面 Loading 條基本人人都會用。它的原理很簡單:在頁頭放置一個文字或者圖片的 loading 狀態(tài),然后頁尾載入一段 JS 隱藏掉,即根據(jù)瀏覽器的載入順序來實(shí)現(xiàn)的簡易 Loading 狀態(tài)條。2009-02-02jquery實(shí)現(xiàn)的藍(lán)色二級導(dǎo)航條效果代碼
這篇文章主要介紹了jquery實(shí)現(xiàn)的藍(lán)色二級導(dǎo)航條效果代碼,涉及jquery鼠標(biāo)事件及頁面樣式的動態(tài)切換效果實(shí)現(xiàn)技巧,非常簡單實(shí)用,需要的朋友可以參考下2015-08-08jquery異步循環(huán)獲取功能實(shí)現(xiàn)代碼
頁面html的repeater控件中有一個span,需要根據(jù)指定ID異步獲取相關(guān)信息。2010-09-09深入理解Jquery表單驗(yàn)證(使用formValidator)
表單驗(yàn)證在web中中的應(yīng)用很廣,本篇文章主要介紹了Jquery表單驗(yàn)證(使用formValidator),非常具有實(shí)用價值,需要的朋友可以參考下。2017-01-01jQuery實(shí)現(xiàn)類似滑動門切換效果的層切換
滑動門切換效果想必大家都有見到過吧,在本文為大家介紹下jQuery是如何實(shí)現(xiàn)層切換的2013-09-09利用JQuery操作iframe父頁面、子頁面的元素和方法匯總
這篇文章主要給大家介紹了關(guān)于利用JQuery操作iframe父頁面、子頁面的元素和方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09jQuery基于json與cookie實(shí)現(xiàn)購物車的方法
這篇文章主要介紹了jQuery基于json與cookie實(shí)現(xiàn)購物車的方法,涉及jQuery操作json格式數(shù)據(jù)與cookie存儲購物車信息的相關(guān)技巧,需要的朋友可以參考下2016-04-04boxy基于jquery的彈出層對話框插件擴(kuò)展應(yīng)用 彈出層選擇器
當(dāng)大家進(jìn)行復(fù)雜功能設(shè)計(jì)的時候,在對多級聯(lián)選擇進(jìn)行設(shè)計(jì),為了獲得更好的用戶體驗(yàn)和節(jié)省頁面空間,往往會使用彈出層的方法。2010-11-11