search

Home  >  Q&A  >  body text

javascript - How does JS dynamically load images determine whether the image is on the screen?

Scroll down with the mouse and load the image when the target area appears on the screen. So how to determine whether the target area appears on the screen?

世界只因有你世界只因有你2804 days ago512

reply all(2)I'll reply

  • 阿神

    阿神2017-05-19 10:22:40

    Use the getBoundingClientRect() method of the image to return the left, top, right and bottom relative to the viewport
    to determine whether it is within the viewport.

    Refer to getBoundingClientRect

    reply
    0
  • 高洛峰

    高洛峰2017-05-19 10:22:40

    Calculate the distance from the image to the head of the page, and then detect the scrolling distance of the scroll bar

    reply
    0
  • Cancelreply