jQuery - Auto scroll / loop text in div
source: http://stackoverflow.com/questions/10220195/jquery-how-to-loop-a-div
by Ea Bangalore
HTML
<!--<div id="right">
<!--<div class="news_container" id="example1">
<div class="news">
<div class="text"> <b>Lorem Ipsum</b> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.------------</div>
</div>
</div>
<br>
<br>
<div class="news_container" id="example2">
<div class="news">
<div class="text"> <b>Lorem Ipsum</b> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>
</div>
</div>
</div>-->
<div class="news_container" id="example3">
<div class="news">
<div class="text"> <b>Lorem Ipsum</b> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the...
CSS
.news_container {
border: 1px solid black;
width:500px;
height: 300px;
overflow: hidden;
position: relative;
padding: 3px;
}
.news {
position: absolute;
left: 0px;
top: 0px;
}
/*#right {
float: right;
}*/
JavaScript
(function ($, undefined) {
$.fn.loopScroll = function (p_options) {
var options = $.extend({
direction: "upwards",
speed: 60
}, p_options);
return this.each(function () {
var obj = $(this).find(".news");
var text_height = obj.find(".text").height();
var start_y, end_y;
if (options.direction == "downwards") {
start_y = -text_height;
end_y = 0;
} else if (options.direction == "upwards") {
start_y = 0;
end_y = -text_height;
}
var animate = function () {
// setup animation of specified block "obj"
// calculate distance of animation
var distance = Math.abs(end_y - parseInt(obj.css("top")));
//alert("animate " + obj.css("top") + "-> " + end_y + " " + distance);
//duration will be distance / speed
obj.animate({
top: end_y
}, //scroll upwards
1000 * distance / options.speed,
"linear",
function () {
// scroll to start position
obj.css("top", start_y);
animate();
});
};
//obj.find(".text").clone().appendTo(obj);
$(this).on("mouseover", function () {
obj.stop();
}).on("mouseout", function () {
animate(); // resume animation
});
obj.css("top", start_y);
animate(); // start animation
});
};
}(jQuery));
$("#example3").loopScroll({
speed: 20
});
//$("#example4").loopScroll({ speed: 120 });
//$("#example1").loopScroll({ direction: "downwards" });
//$("#example2").loopScroll({ direction: "downwards", speed: 30 });