This question already has answers here:
How to Check if element is visible after scrolling?
                                
                                    (40个答案)
                                
                        
                                5年前关闭。
            
                    
为了节省服务器上的资源,我在所有帖子上都使用了相同的图像视图,并且我想在div向用户显示的同时更改该图像,同时向下滚动页面以节省带宽。
码:

<div class="image_showen" s="">
  <a href="{link}" target="">
    <img src="images/defaultimage.jpg" alt="{thread_title}" />
  </a>
</div>


我的问题是如何检测div是否在视口上并将其传递给向服务器发送请求以获取该图像的函数。

最佳答案

找到Appear Plugin。可能为您工作。当特定的div出现要查看时,则显示图像。您不需要去服务器。只需将imagePath保存为隐藏字段,并在div出现后立即显示。

包含API之后,您可以轻松编写如下内容:

$('someselector').on('appear', function(event, $all_appeared_elements) {
  // this element is now inside browser viewport... so show the image you need...
});

09-25 18:33
查看更多