如何预加载大尺寸图像?

发布于 2024-09-10 14:37:18 字数 1006 浏览 0 评论 0原文

我有某些链接,将鼠标悬停在这些链接上时,我正在更改

背景图像

我使用过的 jQuery 是-

function imgchange()
    {
        $('.smenu li').mouseover( function(){
        var src = $(this).find('a').attr('href');
        $('.hbg').css('background-image', 'url(' + src + ')');
        $(this).find('hbg').attr('title', 'my tootip info');
        });     
    }

它工作正常,但问题是当我在服务器图像上运行它时需要 3 -4 秒在更改时更改,但第二次我将鼠标悬停在图像上时会立即更改,我认为这是因为浏览器在缓存中存储了图像。因此,我在 onLoad() ivent -

<script language = "JavaScript">
function preloader() 
{
heavyImage = new Image(); 
heavyImage.src = "images/soluinfo1.jpg";
heavyImage.src = "images/soluinfo2.jpg";
heavyImage.src = "images/soluinfo3.jpg";
heavyImage.src = "images/soluinfo4.jpg";
heavyImage.src = "images/soluinfo5.jpg";
heavyImage.src = "images/soluinfo6.jpg";
heavyImage.src = "images/soluinfo7.jpg";
}
</script>

页面上添加了一个 javascript 来预加载图像,但此脚本并没有解决我的问题。我该怎么办?

i have certain links, on mouse over of those links I am changing <div> background image

jQuery I have used is-

function imgchange()
    {
        $('.smenu li').mouseover( function(){
        var src = $(this).find('a').attr('href');
        $('.hbg').css('background-image', 'url(' + src + ')');
        $(this).find('hbg').attr('title', 'my tootip info');
        });     
    }

It is working fine but the problem is when I running it on server images takes 3-4 sec to be changed on change, but the second time I do mouse over images are getting changed instantly, I think this is because of browser stored images in cache. So I added one javascript to preload images on page onLoad() ivent -

<script language = "JavaScript">
function preloader() 
{
heavyImage = new Image(); 
heavyImage.src = "images/soluinfo1.jpg";
heavyImage.src = "images/soluinfo2.jpg";
heavyImage.src = "images/soluinfo3.jpg";
heavyImage.src = "images/soluinfo4.jpg";
heavyImage.src = "images/soluinfo5.jpg";
heavyImage.src = "images/soluinfo6.jpg";
heavyImage.src = "images/soluinfo7.jpg";
}
</script>

<body onLoad="javascript:preloader()">

but this script has not solved my problem.what should I do?

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

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

发布评论

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

评论(2

听风吹 2024-09-17 14:37:18

@Richard的答案(精灵)可能是您所追求的最好的答案,但您的代码不起作用的原因是在大多数浏览器中,只有最后一个 heavyImage.src="" 给出了足够的作为实际请求实际向浏览器注册的时间。您仅创建一个 Image 对象设置并重置 .src 属性,其速度比浏览器请求文件的速度要快(我认为现代 JavaScript 引擎采取了删除所有内容的附加步骤)中间 .src 语句就是因为这个原因)。

有几种方法可以解决这个问题。最简单的方法是创建多个 Image 对象,每个图像对应一个对象。最简单的方法是通过这样的方法:

<script type="text/javascript">
function preloader() 
{
    function doPreload(strImagePath) 
    {
        var heavyImage = new Image();
        heavyImage.src = strImagePath;
    }

    var arrImages = ["images/soluinfo1.jpg","images/soluinfo2.jpg","images/soluinfo3.jpg","images/soluinfo4.jpg","images/soluinfo5.jpg","images/soluinfo6.jpg","images/soluinfo7.jpg"];
    for (var i = 0; i < arrImages.length; i++) {
        doPreload(arrImages[i]);
    }
}
</script>

通过将 heavyImage 变量放入其自己的函数中(记住使用 var 关键字),您可以确保Image 对象存在于其自己的专用范围内。

另一种方法是将“load”事件侦听器附加到单个 heavyImage。每次图像加载完成后,就去获取下一张图像。这种方法的缺点是,您的图像将一次加载一个(对于导航图像不利,但对于图像库来说却很好),而第一种技术将并行获取图像(通常一次四个) 。

@Richard's answer (sprites) is probably the best one for what you are after, but the reason your code is not working is that in most browsers, only the last heavyImage.src="" is given enough time to actually register with the browser as an actual request. You're creating only one Image object setting and resetting the .src attribute faster than the browser can request the files (I think modern JavaScript engines take the added step of removing all the intermediate .src statements specifically because of this).

There are a couple of ways to fix this. The easiest is to create multiple Image objects, one for each image. And the easiest way to do that is through something like this:

<script type="text/javascript">
function preloader() 
{
    function doPreload(strImagePath) 
    {
        var heavyImage = new Image();
        heavyImage.src = strImagePath;
    }

    var arrImages = ["images/soluinfo1.jpg","images/soluinfo2.jpg","images/soluinfo3.jpg","images/soluinfo4.jpg","images/soluinfo5.jpg","images/soluinfo6.jpg","images/soluinfo7.jpg"];
    for (var i = 0; i < arrImages.length; i++) {
        doPreload(arrImages[i]);
    }
}
</script>

By putting the heavyImage variable inside its own function (remember to use the var keyword), you're ensuring that the Image object exists inside its own dedicated scope.

Another way to do this is to attach a "load" event listener to a single heavyImage. Every time the image finishes loading, go fetch the next image. The disadvantage to this method is that your images will be loaded one at a time (bad for navigation images, but great for, say, and image gallery), whereas the first technique will fetch the images in parallel (typicallly four at a time).

看透却不说透 2024-09-17 14:37:18

您可能会发现更改方法并使用 CSS sprites (以及另一篇文章)。然后,您将只引用一张图像,并使用 CSS 来设置该图像的哪一部分在哪种情况下显示。

这假设您正在使用的图像在您的控制之下,并且您可以使用图像编辑器将它们组合成一张大图像。

You might find it easier to change your approach and use CSS sprites (and another article). Then you would just have one image referenced, and you use CSS to set which part of that image gets shown in which scenario.

This assumes that the images you're using are under your control and you can use an image editor to combine them into one large image.

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