使用 datasrc、live 和 Inview 插件滚动到视口时图像会淡入淡出。

发布于 2024-12-12 00:18:06 字数 1364 浏览 1 评论 0原文

我非常接近破解这个问题,或者破解,我还不确定是哪一个。也许两者都有。

我通过无限滚动 javascript 在页面上加载了一系列图像(有些相当大)(显然是整个万维网上唯一可以与 tumblr 配合使用的图像,很好)http://syndex.me

经过大量、大量的研究,我终于找到了 inview 插件 这似乎就是我想要的。我有两种使用它的方法,每种方法都有很大的问题。


方法 1

此方法的问题是我会收到数百个控制台日志。我认为这个插件的全部意义在于当一个非常具体的事件发生时执行 doStuff。即一个img进入视野。我必须使用 live,因为 imgs 是动态加载的。我尝试过解除绑定、停止传播等,但无济于事。

$('img').live('inview', function(event, isInView, visiblePartX, visiblePartY) {
  if (isInView) {
      //fadein the image
      console.log("I've just done something")
  }
  });

方法 2(演示位于http://syndex.me

图像“跳”到位一旦它们完成加载(位置,而不是不透明度)。

我可以想象人们会说,是的,那是因为浏览器在 100% 加载之前不知道图像的高度。但我见过无数网站设法做到这一点。也许我需要在不使用“加载”的情况下执行此操作?但那会是什么?

$(function() {
 $('img[data-src]').live('inview', function(event, isVisible) {
  if (!isVisible) { return; }

  var img = $(this);

  // Show a smooth animation
  img.load(function() { img.parent().animate({ opacity: 1 }); });

  // Change src
  img.attr('src', img.attr('data-src'));

  // Remove it from live event selector
  img.removeAttr('data-src');
  console.log(img.height())
 });
});

I'm pretty close to cracking this, or cracking up, i'm not sure which yet. Maybe both.

I have a series of images loading on the page (some quite big) via an infinite scroll javascript (the only one that works with tumblr apprently, on the entire world wide web, nice) http://syndex.me

After much,much research I've come to find a fork of the inview plugin which seems to be what I'm after. I have two ways of using it, each have big problems.


Method 1

The problem with this method is that it i get hundreds of console logs. I thought the whole point of this plugin was to doStuff when a very specific event happened. ie an img came into view. I have to use live, as imgs are being loaded dynamically. I have tried to unbind, stop propogation etc, to no avail.

$('img').live('inview', function(event, isInView, visiblePartX, visiblePartY) {
  if (isInView) {
      //fadein the image
      console.log("I've just done something")
  }
  });

Method 2 (demo at http://syndex.me)

The images "jump" into position as soon as they have finished loading (position, not opacity).

I can imagine people saying, yes thats because the browser does not know the height of the image until it's 100% loaded. But I've seen countless sites manage to do this. Maybe i need to do this without using "load" then? But what would that be?

$(function() {
 $('img[data-src]').live('inview', function(event, isVisible) {
  if (!isVisible) { return; }

  var img = $(this);

  // Show a smooth animation
  img.load(function() { img.parent().animate({ opacity: 1 }); });

  // Change src
  img.attr('src', img.attr('data-src'));

  // Remove it from live event selector
  img.removeAttr('data-src');
  console.log(img.height())
 });
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陪我终i 2024-12-19 00:18:06

好吧,如果你尝试一下这个: http://www.appelsiini.net/projects/lazyload

你可以很容易地实现同样的目标......祝你好运!

=======================================编辑============ ==============================

好的,所以我浏览了 http://syndex.me,并且它们不会加载特定图像,而是通过 ajax 加载整个页面(如果您检查的话)。

好吧,让我们回到问题上来。如果您想跟踪如何跟踪某个元素是否进入视口,那么这非常简单。

您只需要获取窗口的尺寸(如果垂直滚动在此处工作,则为高度)。
在窗口上绑定滚动事件并检查 element.offsetTop 并将其与 document.body.scrollTop - window.innerHeight 进行比较,以了解 element.offsetTop 是否更大。如果是的话,田田!那是在视口中。

最初使用原始 src 设置图像中的 data-src 属性,并在 [tada!] 条件下,将其设置为 src 属性并删除 data-src 属性。

在设置图像中的 src 属性时,请始终检查 data-src 是否可用,以便当您删除 data-src 时,不会多次重新设置。

良好的结构应该是:

var winHeight = window.innerHeight,
    winPos = document.body.scrollTop,
    elePos = element.offsetTop;

window on scroll = function(){
    winPos = document.body.scrollTop; // get the current position
    winHeight = window.innerHeight; // get the current height as what if window is resized

    if(elePos > winPos - winHeight){
        // Tada! this is in the viewport

        if(image.getAttrubute("data-src")){
            image.setAttribute("src")  = image.getAttrubute("data-src");
            image.removeAttribute("data-src");
        }
    }
}

默认情况下,您可以在每个图像上放置一个占位符,这样就不会在 w3 验证中引发错误。

在图像上,您可以绑定加载事件以使其具有动画效果。因为一开始就让它不可见。

这是一段粗略的(类似伪)代码,而不是实际的代码。所以看逻辑而不是语法。

好吧,我想这会对你有帮助!

Alright, so what if you try this: http://www.appelsiini.net/projects/lazyload.

You can achieve the same very easily... Good luck!!

===================================== EDIT ========================================

OK, so I gone through http://syndex.me, and they are not loading a particular image, instead they are loading an entire page via ajax if you check.

Well, lets come back to the problem. If you want to track how to track if some element is coming in the viewport, then this is quite simple.

You just need to get the dimention of the window (height if vertical scroll is working here).
Bind scroll event on the window and check for the element.offsetTop and compare it with document.body.scrollTop - window.innerHeight to know if element.offsetTop is greater or not. If yes, tada! that is in the viewport.

Set the data-src attribute in the image with the original src initially and on the [tada!] condition, set it to src attribute and remove the data-src attribute.

while setting the src attribute in the image, always check if data-src is available or not, so that when you remove the data-src, that re-setting doesn't happen multiple times.

well structure should be:

var winHeight = window.innerHeight,
    winPos = document.body.scrollTop,
    elePos = element.offsetTop;

window on scroll = function(){
    winPos = document.body.scrollTop; // get the current position
    winHeight = window.innerHeight; // get the current height as what if window is resized

    if(elePos > winPos - winHeight){
        // Tada! this is in the viewport

        if(image.getAttrubute("data-src")){
            image.setAttribute("src")  = image.getAttrubute("data-src");
            image.removeAttribute("data-src");
        }
    }
}

By Default, you can put a placeholder on the every image, so that doesn't throw error on w3 validations.

On the image, you can bind load event to make it animated. for that keep it invisible initially.

This is a rough (like pseudo) code, not the actual one. so see the logic not the syntax.

Well, I think this will help you!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文