使用jQuery:
$(window).scroll(function(){
if($(window).scrollTop() === 10){
$('.element').fadeOut();
}
});
小提琴:http: //jsfiddle.net/Hive7/vV7Wd/2/
添加更多用途:
if ($(window).scrollTop() >= "number of pixels") {
if ($('"button plus number"').css('display') === 'none') {
$('"button plus number"').fadeIn('slow');
$('"button plus number"').prev().fadeOut();
$('"button plus number"').next().fadeOut();
}
}
双引号中的元素由您设置
示例(对于数字 4):
if ($(window).scrollTop() >= 400) {
if ($('button4').css('display') === 'none') {
$('button4').fadeIn('slow');
$('button4').prev().fadeOut();
$('button4').next().fadeOut();
}
}
或者使用 for 循环:
$(window).scroll(function () {
for (i = 0; i <= 20; i++) {
if ($(window).scrollTop() >= (i * 100)) {
if ($(window).scrollTop() <= ((i * 100) + 100)) {
$('.button' + i).fadeIn('slow');
$('.button' + i).prev().fadeOut();
$('.button' + i).next().fadeOut();
}
}
}
});
for 循环更好,因为这意味着您每次添加一件事时只需实现一件事,这是 for 循环中的条件