Javascript 预加载图像以更改 css 背景图像
我正在开发一个网站,需要在链接悬停时更改 div 的背景图像。
它的工作方式是:
<a href="index.php" title="Home ">
<li id="current">
Home<br \>
<span class="nav_desc">Text text</span>
</li>
</a>
<a href="about.php" title="About" id="about-link"
onmouseover="hover('about');"
onmouseout="hoverClear();">
<li id="about">
About<br \>
<span class="nav_desc">About me</span>
</li>
</a>
<a href="more.php" title="More"
onmouseover="hover('portfolio');"
onmouseout="hoverClear();">
<li id="more">
More<br \>
<span class="nav_desc">More More More
</span>
</li>
</a>
js:
function hoverClear(){
$('.navReflect').css("background-image", "url(images/"+page+"/reflect.png)");
}
function hover(hover){
$('.navReflect').css("background-image", "url(images/"+page+"/reflect-"+hover+".png)");
}
因此,当链接悬停时,它会执行更改 div 背景图像的功能。但问题是,当页面首次加载并且链接第一次悬停时,图像加载速度很慢。
但一旦他们加载完毕,它就可以无缝运行。我预计这是一个需要加载的问题。那么有没有一种方法可以让我预先加载图像并仍然使用相同的方法进行悬停。
I am developing a website which requires the background image of a div to change on hover of a link.
The way it works is by:
<a href="index.php" title="Home ">
<li id="current">
Home<br \>
<span class="nav_desc">Text text</span>
</li>
</a>
<a href="about.php" title="About" id="about-link"
onmouseover="hover('about');"
onmouseout="hoverClear();">
<li id="about">
About<br \>
<span class="nav_desc">About me</span>
</li>
</a>
<a href="more.php" title="More"
onmouseover="hover('portfolio');"
onmouseout="hoverClear();">
<li id="more">
More<br \>
<span class="nav_desc">More More More
</span>
</li>
</a>
js:
function hoverClear(){
$('.navReflect').css("background-image", "url(images/"+page+"/reflect.png)");
}
function hover(hover){
$('.navReflect').css("background-image", "url(images/"+page+"/reflect-"+hover+".png)");
}
So, when a link is hovered it does a function to change div background image. But the issue is when the page is first loaded and the links hovered for the first time there is slow loading of the image.
But once they have loaded it works seamless. I expect this is an issue with it needed to be loaded. So is there a way I can preload the images before hand and still use the same method for the hover.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以制作一张精灵图像,即将两个图像合二为一。然后您只需更改背景位置即可显示图像的其他部分。例如,如果元素高 20 像素,则将背景位置移动 20 像素:
由于它只是单个图像,因此从一开始就加载替代外观。这也将减少对服务器的请求数量。
您可以像这样将更多图像放在一起。您可以在我的网站的右上角看到一个示例,其中单个图像用于两个不同的标志在两个不同的州。
You can make a sprite image, i.e. put the two images together into one. Then you just change the background position to show the other part of the image. If the element is for example 20 pixels high, you move the background position by 20 pixels:
As it's only a single image, the alternate look is loaded from the start. This will also reduce the number of requests to the server.
You can ever put more images together like this. You can see an example at the top right corner of my website, where a single image is used for two different flags each in two different states.
预加载图像非常简单,例如:
这可能位于某处的 window.load 或 dom:ready 事件中
pretty simple to preload images, something like:
This could be in a window.load or dom:ready event somewhere
你可以使用这个解决方案。
http://www.gayadesign.com/diy/queryloader-预加载您的网站风格/
you can use this solution.
http://www.gayadesign.com/diy/queryloader-preload-your-website-in-style/
将图像堆叠为一张图像,然后添加一个类,将图像的背景属性移动到适当的偏移量。
这不仅可以将多个图像转换为一个 HTTP 请求,还意味着您无需编写代码来预加载图像的悬停状态。
这称为精灵表。
Stack your images as one image, and then add a class that shifts the image's background property to the appropriate offset.
Not only does this turn multiple images into one HTTP request, it also means you don't have to bother writing code to preload the hover state of the images.
This is called a sprite sheet.