그것을 할 수있는 매우 간단한 방법입니다에 왼쪽에서 오른쪽으로 스크롤 항목을 이동하는 .animate()를 사용할 수 있습니다. 그런 다음 내용의 기본, untruncated 폭을 결정하는 경우
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>
: 첫째, 긴 텍스트를 포함하는 사업부를 설정 jQuery의 .animate()
을 사용하여 런타임까지 텍스트의 길이를 알지 못하는 경우에도 일관된 속도로 해당 컨텐츠를 이동할 수 있습니다 (트위터 피드의 경우처럼).
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"
});
, 당신은 약 1 초에 100 개의 일관된 속도를 얻을 수 있습니다 픽셀 (원하는대로 조정). 마우스를 놓은 상태에서 콘텐츠를 다시 설정하거나 되감기하는 것은 연습 과제로 남겨 둡니다. 이 접근 방식은 약간의 순진한 비트가 있지만 몇 가지 아이디어를 줄 수 있습니다.여기
가 작동 예제 : 지난 http://jsfiddle.net/redler/zdvyj/
감사 : 위대한 폭발물 : 여기에 코드입니다 anatory comments – Purefan