Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for when element is visible on the screen?

Tags:

In angular, I want to trigger a function and/or set a css-class once an element is actually visible on the screen, as in once you scroll down for example. What's the best practice for this?

like image 438
Himmators Avatar asked Nov 25 '13 10:11

Himmators


People also ask

How do you know if an element is visible on screen?

Summary. Use the getBoundingClientRect() method to get the size of the element and its relative position to the viewport. Compare the position of the element with the viewport height and width to check if the element is visible in the viewport or not.

What is element visibility?

Google Tag Manager's element visibility trigger fires when a selected element becomes visible in the web browser's viewport.


1 Answers

I was trying to do the same thing and after lot of research I found a directive that does exactly what I needed:

https://github.com/thenikso/angular-inview

It was also very easy to setup. In case you run into issues with this then let me know and I can help you out.

like image 160
whizcreed Avatar answered Sep 20 '22 17:09

whizcreed