それを行うには、かなり簡単な方法だ上で左から右にスクロールアイテムを移動する.animate()を使用することができます。あなたは、コンテンツのネイティブ、切り捨てられていない幅を決定した場合
div#container {
/* among other settings: see fiddle */
width: 200px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
:あなたは自動的に切り捨てと省略記号を処理するために、いくつかのCSSを使用することができます
<div id="container">
Here is the long content, long long content. So long. Too long.
</div>
:まず、あなたの長いテキストを含むdiv要素を設定jQueryの.animate()
を使用すると、実行時までテキストの長さがわからなくても(一定の速度で)コンテンツを移動することができます(twitterフィードの場合のように)。ここでは、測定を得るための、やや機械的な方法です:
var true_width = (function(){
var $tempobj = $('#container') // starting with truncated text div container
.clone().contents() // duplicate the text
.wrap('<div id="content"/>') // wrap it in a container
.parent().appendTo('body') // add this to the dom
.css('left','-1000px'); // but put it far off-screen
var result = $tempobj.width(); // measure it
$tempobj.remove(); // clean up
return result;
})();
最後に、いくつかのアニメーションは:
$('#container').one('mouseenter', function(){ // perhaps trigger once only
var shift_distance = true_width - $(this).width(); // how far to move
var time_normalized = parseInt(shift_distance/100, 10) * 1000; // speed
$(this).contents().wrap('<div id="content">').parent() // wrap in div
.animate({
left: -shift_distance,
right: 0
}, time_normalized, 'linear'); // and move the div within its "viewport"
});
にかかわらず、テキストの長さの、あなたは100あたり約1秒の一貫した速度を取得しますピクセル(必要に応じて調整)。マウス離脱のコンテンツをリセットまたは巻き戻すことは、練習として残されています。このアプローチはいくつかの単純なビットを持っていますが、あなたにいくつかのアイデアを与えるかもしれません。ここで
は、作業例を示します。最後http://jsfiddle.net/redler/zdvyj/
フィドル:)に感謝し、素晴らしいが正しくありません弔辞 – Purefan