I'm currently using getBoundingClientRect() to work out if an element enters the viewport. What I really need to do though is to check whether 50% (or any given percentage) of the element has entered the viewport (i'm checking on scroll). If it is visible then I update some text on the page to say yes, if it isn't then the text says no.
I can't seem to get my head around the logic and its starting to drive me crazy, is anyone able to help?
Current code below!
isBannerInView: function (el, y) {
var _this = this,
elemTop,
elemBottom,
elemHeight,
isVisible;
for (var i = 0; i < el.length; i++) {
var pos = banners.indexOf(el[i]);
elemTop = el[i].getBoundingClientRect().top;
elemBottom = el[i].getBoundingClientRect().bottom;
elemHeight = el[i].getBoundingClientRect().height;
isVisible = (elemTop >= 0) && (elemBottom <= window.innerHeight);
_this.updateResults(el[i], pos, isVisible);
};
},
updateResults: function (el, pos, isVisible) {
var isInView = isVisible ? 'Yes' : 'No';
document.querySelectorAll('.results')[0].getElementsByTagName('span')[pos].innerHTML = isInView;
},