Skip to content Skip to sidebar Skip to footer

How To Find Scroll Top Value For Div

I am working on responsive site. We need when a user see video section that's need to autoplay. If set scroll top for video tag is not working. But i set scrolltop to window it's w

Solution 1:

You need use offest().top to get distance to reach video element. scrollTop will get window scroll distance not distance to an element. Just use this:

$('#test').offset().top

jsFiddle

   $(window).scroll(function () {
      var scrollTop = $(window).scrollTop();
       var scrollToVid = $('#test').offset().topconsole.log(scrollTop);
        console.log(scrollToVid);

        if ($(window).scrollTop() >= scrollToVid) {
        alert('You reached to the video!');
        }
      });

Solution 2:

You can also use

document.body.scrollTop + Element.getBoundingClientRect().top

  1. document.body.scrollTop is the offset of document had scrolled out viewport
  2. Element.getBoundingClientRect().top is the offset of element to viewport top.

Post a Comment for "How To Find Scroll Top Value For Div"