Search code examples
javascriptjqueryhtmlwindowviewport

Can we get the object of current view in browser?


I want the element which are currently visible to the end user on his/her screen. I want this thing to be done through javascript. Can I do that??

For eg- On user screen, suppose he is see images currently. So I want the elements(img element in this case) which are visible to him.


Solution

  • you can use window.innerWidth and window.innerHeight to get the width and height of the viewport, and use element.getBoundingClientRect to get the position of the element relative to the top-left of the viewport. From there you can check if the element is in the viewport or not