如何延迟 html 文本的显示,直到加载背景图像精灵?
下面是我想要使用 jQuery 控制的一些示例代码(黑色页面 bg 上的白色按钮 bg):
<ul class="buttons">
<li class="button-displays"><a href="/products/">Products and Services for the company</a></li>
<li class="button-packaging"><a href="/packaging/">Successful packaging services for the company</a></li>
<li class="button-tools"><a href="/tools/">Data, mail and print tools for your company</li>
</ul>
在 CSS 文件中,我有以下内容:
.buttons li { background-image: url(images/sprite.png); height:126px; width:293px; float:left; margin:0 0 0 9px; }
.button-displays { background-position: 0 125px; }
.button-packaging { background-position: 0 250px; }
.button-tools { background-position: 0 375px; }
我将这些列表项的样式设置为看起来像可点击的按钮,并使用背景精灵帮助填充按钮的背景。
我的客户不喜欢 Firefox 和 Safari,当页面第一次加载时,锚点内的文本首先加载,然后是 li 的背景精灵(大约 150kb b/c 我总共有 6 个按钮)仅在精灵完全下载后加载。下载几秒钟后,背景突然出现,将文本留在锚点中,直到背景弹出。
我尝试使用以下代码,希望它会延迟此标记和 CSS 的加载:
$('.buttons li a').load(function() {
});
确实
$(window).load(function() {
$(.buttons);
});
我 这么做了对 jQuery 的了解不够,不知道这是否会强制代码等待所有元素加载后再出现。我宁愿强制按钮代码中的列表项元素延迟出现在页面上,直到 bg img sprite 完全加载。
感谢您的帮助和建议!
Here's some sample code that I want to control using jQuery (white button bg on black page bg):
<ul class="buttons">
<li class="button-displays"><a href="/products/">Products and Services for the company</a></li>
<li class="button-packaging"><a href="/packaging/">Successful packaging services for the company</a></li>
<li class="button-tools"><a href="/tools/">Data, mail and print tools for your company</li>
</ul>
In the CSS file, I have the following:
.buttons li { background-image: url(images/sprite.png); height:126px; width:293px; float:left; margin:0 0 0 9px; }
.button-displays { background-position: 0 125px; }
.button-packaging { background-position: 0 250px; }
.button-tools { background-position: 0 375px; }
I have styled these list items to look like clickable buttons with the background sprite helping to fill out the background of the buttons.
My client doesn't like that in Firefox and Safari, when the page loads for the first time, the text inside the anchor loads first then the background sprite of the li (which is around 150kb b/c I have a total of 6 buttons) loads only when the sprite is fully downloaded. The background suddenly appears after a couple of seconds of downloading leaving the text in the anchor by itself until the background pops in.
I have tried playing with the following code in hopes that it would delay the loading of this markup and CSS:
$('.buttons li a').load(function() {
});
and
$(window).load(function() {
$(.buttons);
});
I do not understand jQuery enough to know if this forces the code to wait until all elements load before appearing. I'd rather force the list item elements in the buttons code to delay appearing on the page until the bg img sprite is completely loaded.
Thanks for your help and suggestions!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为您不能附加专门用于背景图像的加载事件。
请尝试以下操作:
将
.button li
元素设置为display:none
:然后在 jQuery 中,使用精灵的 URL 创建一个图像,并附加一个
load
处理程序将显示按钮。I don't think you can attach a load event specifically for background images.
Try this instead:
Make the
.button li
elementsdisplay:none
:Then in your jQuery, create an image with the URL of your sprite, and attach a
load
handler to it that will show the buttons.我认为你走在正确的道路上。 JQuery 的加载函数在所选元素完全完成加载后执行 - 这意味着所有内容都已下载。问题在于您的代码,按钮标记将在下载内容之前下载。因此,您可以使用 jQuery 通过加载事件完全显示按钮。因此,将按钮设置为隐藏在 CSS 中(可见性:隐藏)。然后,使用 $(document).load(function(){}) 显示按钮:
I think you're on the right track. JQuery's load function executes after the element selected has completely finished loading - meaning all content is downloaded. The thing is with your code, the button markup will download before the content is downloaded. So, you can use jQuery to reveal the buttons completely with a load event. So, set you're buttons to be hidden in your CSS (visibility:hidden). Then, use you're $(document).load(function(){}) to reveal the buttons:
在其中编写代码
可确保您的 DOM 操作在 DOM 完全加载后开始...
此外,您还可以使用它
来确保预期的元素已准备好进行操作...
更多的是延迟事件处理程序,您可以使用
.setTimeOut()
或在某些情况下.delay()
write you code inside
this makes sure that your DOM manipulation starts after your DOM is fully loded...
plus you can also use
to make sure that the intended element is ready to be manipulated...
more over to delay an event handler your can use
.setTimeOut()
or in some cases.delay()
最初将按钮设置为隐藏,然后在 li 上执行 jquery load ,因为这是背景图像应用了什么:
Set the buttons to be hidden initially, then do the jquery load on the li, since that is what has the background image applied: