Jump to content

Using pourcentage with javascript


Guest georgene

Recommended Posts

Guest georgene

I would like to know how to use pourcentage instead of pixels in that javascript code.

  if (document.body.scrollTop > 1 || document.documentElement.scrollTop > 1) 

I know this sounds really obvious but I don't know and can't find anything online about that.

 

Thank you.

Link to comment
Share on other sites

Guest georgene

I saw what you sent, but my question is about javascript synthax. 

The line of code I sent means when user scrolls 1 pixel, do whatever. But what I want to write is when user scrolls 1% of the page, do whatever.

Link to comment
Share on other sites

Guest georgene

I just understood what you are telling me to do. 

{
var h = document.documentElement,
b = document.body, st = 'scrollTop', sh = 'scrollHeight'; 
var percent = (h[st]||b[st]) / ((h[sh]||b[sh]) - h.clientHeight) * 100;  

if (percent > 1) {
//do whatever
}
 } 

 

 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...