I am trying to use jquery to check if Div RED is within the viewport, and if not, then check if Div ORANGE is in the viewport. The function I am using works great if there is only one IF statement, but when I add another IF ELSE statement, it wont work.
here is the function:
$.fn.isOnScreen = function(){
var win = $(window);
var viewport = {
top : win.scrollTop(),
left : win.scrollLeft()
};
viewport.right = viewport.left + win.width();
viewport.bottom = viewport.top + win.height();
var bounds = this.offset();
bounds.right = bounds.left + this.outerWidth();
bounds.bottom = bounds.top + this.outerHeight();
return (!(viewport.right < bounds.left || viewport.left > bounds.right || viewport.bottom < bounds.top || viewport.top > bounds.bottom));
};
here is the code I added to check and see if div red or div orange is within the viewport
$(window).scroll(function() {
if ($('.red').isOnScreen() === true) {
$('.red').remove();
} else if ($('.orange').isOnScreen() === true) {
$('.orange').remove();
}
});
here is a jfiddle http://jsfiddle.net/wN7ah/453/
If the <div> element is in the viewport, its top and left are always greater than or equal zero. In addition, its distance from the right is less than or equal to the width of the viewport, and ids distance from the bottom is less than or equal to the height of the viewport.
$(element).is(":visible"); Example 1: This example uses :visible selector to check an element is visible or not using jQuery.
You can use .is(':visible') to check whether an element is visible in the layout or not. Elements are considered visible if they consume space in the document. Visible elements have a width or height that is greater than zero. The :visible selector selects every element that is currently visible.
To toggle a div visibility in jQuery, use the toggle() method. It checks the div element for visibility i.e. the show() method if div is hidden. And hide() id the div element is visible. This eventually creates a toggle effect.
Here is the working versions: http://jsfiddle.net/wN7ah/455/
Function that does the magic:
$.fn.isOnScreen = function(){
var win = $(window);
var viewport = {
top : win.scrollTop(),
left : win.scrollLeft()
};
viewport.right = viewport.left + win.width();
viewport.bottom = viewport.top + win.height();
var bounds = this.offset();
bounds.right = bounds.left + this.outerWidth();
bounds.bottom = bounds.top + this.outerHeight();
return (!(viewport.right < bounds.left || viewport.left > bounds.right || viewport.bottom < bounds.top || viewport.top > bounds.bottom));
};
Usage:
$(window).scroll(function() {
if ($('.orange').isOnScreen() == true) {
//alert("removing orange");
$('.orange').remove();
}
if ($('.red').isOnScreen() == true) {
//alert("removing red");
$('.red').remove();
}
});
If any orange or red is present in onscreen it'll remove them.
If you want to check when it's removed, add alert before removing: http://jsfiddle.net/wN7ah/457/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With