I have 3 div
sections on my page, header
, content
and and a footer
and a each of them has a carousel
. I need to check somehow which one of them is fully visible so that I can perform action accordingly. I should when one of them is fully visible enable going through carousel
images in the section that is currently fully visible. I have tried with this script:
$(document).ready(function () {
var coverImage = $('.magazine-hero'),
carousel = $('.carousel'),
carouselVisible = isElementInViewport(carousel),
coverEventBinded = false;
carouselEventBinded = false;
if (carouselVisible && !carouselEventBinded ) {
$(document).on('keyup', null, 'right', function(){ $('.next').click(); });
$(document).on('keyup', null, 'left', function(){ $('.previous').click(); });
carouselEventBinded = true;
}
window.onscroll = function() {
coverVisible = isElementInViewport(coverImage);
carouselVisible = isElementInViewport(carousel);
if (coverVisible && !coverEventBinded ) {
$(document).on('keyup', null, 'right', forward);
$(document).on('keyup', null, 'left', back);
coverEventBinded = true;
}
if (carouselVisible && !carouselEventBinded ) {
$(document).on('keyup', null, 'right', function(){ $('.next').click(); });
$(document).on('keyup', null, 'left', function(){ $('.previous').click(); });
carouselEventBinded = true;
}
}
});
function isElementInViewport(el) {
var rect = el[0].getBoundingClientRect();
return rect.bottom > rect.height &&
rect.top < (window.innerHeight || document.documentElement.clientHeight) &&
rect.right > 0 &&
rect.left < (window.innerWidth || document.documentElement.clientWidth);
}
But that doesn't work, because when I am scrolling down and the upper div is still visible I am able to navigate carousels of both sections. How can I fix that?
Update
I have updated my script, now I am trying to see if the cover is visible and then bind key events for navigating covers, and if it is not visible I am unbiding the events for covers navigation, and biding the events for carousels:
$(document).ready(function () {
var coverImage = $('.magazine-hero'),
headerCarousel = $('.header-carousel'),
headerCarouselVisible = isElementInViewport(headerCarousel),
coverEventBinded = false,
carouselEventBinded = false;
if (headerCarouselVisible && !carouselEventBinded ) {
$(document).on('keyup', null, 'right', function(){ $('.next').click(); });
$(document).on('keyup', null, 'left', function(){ $('.previous').click(); });
carouselEventBinded = true;
}
window.onscroll = function() {
coverVisible = isElementInViewport(coverImage);
headerCarouselVisible = isElementInViewport(headerCarousel);
console.log('Cover visible ' + coverVisible);
console.log('Event binded ' + coverEventBinded);
if (coverVisible && !coverEventBinded) {
$(document).unbind('keyup', null, 'right', function(){ $('.next').click(); });
$(document).unbind('keyup', null, 'left', function(){ $('.previous').click(); });
$(document).on('keyup', null, 'right', forward);
$(document).on('keyup', null, 'left', back);
coverEventBinded = true;
carouselEventBinded = false;
}
else {
$(document).unbind('keyup', null, 'right', forward);
$(document).unbind('keyup', null, 'left', back);
$(document).on('keyup', null, 'right', function(){ $('.next').click(); });
$(document).on('keyup', null, 'left', function(){ $('.previous').click(); });
carouselEventBinded = true;
coverEventBinded = false;
}
}
});
function isElementInViewport(el) {
var rect = el[0].getBoundingClientRect();
console.log('Bottom ' + rect.bottom);
console.log('Height ' + rect.height);
console.log('Client height ' + document.documentElement.clientHeight);
console.log('Window height ' + window.innerHeight);
return rect.bottom > 0 &&
rect.top < (window.innerHeight || document.documentElement.clientHeight) &&
rect.right > 0 &&
rect.left < (window.innerWidth || document.documentElement.clientWidth);
}
The issue I have now, is that sometimes even when the bottom is a minus value or less than 0, I get true for coverVisible
. And also sometimes the coverEventBinded is true when it shouldn't be true. I am not sure what is wrong with the script, that it behaves this way?