0

I have many instances of a fadein css class. What I want to happen is for when I scroll to each div with the fadein class, that that div fades in. I've looked at Check if element is visible after scrolling, but I'm not sure how to use it. This doesn't seem to work:

$(window).scroll(function () {
    $('.full-pull-quote-text-fadein').each(function (i) {
        if (isScrolledIntoView(this) == 'true') {
            alert();
        }
    });

});
function isScrolledIntoView(elem)
{
    var $elem = $(elem);
    var $window = $(window);

    var docViewTop = $window.scrollTop();
    var docViewBottom = docViewTop + $window.height();

    var elemTop = $elem.offset().top;
    var elemBottom = elemTop + $elem.height();

    return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop));
}

What am I doing wrong, please?

Community
  • 1
  • 1
LauraNMS
  • 2,420
  • 7
  • 29
  • 63
  • should it return ((elemBottom <= docViewBottom) || (elemTop >= docViewTop)) instead? – Jesse Oct 19 '15 at 19:00
  • @Jesse: No, it still doesn't work. – LauraNMS Oct 19 '15 at 19:03
  • Probably the best way to get a complete answer would be to make a jsfiddle with your complete code. Otherwise, we'll just be guessing at possible solutions. – Kevin Lee Oct 19 '15 at 19:05
  • I'd really suggest you to use a parallax scrolling library. I've used https://github.com/Prinzhorn/skrollr before and it's really cool to see what's possible (demo on https://ihatetomatoes.net/demos/parallax-scroll-effect/). As this library is not active anymore, and you are using jQuery, there are many others yu can choose from. – msoft Oct 19 '15 at 19:08

3 Answers3

0

TL;DR - Fiddle at http://jsfiddle.net/esxf0vLh/2/

It's hard to help you solve your exact problem without seeing more specific samples of the project you're working on, but I can help you understand a bit more about the code you've posted.

To keep things as simple as possible, I've stuck to your exact request describing a <div> becoming visible when it scrolls into view. For now I haven't taken care of it becoming invisible again if it's not in view, but this code could be a starting point for that with more tinkering :)

Firstly, let's imagine a group of <div> elements:

<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>

Secondly, let's give them some styles. By default, our elements will be invisible. We have an extra .is-visible class which can make them visible - we'll use jQuery later to add it when the time comes:

div {
  background: rebeccapurple;
  opacity: 0;
}

div.is-visible {
  opacity: 1;
}

jQuery time! Let's make a function that takes an element as an argument, and then returns true or false based on whether the top of that element has passed the bottom of our viewport (ie, the element has scrolled into the viewport):

function isScrolledIntoView(element) {
  // Calculate the current scroll position of the bottom of your
  // viewport - that's the line an element has to pass to qualify 
  // for becoming visible
  var scrollBottomPosition = $(window).scrollTop() + $(window).height();

  // Tell us whether it's true or false that the scroll position of
  // the top of your element is less than the current scroll position
  // of the bottom of your viewport - ie the element has entered the
  // viewport
  return ($(element).offset().top < scrollBottomPosition);
}

This function is going to come in handy. We'll give it a particular element we care about, and then keep firing the function every time the user scrolls. But not yet.

First we're going to make a function to add the .is-visible class to an element of our choosing.

// Takes an element as an argument - we'll give it one later.
function addClassIfVisible(element) {

  // For each of that element, it asks "Is the element scrolled
  // into view?" using the function we've made to answer that question.
  // It'll give us true or false. If true, it adds the `.is-visible` class.
  $(element).each(function () {      
    if (isScrolledIntoView(this)) {
      $(this).addClass('is-visible');
    }
  });
}

Almost done. Lastly, let's set the stage. Firstly, I'd say we want this function to fire once as soon as the page loads - any elements above the fold will immediately fade into view.

Then, we add an event listener to keep firing the function every time the user scrolls. That way, it continually checks all the divs on the page and asks of each one, "Is this element visible above the fold?". And for each one, it's ready to add the .is-visible class when it gets the answer "Yes, this element is visible!":

// Run the function once first for any `div`
addClassIfVisible('div');

// Create an event listener that will run this function for any `div`
// every time the user scrolls.
$(window).scroll(function () {
  addClassIfVisible('div');
});

Have a look at the working version at http://jsfiddle.net/esxf0vLh/2/ - you might need to adapt the above code depending on exactly what you're trying to achieve, but hopefully it helped. Let me know how you get on!

0

Here, this makes content fly in sideways while fading in on scroll.

I would not use it for production sites though, well maybe i would:-)

<div class="bg"></div>
<div id="menu">
<ul>
<li id="one" class="fiirst"><a href="index.html" class="transition">Home</a>
</li>

http://codepen.io/damianocel/pen/EVojrx

Relevant code is line 1-67

damiano celent
  • 689
  • 6
  • 12
0

I used waypoints to detect when my divs scrolled into view.

LauraNMS
  • 2,420
  • 7
  • 29
  • 63