最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can i use a percentage as a value in scrolltop? - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question edited Nov 19, 2018 at 16:08 morten.c 3,5155 gold badges43 silver badges47 bronze badges asked Nov 19, 2018 at 13:36 Jan Fernando Pavel GilJan Fernando Pavel Gil 637 bronze badges 1
  • When the page is smaller than the windows size you can't scroll. So I think you mean after 80% of scrolling. Take a look at this answer: stackoverflow./questions/17688595/… to calculate what the max scrollTop is. Then you can check if scrollTop > maxScrollTop * 0.8 – Thomas Huijzer Commented Nov 19, 2018 at 13:58
Add a ment  | 

2 Answers 2

Reset to default 7

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./ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<div id="container">
  <div id="content"></div>
</div>

Update! I ended up using $(document).height() instead of scrolltop, because it allowed me to introduce a percentage easily. So my code ended up looking like this:

$(window).scroll(function () { 
    if ($(this).scrollTop() > 445) { 
        nav.addClass("f-nav");
    if ($(this).scrollTop() > $(document).height()*0.64) 
        nav.removeClass("f-nav");
    }       
});

Anyways, thanks for the help and I hope someone can find this useful!

发布评论

评论列表(0)

  1. 暂无评论