Hi folks,
I created a UX in which an element is displayed only when the user clicks on a button. Now I want that when the user is scrolling up from that area, that that element becomes hidden?
Here is code that I currently have:
$("#tell-state").click(function(){
alert("Here's something new.");
$(".drop-light").show();
});
$(window).bind('scroll', function() {
if ($(window).scrollBottom() > 100) {
$(".drop-light").hide();
}
});
And here is some new code that I’m considering:
var output = document.getElementById('output')
function getDocHeight() {
var D = document;
return Math.max(
D.body.scrollHeight, D.documentElement.scrollHeight,
D.body.offsetHeight, D.documentElement.offsetHeight,
D.body.clientHeight, D.documentElement.clientHeight
)
}
var winheight, docheight, trackLength, throttlescroll
function getmeasurements(){
winheight= window.innerHeight || (document.documentElement || document.body).clientHeight
docheight = getDocHeight()
trackLength = docheight - winheight
}
function amountscrolled(){
var scrollTop = window.pageYOffset || (document.documentElement || document.body.parentNode || document.body).scrollTop
var pctScrolled = Math.floor(scrollTop/trackLength * 100) // gets percentage scrolled (ie: 80 or NaN if tracklength == 0)
output.innerHTML = pctScrolled + '% scrolled'
}
getmeasurements()
window.addEventListener("resize", function(){
getmeasurements()
}, false)
window.addEventListener("scroll", function(){
clearTimeout(throttlescroll)
throttlescroll = setTimeout(function(){ // throttle code inside scroll to once every 50 milliseconds
amountscrolled()
}, 50)
}, false)
This last code is almost perfect, but I don’t know how to integrate it within my code! It outputs a percentage of how much you have scrolled, but I need it do something when it reaches a certain percentage…For example, lets say, at 50% scroll, I want to hide my element. How can I accomplish that?
Thanks in advance for your help!