I'm pretty new to HTML in general. I have the following code and I was wondering if there is any way of using a percentage instead of a fixed value. I have searched but I couldn't find a simple solution.
$(window).scroll(function () {
if ($(this).scrollTop() > 445 && $(this).scrollTop() < 1425 ) {
nav.addClass("f-nav");
} else {
nav.removeClass("f-nav");
}
Basically what I want is the class to be removed after scrolling past 80% of the page, instead of after 1425px so that it also works properly if the window size is modified.
An element's scrollTop value is a measurement of the distance from the element's top to its topmost visible content. When an element's content does not generate a vertical scrollbar, then its scrollTop value is 0 .
An element's scrollTop is a form of distance measurement regarding an element's top to its topmost visible content. When an element content does not generate a vertical scrollbar, then its scrollTop value defaults to 0." Since the content of inner doesn't generate a scrollbar, scrollTop is 0 .
The scrollTop() method sets or returns the vertical scrollbar position for the selected elements.
ng-scrolltop demo: angular component that monitors current Y position in a long page or element then if scrolled down enough, shows up a clickable, unobtrusive icon that scrolls to top smoothly.
The scrollTop property sets or returns the number of pixels an element's content is scrolled vertically. Tip: Use the scrollLeft property to set or return the number of pixels an element's content is scrolled horizontally.
HTML DOM scrollTop Property 1 Definition and Usage. The scrollTop property sets or returns the number of pixels an element's content is scrolled vertically. 2 Browser Support 3 Syntax 4 Property Values. Specifies the number of pixels the element's content is scrolled vertically. 5 Technical Details 6 More Examples. ...
Tip: Use the scrollLeft property to set or return the number of pixels an element's content is scrolled horizontally. Tip: To add scrollbars to an element, use the CSS overflow property.
Specifies the number of pixels the element's content is scrolled vertically. Special notes: If the number is a negative value, the number is set to "0". If the element cannot be scrolled, the number is set to "0". If the number is greater than the maximum allowed scroll amount, the number is set to the maximum number.
From the doc, scrollTop() expects a number which represents position in pixel.
But you can calculate when scroll reaches 80% with, for example, something like
Pseudo-code:
if ((this.scrollTop + this.height) / content.height >= .8){
// do something
}
See the working snippet below for example
$("#container").scroll(function () {
if (($(this).scrollTop()+$(this).height())/$("#content").height() >= .8) {
$("#content").addClass("scrolled");
}else{
$("#content").removeClass("scrolled");
}
});
#container{
width:80%;
height:300px;
border: solid 1px red;
overflow:auto;
}
#content{
width:80%;
height:1000px;
border: solid 1px gray;
transition: background-color 1s;
}
#content.scrolled{
background-color:blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
<div id="content"></div>
</div>
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