尝试使用 javascript 停止图像加载

发布于 2024-12-04 17:17:55 字数 235 浏览 0 评论 0原文

有没有办法用javasript停止图像下载?我想从图像标签中提取所有网址,并仅在用户滚动到特定标签时才开始加载图像。我知道我可以通过以下方式停止下载

window.stop()

但是通过使用此解决方法,浏览器也会停止加载 CSS 文件中定义的背景图像。

那么有没有一种方法可以在不实现“标记解决方法”(例如“将图像 url 包含到跨度或其他内容中”)的情况下实现这一目标。

is there a way to halt the image download with javasript? I would like to extract all urls from the image tags and start the image loading only when the user scrolls to a specific one. I know that I can stop the download via

window.stop()

But by using this workaround the browser stops also loading the background images which are defined in the CSS file(s).

So is there a way to achieve this without implementing a "markup workaround" such as "including the image url into a span or something".

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

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

发布评论

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

评论(2

梦行七里 2024-12-11 17:17:55

您可以使用 JavaScript 完全删除 src 属性,这将有效地阻止图像被下载。

var img = document.getElementById('imageID'),
    src = img.getAttribute('src');

img.removeAttribute('src');

这将确保如果用户禁用了 JS,您的图像仍然会被加载,因为您稍后将使用 JS 删除 src 属性。

然后,您可以将 url 存储在变量中,并在想要再次加载它们时将其设置回来。

img.setAttribute('src', src);

关键点是不要留下空的 src 属性(src=""),否则浏览器将把它视为“/”,实际上是在尝试加载您的主页并将其存储在图像中元素。您必须完全删除 src 属性。

You can remove the src attribute entirely using JavaScript, this will effectively stop the images from being downloaded.

var img = document.getElementById('imageID'),
    src = img.getAttribute('src');

img.removeAttribute('src');

This will ensure your images will still be loaded if a user has JS disabled, because you are removing the src attributer later on using JS.

You can then store the url in a variable and set it back when you want to load them again.

img.setAttribute('src', src);

The key point it not leaving an empty src attribute (src=""), otherwise it will be treated by the browser as '/', actually trying to load your home page and store it in the image element. You have to remove the src attribute entirely.

萌面超妹 2024-12-11 17:17:55

我遇到了类似的问题,以下是我的解决方法:

简而言之:

在创建元素时,我制作了 src='Blank.gif' 并创建了 setAttribute("data-src",response[i].photo_url)。由于 Blank.gif 不存在,如果下面的代码不起作用,或者加载速度不够快,它将被渲染为损坏的图像链接...

然后,onscoll 我查看了一下当它在视图中并将 src 设置为“data-src”时。这对我来说非常有效...

详细信息!

window.onscroll = function() {

  var top = document.body.scrollTop;
  var bottom = document.documentElement.clientHeight + top;

  var img;

  for (var i=startImage;i<index;i++){
    img = document.getElementById("image" + i);
    var a = img.style.top.substring(0,img.style.top.length-2);
    var b = bottom;//Just for simplicity...
    if (a < b) {
        if (img.src.indexOf('Blank.gif') != -1) {
            img.src = img.getAttribute("data-src");
        }
        startImage++;
    }
  }
}

希望这可以节省其他人 2 1/2 小时的搜索/工作!

如果您认为有一种方法可以提高上述效率,请随时告诉我!

I had a similar problem and here's how I solved it:

In a nut shell:

When creating the element I made src='Blank.gif' and I created setAttribute("data-src",response[i].photo_url). Seeing as Blank.gif doesn't exist, it would be rendered as a broken image link if the below didn't work, or it cant load fast enough...

Then, onscoll i checked to see when it was in view and set the src to the 'data-src'. This works very well for me...

The details!

window.onscroll = function() {

  var top = document.body.scrollTop;
  var bottom = document.documentElement.clientHeight + top;

  var img;

  for (var i=startImage;i<index;i++){
    img = document.getElementById("image" + i);
    var a = img.style.top.substring(0,img.style.top.length-2);
    var b = bottom;//Just for simplicity...
    if (a < b) {
        if (img.src.indexOf('Blank.gif') != -1) {
            img.src = img.getAttribute("data-src");
        }
        startImage++;
    }
  }
}

Hope this can save someone else 2 1/2 hours of searching/work!

Feel free to let me know if you think there's a way to make the above more efficient!

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