jQuery fade out elements as they scroll off page, fade back as they scroll back on

牧云@^-^@ 提交于 2019-11-27 22:20:40

问题


I want elements to fade out as they scroll off the top of the page, and then fade back in as they scroll back onto the page. I wrote some code that works, but I am looking for a more elegant solution. Here is the solution I have working on jsfiddle: http://jsfiddle.net/wmmead/JdbhV/3/

I would like to find a much shorter, more elegant piece of code, but just can't quite work it out. Maybe something with an array and the each() method? If I put a class on the divs instead of an ID, it gets a lot shorter, but then they all fade out at once. I want each box to fade out as it scrolls off the page.

HTML

<div id="box1"></div>
<div id="box2"></div>
<div id="box3"></div>
<div id="box4"></div>
<div id="box5"></div>
<div id="box6"></div>

CSS

#box1, #box2, #box3, #box4, #box5, #box6 {
  width: 100px;
  height: 100px;
  background: orange;
  margin:100px auto;
}
#box6 {
  margin-bottom:600px;
}

jQuery

var box1Top = $('#box1').offset().top;
var box2Top = $('#box2').offset().top;
var box3Top = $('#box3').offset().top;
var box4Top = $('#box4').offset().top;
var box5Top = $('#box5').offset().top;
var box6Top = $('#box6').offset().top;

$(window).scroll(function () {
if ((box1Top - $(window).scrollTop()) < 20) {
    $('#box1').stop().fadeTo(100, 0);
} else {
    $('#box1').stop().fadeTo('fast', 1);
}

if ((box2Top - $(window).scrollTop()) < 20) {
    $('#box2').stop().fadeTo(100, 0);
} else {
    $('#box2').stop().fadeTo('fast', 1);
}

if ((box3Top - $(window).scrollTop()) < 20) {
    $('#box3').stop().fadeTo(100, 0);
} else {
    $('#box3').stop().fadeTo('fast', 1);
}

if ((box4Top - $(window).scrollTop()) < 20) {
    $('#box4').stop().fadeTo(100, 0);
} else {
    $('#box4').stop().fadeTo('fast', 1);
}

if ((box5Top - $(window).scrollTop()) < 20) {
    $('#box5').stop().fadeTo(100, 0);
} else {
    $('#box5').stop().fadeTo('fast', 1);
}
if ((box6Top - $(window).scrollTop()) < 20) {
    $('#box6').stop().fadeTo(100, 0);
} else {
    $('#box6').stop().fadeTo('fast', 1);
}
});

回答1:


You can use attribute selector '[attr="someattr"]' with use of .each() method of jQuery:

$(window).scroll(function () {
   $('[id^="box"]').each(function () { // <---loop the divs id starts with #box 
      if (($(this).offset().top - $(window).scrollTop()) < 20) { //<---mark the $(this).offset().top of current object
          $(this).stop().fadeTo(100, 0); //<----fadeOut the current obj
      } else {
          $(this).stop().fadeTo('fast', 1); //<----fadeIn the current obj
      }
   });
});

You can take a demo here:

DEMO




回答2:


This version of your jsfiddle is considered better for several reasons:

  • Well functionally decomposed (its broken into little functions): higher readability for you and other coders, and easier to maintain in the future if you need to change something.
  • Flexibility: you can change the number of boxes, without even knowing the javascript!
  • Efficiency: due to functional decomposition, it means that each line of code only gets called if 100% necessary

Apart from the javascript, i added classes in addition to existing id's. eg: id="box1" class="box"

enjoy :)




回答3:


Well, I guess just set a class for all your divs. Instead of calling each one with an #id



来源:https://stackoverflow.com/questions/15894157/jquery-fade-out-elements-as-they-scroll-off-page-fade-back-as-they-scroll-back

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!