水平文本滚动条,长度检测javascript / jquery

时间:2009-09-01 06:11:47

标签: javascript jquery html scroller

我正在寻找一种方法来进行水平文本滚动,只有当文本长度比容器DIV长时才滚动。有什么建议怎么做?

2 个答案:

答案 0 :(得分:0)

这是一个很好的

JavaScript Bs_ScrollableDiv

答案 1 :(得分:0)

试试这个:

CSS

.container { width: 600px; background: #ccc; }
.ticker { white-space: nowrap; }

HTML

<div class="container">
<span class="ticker">Some short text that isn't greater than the browser window</span>
<span class="ticker">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed facilisis metus quis augue facilisis elementum. Pellentesque aliquet congue tristique. Phasellus feugiat cursus lobortis. Cras et massa odio, et vehicula nisi. Donec adipiscing condimentum diam sed hendrerit. Curabitur eros elit, aliquet et aliquam id, facilisis id nisi. Vivamus dictum commodo libero, eu venenatis nulla sodales in. Maecenas rhoncus, dui at sollicitudin scelerisque, neque lectus viverra metus, vel elementum nisl ante quis mi. Cras sagittis enim ac arcu scelerisque tristique vel in dolor. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Cras vehicula leo vel lacus blandit ut adipiscing lorem sodales.Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed facilisis metus quis augue facilisis elementum. Pellentesque aliquet congue tristique. Phasellus feugiat cursus lobortis. Cras et massa odio, et vehicula nisi. Donec adipiscing condimentum diam sed hendrerit. Curabitur eros elit, aliquet et aliquam id, facilisis id nisi. Vivamus dictum commodo libero, eu venenatis nulla sodales in. Maecenas rhoncus, dui at sollicitudin scelerisque, neque lectus viverra metus, vel elementum nisl ante quis mi. Cras sagittis enim ac arcu scelerisque tristique vel in dolor. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Cras vehicula leo vel lacus blandit ut adipiscing lorem sodales. Vestibulum ultricies elementum mattis. Maecenas sit amet turpis magna. Phasellus posuere mi nec nisi sodales non dapibus nibh faucibus. Vestibulum leo tellus, blandit sed auctor id, faucibus quis sem. Sed at purus quam. Praesent eget ante sem, a scelerisque nulla. Praesent id quam sit amet eros viverra euismod. Integer volutpat vulputate aliquam. Aliquam convallis dolor tellus, in cursus justo. Donec molestie consequat dolor. Nunc imperdiet arcu et justo vehicula mollis. Suspendisse id molestie velit. Morbi sed est arcu, non aliquam augue.</span>
</div>

脚本

$(document).ready(function(){
 var tickerWidth = '100%'; // add '%' or 'px' to this variable
 var tickerSpeed = 10; // 0 = stopped, 30 = almost too fast
 var tickerDelay = 0; // delay in ms
 var tickerDirection = 'left'; // options = left, right (up & down work as well, but not the way you want)
 var tickerMouseoverPause = true;
 var tickerLoopTimes = 0; // 0 = infinite # of loops

 $('.ticker').each(function(){
  if ( $(this).width() >  $(this).parent().width() ) {
   var marq = $('<marquee></marquee>').attr({
    'behavior' : 'scroll',
    'scrollamount' : tickerSpeed,
    'direction' : tickerDirection,
    'width' : tickerWidth
   })
   if (tickerLoopTimes != 0) {
    marq.attr('loop', tickerLoopTimes) 
   }
   if (tickerMouseoverPause) {
    marq.attr({
     'onmouseover' : 'this.stop()',
     'onmouseout' : 'this.start()'
    })
   }
   $(this).wrap(marq);
  }
 })
})

此脚本基本上用<marquee>包装文本,以获取有关其属性的更多信息,请查看this site

注意我尝试使用不显眼的jQuery,在选框上使用hover,但它不起作用。但是,向标记添加内联mouseovermouseout事件的效果非常好。