Scroll if element is not visible

前端 未结 9 723
旧时难觅i
旧时难觅i 2020-12-14 17:57

how to determine, using jquery, if the element is visible on the current page view. I\'d like to add a comment functionality, which works like in facebook, where you only sc

9条回答
  •  感动是毒
    2020-12-14 18:32

    Live Demo

    Basically you just check the position of the element to see if its within the windows viewport.

    function checkIfInView(element){
        var offset = element.offset().top - $(window).scrollTop();
    
        if(offset > window.innerHeight){
            // Not in view so scroll to it
            $('html,body').animate({scrollTop: offset}, 1000);
            return false;
        }
       return true;
    }
    

提交回复
热议问题