Reputation: 1031
I am trying to start some animations only when the DIV is visible in the page.
This is the function I have done:
function startAnimations() {
if (animated_contents)
jQuery.each(animated_contents, function(index, arr) {
var item = jQuery('#' + arr[1]);
var page_id = arr[0];
setInterval(function() {
var visible_window = jQuery(window.top).height();
var top = item.offset().top - jQuery(document).scrollTop();
console.log(item + visible_window + top);
if (top < visible_window)
animateEl(arr[1], arr[2], arr[3], arr[4]);
}, 500);
})
}
It works. The problem is that the setInterval
has to run always and I am afraid it will reduce the resources sensibly.
Is there a better way?
Please consider that the items are created dinamically so it must be a continuos check.
Thanks for any suggestion
Upvotes: 0
Views: 37
Reputation: 138
Use $(window).scroll() and check the position of the div to run the animation.
Upvotes: 1