Skip to content

Instantly share code, notes, and snippets.

@luislobo14rap
Last active August 13, 2018 13:51
Show Gist options
  • Select an option

  • Save luislobo14rap/554fc7606dd5fadf3617b4ec9202c1f4 to your computer and use it in GitHub Desktop.

Select an option

Save luislobo14rap/554fc7606dd5fadf3617b4ec9202c1f4 to your computer and use it in GitHub Desktop.
<!-- example - isVisibleInWindow.html v1 -->
<!doctype html>
<html lang="pt-br">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<title>bootstrap starter template</title>
<style>
.container-9999px{
background: #aaaaaa;
height: 9999px;
width: 9999px;
align-items: center;
display: flex;
justify-content: center;
}
.inner-block{
background: #000000;
height: 500px;
width: 500px;
}
</style>
</head>
<body>
<div class="container-9999px">
<div class="inner-block"></div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<script src="js/isVisibleInWindow.min.js"></script>
<script>
$(function(){
setInterval(function(){
console.log( isVisibleInWindow( $('.inner-block') ) );
}, 100);
});
// get-scrolls.min.js v1
function getScrollX(){return null!=window.pageXOffset?window.pageXOffset:(document.documentElement||document.body.parentNode||document.body).scrollLeft}function getScrollY(){return null!=window.pageYOffset?window.pageYOffset:(document.documentElement||document.body.parentNode||document.body).scrollTop}
// between.min.js v1.1.1
function between(c,d,e,f=!1){let g=Math.min.apply(Math,[d,e]),h=Math.max.apply(Math,[d,e]);if(!0==f)return c>=g&&c<=h;return c>g&&c<h}Number.prototype.between=function(c,d,e=!1){return between(this,c,d,e)},String.prototype.between=function(c,d,e=!1){return between(this,c,d,e)};
// isVisibleInWindow.min.js v1.0.1, require between.min.js v1.1.1
function isVisibleInWindow(a){'string'==typeof a&&(a=$(a).eq(0));let b={x:getScrollX(),y:getScrollY()},c={x:$(window).innerWidth(),y:$(window).innerHeight()},d={max_x:b.x+c.x,min_x:b.x,max_y:b.y+c.y,min_y:b.y},e={x:a.offset().left,y:a.offset().top},f={x:a.innerWidth(),y:a.innerHeight()},g={max_x:e.x+f.x,min_x:e.x,max_y:e.y+f.y,min_y:e.y},h=!1,i=!1;return g.min_x.between(d.min_x,d.max_x,!0)&&(h=!0),g.max_x.between(d.min_x,d.max_x,!0)&&(h=!0),d.min_x.between(g.min_x,g.max_x,!0)&&(h=!0),d.max_x.between(g.min_x,g.max_x,!0)&&(h=!0),g.min_y.between(d.min_y,d.max_y,!0)&&(i=!0),g.max_y.between(d.min_y,d.max_y,!0)&&(i=!0),d.min_y.between(g.min_y,g.max_y,!0)&&(i=!0),(d.max_y.between(g.min_y,g.max_y,!0)&&(i=!0),h&&i)}
</script>
</body>
</html>
// isVisibleInWindow.js v1.0.1, require between.min.js v1.1.1
function isVisibleInWindow(element) {
if (typeof element == 'string') {
element = $(element).eq(0);
};
let screenPosition = {
x: getScrollX(),
y: getScrollY()
},
screenSize = {
x: $(window).innerWidth(),
y: $(window).innerHeight()
},
screenRectangle = {
max_x: screenPosition.x + screenSize.x,
min_x: screenPosition.x,
max_y: screenPosition.y + screenSize.y,
min_y: screenPosition.y,
},
containerPosition = {
x: element.offset().left,
y: element.offset().top
},
containerSize = {
x: element.innerWidth(),
y: element.innerHeight()
},
containerRectangle = {
max_x: containerPosition.x + containerSize.x,
min_x: containerPosition.x,
max_y: containerPosition.y + containerSize.y,
min_y: containerPosition.y,
},
visibleX = false,
visibleY = false;
if (containerRectangle.min_x.between(screenRectangle.min_x, screenRectangle.max_x, true)) {
visibleX = true;
};
if (containerRectangle.max_x.between(screenRectangle.min_x, screenRectangle.max_x, true)) {
visibleX = true;
};
if (screenRectangle.min_x.between(containerRectangle.min_x, containerRectangle.max_x, true)) {
visibleX = true;
};
if (screenRectangle.max_x.between(containerRectangle.min_x, containerRectangle.max_x, true)) {
visibleX = true;
};
if (containerRectangle.min_y.between(screenRectangle.min_y, screenRectangle.max_y, true)) {
visibleY = true;
};
if (containerRectangle.max_y.between(screenRectangle.min_y, screenRectangle.max_y, true)) {
visibleY = true;
};
if (screenRectangle.min_y.between(containerRectangle.min_y, containerRectangle.max_y, true)) {
visibleY = true;
};
if (screenRectangle.max_y.between(containerRectangle.min_y, containerRectangle.max_y, true)) {
visibleY = true;
};
if (visibleX && visibleY) {
return true;
} else {
return false;
};
};
// isVisibleInWindow.min.js v1.0.1, require between.min.js v1.1.1
function isVisibleInWindow(a){'string'==typeof a&&(a=$(a).eq(0));let b={x:getScrollX(),y:getScrollY()},c={x:$(window).innerWidth(),y:$(window).innerHeight()},d={max_x:b.x+c.x,min_x:b.x,max_y:b.y+c.y,min_y:b.y},e={x:a.offset().left,y:a.offset().top},f={x:a.innerWidth(),y:a.innerHeight()},g={max_x:e.x+f.x,min_x:e.x,max_y:e.y+f.y,min_y:e.y},h=!1,i=!1;return g.min_x.between(d.min_x,d.max_x,!0)&&(h=!0),g.max_x.between(d.min_x,d.max_x,!0)&&(h=!0),d.min_x.between(g.min_x,g.max_x,!0)&&(h=!0),d.max_x.between(g.min_x,g.max_x,!0)&&(h=!0),g.min_y.between(d.min_y,d.max_y,!0)&&(i=!0),g.max_y.between(d.min_y,d.max_y,!0)&&(i=!0),d.min_y.between(g.min_y,g.max_y,!0)&&(i=!0),(d.max_y.between(g.min_y,g.max_y,!0)&&(i=!0),h&&i)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment