使用jquery滚动窗格

时间:2012-08-21 09:17:41

标签: javascript jquery html css html5

<script language="javascript">
$(document).ready(function($) {

    var methods = {
        init: function(options) {
            this.children(':first').stop();
            this.marquee('play');
        },
        play: function() {
            var marquee = this,
                pixelsPerSecond = 100,
                firstChild = this.children(':first'),
                totalHeight = 0,
                difference,
                duration;

            // Find the total height of the children by adding each child's height:
            this.children().each(function(index, element) {
                totalHeight += $(element).innerHeight();
            });

            // The distance the divs have to travel to reach -1 * totalHeight:
            difference = totalHeight + parseInt(firstChild.css('margin-top'), 10);

            // The duration of the animation needed to get the correct speed:
            duration = (difference/pixelsPerSecond) * 1000;

            // Animate the first child's margin-top to -1 * totalHeight:
            firstChild.animate(
                { 'margin-top': -1 * totalHeight },
                duration,
                'linear',
                function() {
                    // Move the first child back down (below the container):
                    firstChild.css('margin-top', marquee.innerHeight());
                    // Restart whole process... :)
                    marquee.marquee('play');
                }
            );
        },
        pause: function() {
            this.children(':first').stop();
        }
    };

    $.fn.marquee = function(method) {

        // Method calling logic
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.marquee');
        }

    };

})(jQuery);

var marquee = $('#marquee');

marquee.marquee();

marquee.hover(function() {
    marquee.marquee('pause');
}, function() {
    marquee.marquee('play');    
});
</script>
<style type="text/css">
#marquee {
    margin:inherit;
    width:auto;
    height:inherit 
}

</style>

我想使用jquery创建一个滚动条,但是我失败了。上面的代码是我用来向上滚动项目的选框。我正在使用它,如下所示,

<html>
<body>
<div class="content">
<div id="marquee">
    <ul>
       <li>...</li> 
       ....
    </ul>
</div>
</div></body>
</html>

但它根本没有滚动,我使用的代码中有什么不正确的东西你可以找到吗?

1 个答案:

答案 0 :(得分:0)

不确定margin-top是否应该适用于此。 尝试使用position:relative表示holder block(marquee)和position:absolute表示内容(ul)。并更新顶部而不是保证金顶部。但在这种情况下,您可能需要指定高度和溢出:隐藏为marquee div。另一个选项是设置高度和oveflow:隐藏为选框,但保留位置默认值。并使用scrollTop或一些类似的jquery函数滚动内容。