Last active
May 14, 2020 07:14
-
-
Save iqbalrony/012e0fb27b942f9a84788c9203f9967c to your computer and use it in GitHub Desktop.
Revisions
-
iqbalrony revised this gist
May 14, 2020 . No changes.There are no files selected for viewing
-
iqbalrony revised this gist
May 14, 2020 . 1 changed file with 23 additions and 40 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,29 +1,19 @@ ( function($){ "use strict"; // By JavaScript var $target = document.querySelector('.targetClass'); var setPosition = function ($target) { var docViewHeight = window.innerHeight; var docViewTop = window.scrollY; var docViewBottom = docViewTop + docViewHeight; var eHeight = $target.offsetHeight; var eTop = $target.offsetTop; var eBottom = eTop + eHeight; return ((eTop <= docViewBottom) && (eBottom >= docViewTop)); } setPosition($target); window.addEventListener('scroll', function () { if(setPosition($target)){ console.log('In the viewport!'); } else { console.log('Not in the viewport.'); @@ -33,33 +23,26 @@ // By jQuery var $target = $('.targetClass'), once = false; var setPosition = function ($target) { var docViewHeight = $(window).height(); var docViewTop = $(window).scrollTop(); var docViewBottom = docViewTop + docViewHeight; var eHeight = $target.outerHeight(); var eTop = $target.offset().top; var eBottom = eTop + eHeight; return ((eTop <= docViewBottom) && (eBottom >= docViewTop)); } setPosition($target); $(window).on('scroll', function (e){ if(setPosition($target)){ console.log('In the viewport!'); } else { console.log('Not in the viewport.'); } if(!once){ $( this ).off( e ); } }); // is the target element fully in viewport? -
iqbalrony revised this gist
May 14, 2020 . No changes.There are no files selected for viewing
-
iqbalrony revised this gist
May 14, 2020 . 1 changed file with 26 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -31,20 +31,20 @@ },{once:true}); // By jQuery var $target = $('.targetClass'), docViewHeight, docViewTop, docViewBottom, eHeight, eTop, eBottom; var setPosition = function () { docViewHeight = $(window).height(); docViewTop = $(window).scrollTop(); docViewBottom = docViewTop + docViewHeight; eHeight = $target.outerHeight(); eTop = $target.offset().top; eBottom = eTop + eHeight; return; } @@ -60,7 +60,26 @@ console.log('Not in the viewport.'); } $( this ).off( e ); }); // is the target element fully in viewport? function isInViewport(elem) { var bounding = elem.getBoundingClientRect(); return ( bounding.top >= 0 && bounding.left >= 0 && bounding.bottom <= (window.innerHeight || document.documentElement.clientHeight) && bounding.right <= (window.innerWidth || document.documentElement.clientWidth) ); }; var item = document.querySelector('.targetClass'); window.addEventListener('scroll', function(e){ if ( isInViewport(item) ) { console.log('Fully in the viewport!'); } else { console.log('Not fully in the viewport'); } }); }); -
iqbalrony created this gist
May 14, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,66 @@ ( function($){ "use strict"; // By JavaScript var $target = document.querySelector('.targetClass'), docViewHeight, docViewTop, docViewBottom, eHeight, eTop, eBottom; var setPosition = function () { docViewHeight = window.innerHeight; docViewTop = window.scrollY; docViewBottom = docViewTop + docViewHeight; eHeight = $target.offsetHeight; eTop = $target.offsetTop; eBottom = eTop + eHeight; return; } setPosition(); var isInView = function () { return ((eTop <= docViewBottom) && (eBottom >= docViewTop)); } window.addEventListener('scroll', function () { setPosition(); if(isInView()){ console.log('In the viewport!'); } else { console.log('Not in the viewport.'); } },{once:true}); // By JavaScript var $target = document.querySelector('.targetClass'), docViewHeight, docViewTop, docViewBottom, eHeight, eTop, eBottom; var setPosition = function () { docViewHeight = window) docViewTop = window.scrollY; docViewBottom = docViewTop + docViewHeight; eHeight = $target.offsetHeight; eTop = $target.offsetTop; eBottom = eTop + eHeight; return; } setPosition(); var isInView = function () { return ((eTop <= docViewBottom) && (eBottom >= docViewTop)); } $(window).on('scroll', function (e){ setPosition(); if(isInView()){ console.log('In the viewport!'); } else { console.log('Not in the viewport.'); } $( this ).off( e ); },{once:true}); });