2012-10-20 66 views

回答

2

您可以使用這樣的事情:

var $element = $('div'); 

$(window).scroll(function() { 
    var scroll = $(window).scrollTop() + $(window).height(); 
    var offset = $element.offset().top + $element.height(); 

    if (scroll > offset) { 
     $element.css('background', 'blue'); 
    } else { 
     $element.css('background', 'red'); 
    } 
}); 

演示:http://jsfiddle.net/eNjEs/5/

+0

這可以提高遠一點 - const $element = $('div'); $(window).scroll(function() { const scroll = $(window).scrollTop() + $(window).height(); const offset = $element.offset().top + $element.height(); const colour = (scroll > offset) ? 'blue' : 'red'; $element.css('background', colour); }); Stefano

相關問題