Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can i use a percentage as a value in scrolltop?

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.

like image 901
Jan Fernando Pavel Gil Avatar asked Nov 19 '18 13:11

Jan Fernando Pavel Gil


People also ask

What is scrollTop value?

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 .

Why is scrollTop always 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 .

What does scrollTop return?

The scrollTop() method sets or returns the vertical scrollbar position for the selected elements.

What is scrollTop in angular?

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.

What is the use of scrolltop and scrollleft?

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.

What is scrolltop property in HTML5?

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. ...

How do I set the number of pixels scroll horizontally?

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.

What does the number mean in the scroll settings?

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.


1 Answers

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>
like image 187
scraaappy Avatar answered Sep 23 '22 03:09

scraaappy