Skip to content

Instantly share code, notes, and snippets.

@rustinlee
Forked from roine/index.html
Last active September 22, 2015 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rustinlee/e6ddf4e51ad54cee7fa0 to your computer and use it in GitHub Desktop.
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/
<div id="slider">
<div class="edge"></div>
<ul id="slide-container">
<li class="slideItem" >
News happened somewhere&nbsp;&bull;&nbsp;
</li>
<li class="slideItem">
Futhermore, even more news happened elsewhere&nbsp;&bull;&nbsp;
</li>
<li class="slideItem">
It appears that in a foreign country, today news happened&nbsp;&bull;&nbsp;
</li>
<li class="slideItem">
Lorem ipsum stocks are down&nbsp;&bull;&nbsp;
</li>
<li class="slideItem">
Something about China&nbsp;&bull;&nbsp;
</li>
</ul>
</div>
// 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();
.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