13

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-slider.html

What I'm looking for is a way to check what section is viewable in the window to set the active state on the nav when using the browsers scroll bar, any ideas?

Jacobi
  • 1,308
  • 13
  • 27
Ashley Graham
  • 131
  • 1
  • 3

5 Answers5

17

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:

Luke
  • 14,840
  • 10
  • 84
  • 92
4

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.

ValeriiVasin
  • 8,240
  • 11
  • 53
  • 75
1
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

Community
  • 1
  • 1
bravedick
  • 6,554
  • 2
  • 22
  • 45
0

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.

Kae Verens
  • 3,806
  • 3
  • 19
  • 36
0

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");
    }); 
Maksym
  • 17,924
  • 5
  • 22
  • 29