2010-01-26 77 views
9

是否有可能在jQuery中创建100%无缝的选取框(或只是javascript,但jQuery优先)?无缝的jQuery Marquee?

我做了一个简单的选取框,向左移动,直到它离开屏幕,然后简单地向右跳转(当视图出现时)到右侧并重新开始。不过,我会喜欢它没有等待。

我能想到做到这一点的唯一方法是复制文本并将其放在第一个文本之后,然后再将它们交换。然而,我不知道如何在jQuery中实现这一点,我一直在看jQuery的.clone(),但不能正常工作,一切都跳转。

任何想法?

感谢您的时间,

+14

让我们派对就像是1995年! – Kevin 2010-01-26 23:40:36

+0

你的倾向是完全正确的。 – 2010-01-27 02:51:52

+0

我刚刚为此创建了一个插件。希望这有助于:) https://github.com/aamirafridi/jQuery-Marquee – 2013-01-11 14:56:39

回答

22

考虑以下标记:

<div id="marquee">My Text</div> 

对于重复,我会做这样的事情:

$("#marquee").wrapInner("span"); // wrap "My Text" with a new span 
$("#marquee").append($("#marquee span").clone().hide()); // now there are two spans with "My Text" 

移动和交换的跨度是挺容易。这是一个功能完整的例子:

$(function() { 

    var marquee = $("#marquee"); 
    marquee.css({"overflow": "hidden", "width": "100%"}); 

    // wrap "My Text" with a span (old versions of IE don't like divs inline-block) 
    marquee.wrapInner("<span>"); 
    marquee.find("span").css({ "width": "50%", "display": "inline-block", "text-align":"center" }); 
    marquee.append(marquee.find("span").clone()); // now there are two spans with "My Text" 

    // create an inner div twice as wide as the view port for animating the scroll 
    marquee.wrapInner("<div>"); 
    marquee.find("div").css("width", "200%"); 

    // create a function which animates the div 
    // $.animate takes a callback for when the animation completes 
    var reset = function() { 
     $(this).css("margin-left", "0%"); 
     $(this).animate({ "margin-left": "-100%" }, 3000, 'linear', reset); 
    }; 

    // kick it off 
    reset.call(marquee.find("div")); 

}); 

See it in action

免责声明:

我不推荐任何专业的网站。不过,如果你想重现20世纪90年代的复古外观,它可能会很有用。

+0

仍然需要代码交换后,选框已通过可见窗口。 – 2010-01-26 22:29:03

+2

这不是无缝的,在文本结束之后和文本重新开始之前存在巨大的差距。 – Evgeny 2010-06-20 17:21:33

+1

@Evgeny,取决于你的无缝定义。这种设计使屏幕像圆柱体一样,使文字从一侧流出,立即出现在另一侧。 – Joel 2010-07-12 13:53:24