DIV CSS 佈局教程網

jQuery 滾動新聞實現代碼分享
編輯:關於JavaScript     

網上下載的 jQuery 新聞滾動 Demo 效果不錯,但直接套用時有些細節需要調整。本文記錄了在原 Demo 基礎上做的幾處改進,並附上完整代碼。

主要改進點包括:CSS 完全可以透過動態賦值實現,方便重定義;定位用到的 jquery.dimensions.js 只用到一個方法,將其整合進主 JS 中,少加載一個文件;原 Demo 在多行顯示時會跑錯的 Bug 已修復;同時移除了效果不佳且存在漏洞的 mouseover 屬性。

刪除的部分:

$('> 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');
        }
    });

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);

以上改進後的代碼借鑒了他人思路,特此記錄分享,供大家參考。

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