2010-01-26 82 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