Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use vh units instead of pixels for .scrolltop functions

Tags:

jquery

scroll

I have a menu bar who's class I want to change after a users scrolls to the next div, the div is 100vh tall. The function below works only with screens my size, anything smaller or bigger the animation will go to early or too late.

How do I make the units that this function uses vh? Thanks

  $(window).scroll(function() {    
var scroll = $(window).scrollTop();
if (scroll >= 600) {   <---- change to vh units not px-----
    $(".home").removeClass("open");
}
like image 781
Anfa A Avatar asked Nov 19 '14 00:11

Anfa A


People also ask

What is scroll y?

Definition and Usage The scrollY property returns the pixels a document has scrolled from the upper left corner of the window. The scrollY property is read-only.

How do you scroll down in Javascript?

window. scrollTo(0, document. body. scrollHeight);

How do I know if my scroll is at the top?

You can check if window. scrollY (the number of pixels the window has scrolled vertically) is equal to 0 . If you want to check if the window has been scrolled to its leftermost, you can check if window. scrollX (the number of pixels the window has scrolled horizontally) is equal to 0 .

What is scroll offset?

forward, then scroll offset is the amount the top of the sliver has been scrolled past the top of the viewport. This value is typically used to compute whether this sliver should still protrude into the viewport via SliverGeometry.


1 Answers

Use window.innerHeight and multiply it by fraction. For example, if you want 60 vh use (window.innerHeight)*0.6.

I'm not familliar with jQuery, but with vanillaJS it would be

if (window.pageYOffset > window.innerHeight)
like image 101
Przemex Przemax Avatar answered Oct 07 '22 07:10

Przemex Przemax