简体   繁体   English

当元素在视图中且未滚动过去时,如何使 jQuery 航路点插件触发?

[英]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/ 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: 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: CSS:

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

jQuery: 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?如何在看到有问题的元素而不是滚动过去时立即触发它?

The offset option determines where in relation to the top of the viewport the waypoint should fire. offset选项决定了航路点应该在相对于视口顶部的哪个位置触发。 By default it is 0, so your element fires when it hits the top.默认情况下它是 0,所以你的元素在它到达顶部时会触发。 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%'.如果您希望它在元素的任何部分从底部窥视时触发,您应该将其设置为“100%”。

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

}); });

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

If you want to solve this issue with css then also you can do it by using below css.如果你想用 css 解决这个问题,那么你也可以使用下面的 css 来解决。

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

We are trying to use pseudo-elements (eg ::before and ::after) to add decorations to img elements.我们正在尝试使用伪元素(例如:::before 和 ::after)为 img 元素添加装饰。

Browsers don't render an image's pseudo-elements because img is a replaced element, meaning its layout is controlled by an external resource.浏览器不会渲染图像的伪元素,因为 img 是一个替换元素,这意味着它的布局由外部资源控制。

However, there is an exception to that rule: If an image's src attribute is invalid, browsers will render its pseudo-elements.但是,该规则有一个例外:如果图像的 src 属性无效,浏览器将呈现其伪元素。 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:因此,如果我们将图像的 src 存储在 data-src 中并且 src 为空,那么我们可以使用伪元素来设置纵横比:

As soon as the data-src becomes the src, the browser stops rendering ::before and the image's natural aspect ratio takes over.一旦 data-src 成为 src,浏览器就会停止渲染 ::before 并且图像的自然纵横比接管。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM