-
-
Save rustinlee/e6ddf4e51ad54cee7fa0 to your computer and use it in GitHub Desktop.
improved infinite jQuery marquee with variable pixels/sec, better text width calculation, less resource usage http://jsfiddle.net/ua5rnr1j/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="slider"> | |
<div class="edge"></div> | |
<ul id="slide-container"> | |
<li class="slideItem" > | |
News happened somewhere • | |
</li> | |
<li class="slideItem"> | |
Futhermore, even more news happened elsewhere • | |
</li> | |
<li class="slideItem"> | |
It appears that in a foreign country, today news happened • | |
</li> | |
<li class="slideItem"> | |
Lorem ipsum stocks are down • | |
</li> | |
<li class="slideItem"> | |
Something about China • | |
</li> | |
</ul> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://stackoverflow.com/a/2771544 | |
$.fn.textWidth = function(){ | |
var html_org = $(this).html(); | |
var html_calc = '<span>' + html_org + '</span>'; | |
$(this).html(html_calc); | |
var width = $(this).find('span:first').width(); | |
$(this).html(html_org); | |
return width; | |
}; | |
var marqueeSpeed = 25; // pixels/sec | |
function tickerSlide() { | |
var currentItemWidth = $('.slideItem:first-child').textWidth(); | |
console.log(currentItemWidth); | |
$("#slide-container").animate({marginLeft:-currentItemWidth}, (currentItemWidth / marqueeSpeed) * 1000, 'linear', function(){ | |
$(this).css({marginLeft:0}).find("li:last").after($(this).find("li:first")); | |
tickerSlide(); | |
}); | |
}; | |
tickerSlide(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.slider{ | |
width:100%; | |
overflow:hidden; | |
position:relative; | |
margin:0; | |
} | |
.edge{ | |
left:0; | |
right:0; | |
top:0; | |
bottom:0; | |
position:absolute; | |
height:100%; | |
display:block; | |
} | |
.edge:before{ | |
content:''; | |
position:absolute; | |
left:0; | |
background:-webkit-linear-gradient(left, white 10%, rgba(0,0,0,0) 100%); | |
width:25%; | |
height:100%; | |
} | |
.edge:after{ | |
content:''; | |
position:absolute; | |
right:0; | |
background:-webkit-linear-gradient(right, white 10%, rgba(0,0,0,0) 100%); | |
width:25%; | |
height:100%; | |
} | |
div#slider > ul{ | |
background:#ddd; | |
overflow:hidden; | |
width:1000%; | |
margin:0; | |
} | |
div#slider > ul > li{ | |
list-style:none; | |
display:inline-block; | |
padding:0 50px; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment