简体   繁体   English

如何在html,css中处理PNG图像加载时间

[英]How to handle PNG image loading time in html, css

When I have an <a> tag set to a specific image background like this: 当我将<a>标记设置为特定的图像背景时,如下所示:

HTML:
<a href="..." title="click here">Click Here</a>

CSS:
a {
  background: transparent url(button.png);
}

and I want the background to change whenever a user hovers over the spot, like this: 并且我希望每当用户在现场盘旋时改变背景,如下所示:

CSS:
a {
  background: transparent url(button_HOVER.png);
}

The hover background image flickers (takes about 1-2 seconds until it fully loads) when a user hovers over the link. 当用户将鼠标悬停在链接上时,悬停背景图像会闪烁 (大约需要1-2秒才能完全加载)。

I could save the file as GIF and minify its size and loading time, but that would harm my specific image tremendously (it's big and highly graphical). 我可以将文件保存为GIF并缩小其大小和加载时间,但这would harm my specific image tremendously (它很大且图形化程度很高)。

That's why I was looking for a better solution, such as perhaps counting on the browsers ability to cache images. 这就是我寻找更好的解决方案的原因,例如可能依靠浏览器缓存图像的能力。 Hence would I apply a style to a button like this: 因此,我会将样式应用于这样的按钮:

CSS:
a {
  background: transparent url(button_HOVER.png);
  background: transparent url(button.png);
}

So that the image button_HOVER is first cached. 这样首先缓存图像button_HOVER。 It has seemingly affected the "flickering", but not completely. 它似乎影响了“闪烁”,但并非完全。 I thought of maybe creating a hidden tag with the HOVER image, so that maybe the result would be different. 我想可能用HOVER图像创建一个隐藏标签,这样结果可能会有所不同。

Do you think there's a better way to solve it? 你认为有更好的解决方法吗? (I emphasize I want to keep the file as PNG, it weighs 6-7k). (我强调我想把文件保存为PNG,重量为6-7k)。 Is my method efficient? 我的方法有效吗?

Your solution would be to put both images (hover and active) in the same image file. 您的解决方案是将两个图像(悬停和活动)放在同一个图像文件中。 Positioned on top of each other. 位于彼此之上。 Also known as Image Sprites. 也称为Image Sprites。 The browser will load the entire image file. 浏览器将加载整个图像文件。 On hover, you just change the background position. 悬停时,您只需更改背景位置即可。

Assuming the active image is at the top, and the hover image is positioned directly below that..your css code would be something like: 假设活动图像位于顶部,并且悬停图像位于该图像的正下方..您的css代码将类似于:

a.link {
  width:70px;
  height:24px;
  background: url(image.png) top no-repeat;
}
a.link:hover {
  background-position: bottom;
}

Notice background-position. 注意背景位置。 Here I use top and bottom. 在这里我使用顶部和底部。 You can specific exactly in pixels too. 您也可以精确地以像素为单位。 The entire image in this example would have a width of 70pixels and height of 48pixels. 此示例中的整个图像的宽度为70像素,高度为48像素。 Some sites put all their small icons into one image. 有些网站将所有小图标放入一张图片中。 Loads altogether, save on requests too. 完全加载,也可以节省请求。 :) :)

No need for preload scripts in this case. 在这种情况下不需要预加载脚本。

The basic options available are to use an html element that's hidden from the viewer, or use javascript. 可用的基本选项是使用隐藏在查看器中的html元素,或使用javascript。

The html approach is probably the simplest, though: 尽管如此,html方法可能是最简单的方法:

<div id="preloadedImageContainer">
<img src="img/to/preload_1.png" />
<img src="img/to/preload_2.png" />
</div>

with the css:
#preloadedImageContainer {position: absolute; top: -1000px; left: -1000px; }

or, with javascript: 或者,使用javascript:

(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

the jQuery approach was lifted in its entirety from this page: 从这个页面完全取消了jQuery方法: http://engineeredweb.com/blog/09/12/preloading-images-jquery-and-javascript . http://engineeredweb.com/blog/09/12/preloading-images-jquery-and-javascript

Though the best approach would probably be, as Lyon suggests , css image sprites. 虽然最好的方法可能是,如里昂建议的那样 ,css图像精灵。

You should search about this topic "preload images" You will find ways to preload images using css and javascript. 你应该搜索关于这个主题的“预加载图像”你会找到使用css和javascript预加载图像的方法。
I believe that if you put a hidden image with source equal the src of png images you will use in the css files, this will make the images loaded when the page loads, and CSS work will be just switch preloaded images. 我相信如果你把一个隐藏的图像与源相等的png图像的src你将在css文件中使用,这将使页面加载时加载图像,而CSS工作将只是切换预加载的图像。

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

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