在 Rails 中预加载鼠标悬停图像
我目前在 Rails 中实现了一个翻转按钮,如下所示:
<%= image_tag("header/home_but.gif", :mouseover => "header/home_over.gif") %>
如何预加载/缓存鼠标悬停图像 (home_over.gif),以便用户将鼠标移到图像上时不会出现延迟?谢谢。
I currently have a rollover button implemented in rails as follows:
<%= image_tag("header/home_but.gif", :mouseover => "header/home_over.gif") %>
How can I preload/cache the mouseover image (home_over.gif) so there is no delay when the user moves their mouse over the image? Thanx.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您确定不想要这里的 CSS Sprite 吗?基本上,您将图像状态放入一张图像中 (Photoshop),将图像设置为锚元素的背景,然后使用 CSS 调整背景属性以及 :hover 和 :visited 状态的可见区域。通过这种方式只需下载一张图像。
Are you sure you don't want a CSS Sprite here? Basically you put your image states into one image (Photoshop), set the image as the background of an anchor element, then adjust the visible area with CSS for the background property and the :hover and :visited states. Only one image has to download this way.
我的环境使用 jQuery,因此我希望解决方案也使用 jQuery。
我发现另一个问题关于使用 jQuery 预加载图像,它的最佳答案为我预先编写了 jQuery 。我将我的代码调整为 ERB,如下所示:
My environment uses jQuery, so I wanted the solution to use jQuery as well.
I found another question about preloading images with jQuery, and it's top answer had the jQuery prewritten for me. I adapted my code as follows into ERB:
我不是 Rails 程序员,但我的理解是 Rails 默认使用 Prototype。假设您可以包含此 JavaScript:
然后在 onload 代码运行的任何位置添加此代码。也许是这样的:
您必须确保对图像路径执行任何魔术
image_tag()
操作,以确保预加载正确的图像。I'm not a rails programmer, but my understanding is that Rails uses Prototype by default. Assuming that, you could include this JavaScript:
Then add this code wherever your onload code runs. Maybe something like this:
You'll have to assure that whatever magic
image_tag()
does is done to the image paths to assure that the correct image is preloaded.