jQuery滾動新聞實現(xiàn)代碼
網(wǎng)上下載了一個Jquery新聞滾動Demo,效果不錯。我根據(jù)自己情況,做了些調(diào)整。
下載后,除了Html及Jquery.js,還有三個文件,分別是Css,主Js和一個定位Js(jquery.dimensions.js),Css文件完全可以通過動態(tài)賦值實現(xiàn),省的在Css文件中添加了,況且可以重定義,應該效果更好。
定位Js只用到一個方法,我把它整合到之中了,少加載一個是一個么。
原Demo是一行顯示的滾動效果,有一個Bug,即不等的多行顯示時會跑錯,已修復。
原Demo有一個mouseover屬性,我一般不用(效果不好,看一下實現(xiàn)方法也有漏洞,在多行時效果更差),刪除了。
借鑒別人的思路與代碼,不敢獨享。
刪除的部分:
$('> ul', this) .bind('mouseover', function(e) { if ($(e.target).is('li')) { $(e.target).addClass('hover'); } }) .bind('mouseout', function(e) { if ($(e.target).is('li')) { $(e.target).removeClass('hover'); } });
調(diào)整后代碼:
html部分:
<div id="tbNews"> <ul> <li>1、滾動新聞,滾動新聞,滾動新聞,滾動新聞,滾動新聞</li> <li>2、滾動新聞,滾動新聞</li> <li>3、滾動新聞,滾動新聞,滾動新聞,滾動新聞</li> <li>4、滾動新聞,滾動新聞,滾動新聞,滾動新聞,滾動新聞</li> <li>5、滾動新聞</li> </ul> </div> <script language="JavaScript" src="jdNewsScroll.js" type="text/javascript"> </script> <script defer="defer" language="JavaScript" type="text/javascript"> $(function() { $('#tbNews').jdNewsScroll({divWidth:130,divHeight:50,fontSize:'10.5pt'}); }); </script>
Js代碼:
(function($){ var ELMS = []; $.fn.jdNewsScroll = function(settings) { settings = $.extend({}, arguments.callee.defaults, settings); $(this).css({"position":"relative","overflow":"hidden","width":settings.divWidth,"height":settings.divHeight}); $(this).find("ul").css({"position":"relative","list-style-type":"none","font-size":settings.fontSize,"margin":"0px"}); $(this).find("li").css({"line-height":"130%","margin":"0px","padding":"2px 10px 1px 10px"}); $(this).each(function(){ this.$settings = settings; this.$pause = false; this.$counter = settings.beginTime; $(this).hover(function(){ $(this).jdNewsScrollPause(true) }, function(){ $(this).jdNewsScrollPause(false) }); ELMS.push(this); }); return this; }; $.fn.jdNewsScroll.defaults = { beginTime: 10, fontSize: '9pt', divWidth: '100%', divHeight: '200px', lineHeight: '130%', delay: 20, step: 2 }; $.fn.jdNewsScrollPause = function(pause) { return this.each(function() { this.$pause = pause; }); } function outerHeight(options) { if (!this[0]) 0; options = $.extend({ margin: false }, options || {}); return this[0] == window || this[0] == document ? this.height() : this.is(':visible') ? this[0].offsetHeight + (options.margin ? (num(this, 'marginTop') + num(this, 'marginBottom')) : 0) : this.height() + num(this,'borderTopWidth') + num(this, 'borderBottomWidth') + num(this, 'paddingTop') + num(this, 'paddingBottom') + (options.margin ? (num(this, 'marginTop') + num(this, 'marginBottom')) : 0); } setInterval(scroll, 80); function scroll() { for (var i = 0; i < ELMS.length; i++) { var elm = ELMS[i]; if (elm && !elm.$pause) { if (elm.$counter == 0) { var ul = $('> ul', elm)[0]; if (!elm.$steps) { elm.$steps = $('> li:first-child', ul).outerHeight(); elm.$step = 0; } if ((elm.$steps + elm.$step) <= 0) { elm.$counter = elm.$settings.delay; elm.$steps = false; $(ul).css('top', '0').find('> li:last-child').after($('> li:first-child', ul)); $('> *', ul).not('li').remove(); } else { elm.$step -= elm.$settings.step; if (-elm.$step > elm.$steps) { elm.$step = -elm.$steps; } ul.style.top = elm.$step + 'px'; } } else { elm.$counter--; } } } }; })(jQuery);
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- JQuery獲取元素文檔大小、偏移和位置和滾動條位置的方法集合
- 基于Jquery的文字滾動跑馬燈插件(一個頁面多個滾動區(qū))
- 基于jQuery的圖片左右無縫滾動插件
- 六款幫助你實現(xiàn)驚艷視差滾動效果的jQuery插件
- js/jquery獲取瀏覽器窗口可視區(qū)域高度和寬度以及滾動條高度實現(xiàn)代碼
- jQuery創(chuàng)建平滑的頁面滾動(頂部或底部)
- jquery無縫向上滾動實現(xiàn)代碼
- jquery easyui滾動條部分設置介紹
- jQuery scroll事件實現(xiàn)監(jiān)控滾動條分頁示例
- JQuery插件iScroll實現(xiàn)下拉刷新,滾動翻頁特效
相關文章
詳談jQuery.load()和Jsp的include的區(qū)別
下面小編就為大家?guī)硪黄斦刯Query.load()和Jsp的include的區(qū)別。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04jQuery中select與datalist制作下拉菜單時的區(qū)別淺析
一般我們通常使用select制作下拉菜單,但是H5之后,datalist也可以充當select的角色,而且兩者還有一點小的不同。具體區(qū)別詳解小編通過本文給簡單介紹下2016-12-12jQuery length 和 size()區(qū)別總結
這篇文章主要介紹了jQuery length 和 size()區(qū)別總結,在這里length是屬性,size()是方法,需要的朋友可以參考下2018-04-04jQuery實現(xiàn)單擊彈出Div層窗口效果(可關閉可拖動)
這篇文章主要介紹了jQuery實現(xiàn)單擊彈出Div層窗口效果,具有可關閉可拖動的功能,涉及jQuery響應鼠標事件動態(tài)變換頁面元素樣式的實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-09-09使用jQuery將多條數(shù)據(jù)插入模態(tài)框的實現(xiàn)代碼
這篇文章主要介紹了使用jQuery將多條數(shù)據(jù)插入模態(tài)框的方法,很簡單,很實用,需要的朋友可以參考下2014-10-10jQuery移動頁面開發(fā)中的觸摸事件與虛擬鼠標事件簡介
這篇文章主要介紹了jQuery移動頁面開發(fā)中的觸摸事件與虛擬鼠標事件的簡單編寫方法,jQuery是當今人氣最高的Javascript庫并被廣泛應用于移動web的開發(fā),需要的朋友可以參考下2015-12-12