Skip to content

Instantly share code, notes, and snippets.

@karki-dennis
Created September 21, 2016 06:45
Show Gist options
  • Save karki-dennis/efb043407a6fa1fa557a874fb1e56b76 to your computer and use it in GitHub Desktop.
Save karki-dennis/efb043407a6fa1fa557a874fb1e56b76 to your computer and use it in GitHub Desktop.
detect the div is visivle or not
$.fn.inView = function () {
if (!this.length) return false;
var rect = this.get(0).getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
);
};
var lastScrollTop = 0;
$(window).scroll(function (event) {
var st = $(this).scrollTop();
if (st > lastScrollTop) {
// downscroll code
if ($('.hidden-div').inView()) {
$(".home-banner-content > .btn").css({
'transform': 'translate(-50%,130%)'
});
} else {
$(".home-banner-content > .btn").css({
/* 'transform': 'translate(-50%,0)',*/
'transition': '0.5s all'
});
}
/*show btn after bottom div*/
if ($('.show-div').inView()) {
$(".home-banner-content > .btn").css({
'transform': 'translate(-50%,0)'
});
}
} else {
// upscroll code
/*hide btn after bottom div*/
if ($('.show-div').inView()) {
$(".home-banner-content > .btn").css({
'transform': 'translate(-50%,130%)'
});
}
/*show btn after the div is visible*/
if ($('.timer-block').inView()) {
$(".home-banner-content > .btn").css({
'transform': 'translate(-50%,0)'
});
}
}
lastScrollTop = st;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment