jQuery - function of viewport

0 votes
217 views
added Jul 31, 2019 in jQuery by LC Marshal Captain (25,790 points)
//viewport function
$.fn.isInViewport = function() {
    var elementTop = $(this).offset().top,
        elementBottom = elementTop + $(this).outerHeight(),
        viewportTop = $(window).scrollTop(),
        viewportBottom = viewportTop + $(window).height();

    return elementBottom > viewportTop && elementTop < viewportBottom;
};

$(window).on('resize scroll', function() {
  if ($('#nexpost').isInViewport()) {
    window.history.pushState("", "", '/newpage');
  } else {
    // do something else
  }
});

 

lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...