Check if div is viewable in window?

前端 未结 5 1337
庸人自扰
庸人自扰 2021-01-04 05:33

I have a one page site with fixed navigation and using a scroll script, very similar to this: http://www.ivanjevremovic.in.rs/live/temptation/single/orange/index-cycle-slide

相关标签:
5条回答
  • 2021-01-04 06:22

    Use $('#element').offset().top; to detect element top side.

    $(window).scrollTop(); to detect current scroll position.

    And $(window).height(); to detect current window height.

    And after that steps you actually need only something easy math calculations.

    0 讨论(0)
  • 2021-01-04 06:31
    function isScrolledIntoView(elem)
    {
        var docViewTop = $(window).scrollTop();
        var docViewBottom = docViewTop + $(window).height();
    
        var elemTop = $(elem).offset().top;
        var elemBottom = elemTop + $(elem).height();
    
        return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom));
    }
    

    source: Check if element is visible after scrolling

    0 讨论(0)
  • 2021-01-04 06:32

    Here are all the variables you'll need...

    var $myElt       = $('.myElement');      // whatever element you want to check
    var $window      = $(window);            // the window jQuery element
    var myTop        = $myElt.offset().top;  // the top (y) location of your element
    var windowTop    = $window.scrollTop();           // the top of the window
    var windowBottom = windowTop + $window.height();  // the bottom of the window
    

    Then to make sure your element is within the window's range...

    if (myTop > windowTop && myTop < windowBottom) {
        // element is in the window
    } else {
        // element is NOT in the window
        // maybe use this to scroll... 
        // $('html, body').animate({scrollTop: myTop}, 300);
    }
    

    jQuery reference:

    • http://api.jquery.com/offset/
    • http://api.jquery.com/height/
    • http://api.jquery.com/scrollTop/
    0 讨论(0)
  • 2021-01-04 06:34

    see the following lazyload plugin:

    http://plugins.jquery.com/files/jquery.lazyload.js__6.txt
    

    the section which starts with the comment "return the status of the item relative to the current view" checks to see if an element is visible in the viewport.

    0 讨论(0)
  • 2021-01-04 06:36

    If you are using jQuery just try to check the document position

    $('html').position().top;
    

    for example:

    $(document).bind("scroll", checkLink);
    
    function checkLink(){
    
       /* Position will checked out after 1 sec when user finish scrolling  */
       var s = setTimeout(function(){ 
    
         var docHeight = $('html').position().top;
         var allLinks = $('.navigation a');
    
         if ( docHeight < 0 && docHeight <= -1000 ) { 
    
            allLinks.removeClass('active');
            $('a.firstlink').addClass('active');
    
         } else 
         if ( docHeight < -1000 && docHeight <= -2000 ) { 
    
            allLinks.removeClass('active');
            $('a.secondlink').addClass('active');
    
         } else { /* .... */ }
    
       $(document).bind("scroll", checkLink);
       }, 1000);
    
      $(document).unbind('scroll');
    }
    

    but guys in your example haven't held on this for a long time :) they just toggle classes on click

    $('#navigation').localScroll();
        $('#navigation li a').click( function () {
            $('#navigation li a').removeClass("active");
            $(this).addClass("active");
        }); 
    
    0 讨论(0)
提交回复
热议问题