0

我正在尝试marquee tag通过使用函数动画一组图像来实现在 jQuery 中animate(),使它们向右或向左移动。
但是,我无法弄清楚单个图像何时到达屏幕末尾,然后单独返回 到另一侧。
因为我听说每个浏览器的窗口大小都不是恒定的,那么有没有办法实现呢?
这是我到目前为止提出的(简单而基本):

$(document).ready(function(){
    moveThumbs(500);
    function moveThumbs(speed){
        $('.thumbnails').animate({
            right:"+=150"
        }, speed);
        setTimeout(moveThumbs, speed);
    }

});

注意:我在 SO 中搜索了相关问题,但没有找到针对我的具体问题的确切信息。

4

1 回答 1

2

这是一个基本脚本,它在屏幕上移动图像,然后在另一侧恢复并适应窗口宽度。

你可以看到它在这里工作:http: //jsfiddle.net/jfriend00/rnWa2/

function startMoving(img) {
    var img$ = $(img);
    var imgWidth = img$.width();
    var screenWidth = $(window).width();
    var amount = screenWidth - (parseInt(img$.css("left"), 10) || 0);
    // if already past right edge, reset to 
    // just left of left edge
    if (amount <=0 ) {
        img$.css("left", -imgWidth);
        amount = screenWidth + imgWidth;
    }
    var moveRate = 300;   // pixels per second to move
    var time = amount * 1000 / moveRate;
    img$.stop(true)
        .animate({left: "+=" + amount}, time, "linear", function() {
            // when animation finishes, start over
            startMoving(this);
        })
}

$(document).ready(function() {
    // readjust if window changes size
    $(window).resize(function() {
        $(".mover").each(function() {
            startMoving(this);
        });
    });
});

​ ​</p>

于 2012-05-23T01:44:42.087 回答