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

前端 未结 4 1088
不思量自难忘°
不思量自难忘° 2020-12-16 13:34

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 th

相关标签:
4条回答
  • 2020-12-16 13:41

    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%' });
    
    0 讨论(0)
  • 2020-12-16 13:52

    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' }); 
    

    });

    0 讨论(0)
  • 2020-12-16 13:58

    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%'.

    0 讨论(0)
  • 2020-12-16 14:02

    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.

    0 讨论(0)
提交回复
热议问题