user967451
user967451

Reputation:

How to make jQuery waypoints plugin fire when an element is in view and not scrolled past?

Please see this:

http://jsfiddle.net/5Zs6F/2/

As you can see only when you scroll past the first red rectangle it turns blue, I would like it to turn blue the moment it enters into view. This is why the second never turns blue because there isn't enough content underneath it to allow you to scroll past it.

HTML:

Scoll this window pane
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<div class="box"></div>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<div class="box"></div>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />

CSS:

.box { width: 250px; height: 100px; border: 2px solid red;  }

jQuery:

$.getScript('http://imakewebthings.com/jquery-waypoints/waypoints.min.js', function() {


    $('.box').waypoint(function() {

        $(this).css({
            borderColor: 'blue'
        });

    });


});

How to make it fire as soon as the element in question is seen and not scrolled past?

Upvotes: 18

Views: 28345

Answers (4)

chiku
chiku

Reputation: 60

If you want to solve this issue with css then also you can do it by using below css.

img[data-src]::before {
    content: "";
    display: block;
    padding-top: 70%;
  }

We are trying to use pseudo-elements (e.g. ::before and ::after) to add decorations to img elements.

Browsers don't render an image’s pseudo-elements because img is a replaced element, meaning its layout is controlled by an external resource.

However, there is an exception to that rule: If an image’s src attribute is invalid, browsers will render its pseudo-elements. So, if we store the src for an image in data-src and the src is empty, then we can use a pseudo-element to set an aspect ratio:

As soon as the data-src becomes the src, the browser stops rendering ::before and the image's natural aspect ratio takes over.

Upvotes: 0

Paul
Paul

Reputation: 11

OK. Found a solution which works fine.

    jQuery('.zoomInDownInaktiv').waypoint(function(direction) {
    if (direction === "down") {
        jQuery(this.element).removeClass('zoomInDownInaktiv');
        jQuery(this.element).addClass('zoomInDown');
    }
}, { offset: '80%' });

Upvotes: 0

Paul
Paul

Reputation: 11

Won't work for me. I have serveral classes with the same name and they will all changed if the page loads / first element is on screen.

jQuery(document).ready(function(){

jQuery('.zoomInDownInaktiv').waypoint(function() {
    //jQuery(this).removeClass('zoomInDownInaktiv');
    jQuery(this).addClass('zoomInDown');
}, { offset: 'bottom-in-view' }); 

});

Upvotes: 0

imakewebthings
imakewebthings

Reputation: 3398

The offset option determines where in relation to the top of the viewport the waypoint should fire. By default it is 0, so your element fires when it hits the top. Because what you want is common, waypoints includes a simple alias for setting the offset to fire when the whole element comes into view.

$('.box').waypoint(function() {
  $(this).css({
    borderColor: 'blue'
  });
}, { offset: 'bottom-in-view' });

If you want it to fire when any part of the element peeks in from the bottom, you should set it to '100%'.

Upvotes: 41

Related Questions