javascript - jQuery scrolling text on hover -


i have "a" element want scroll left on hover. remove first character , append end of string.

how can continuously fire scroll function?

mouse enters element -> scroll() fired until mouse leaves element or user clicks on it.

html:

<a href="foo.htm" class="scrollthis">this text scrolls on hover</a> 

jquery:

$(".scrollthis").hover(function(){ scroll($(this)); }); function scroll(ele){ var s = $(ele).text().substr(1)+$(ele).text().substr(0,1); $(ele).text(s); } 

use setinterval() call repeatedly mouseenter, clearinterval() stop on mouseleave:

var intervalid; $(".scrollthis").hover(function(){ var $this = $(this); intervalid = setinterval(function() { scroll($this); }, 100); }, function() { clearinterval(intervalid); }); 

note don't need use $(ele) in scroll() function because ele jquery object:

function scroll(ele){ var s = ele.text().substr(1)+ele.text().substr(0,1); ele.text(s); } 

demo: http://jsfiddle.net/htbzn/

you can make scroll() function bit neater if use callback syntax of .text() method (or move 1 line directly .hover code):

function scroll(ele){ ele.text(function(i,val) { return val.substr(1) + val.substr(0,1); }); }​ 

demo: http://jsfiddle.net/htbzn/1/


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

c++ - Accessing inactive union member and undefined behavior? -

php - Get uncommon values from two or more arrays -