在 Rails 中预加载鼠标悬停图像

发布于 2024-08-10 06:26:24 字数 210 浏览 4 评论 0原文

我目前在 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 技术交流群。

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

发布评论

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

评论(3

青丝拂面 2024-08-17 06:26:24

您确定不想要这里的 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.

巴黎盛开的樱花 2024-08-17 06:26:24

我的环境使用 jQuery,因此我希望解决方案也使用 jQuery。

我发现另一个问题关于使用 jQuery 预加载图像,它的最佳答案为我预先编写了 jQuery 。我将我的代码调整为 ERB,如下所示:

<% alternate_images = [] %>
<% @resources.each do |resource| %>
  <%= image_tag(resource.primary_image.url, :mouseover => resource.alternate_image.url) %>
  <% alternate_images << resource.alternate_image.url %>
<% end %>
<script type="text/javascript">
$.fn.preload = function() {
    this.each(function(){
        $('<img/>')[0].src = this;
    });
}
$([<% alternate_images.each do |image| %>
     "<%= image %>",
   <% end %>]).preload();
</script>

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:

<% alternate_images = [] %>
<% @resources.each do |resource| %>
  <%= image_tag(resource.primary_image.url, :mouseover => resource.alternate_image.url) %>
  <% alternate_images << resource.alternate_image.url %>
<% end %>
<script type="text/javascript">
$.fn.preload = function() {
    this.each(function(){
        $('<img/>')[0].src = this;
    });
}
$([<% alternate_images.each do |image| %>
     "<%= image %>",
   <% end %>]).preload();
</script>
凡间太子 2024-08-17 06:26:24

我不是 Rails 程序员,但我的理解是 Rails 默认使用 Prototype。假设您可以包含此 JavaScript:

Prototype.preloadImages = function(){
    for(var i=0, images=[]; src=arguments[i]; i++){
        images.push(new Image());
        images.last().src = src;
    }
};

然后在 onload 代码运行的任何位置添加此代码。也许是这样的:

Event.observe(window, 'load', function(){
    Prototype.preloadImages('header/home_over.gif','another/image/to/preload.gif');
});

您必须确保对图像路径执行任何魔术 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:

Prototype.preloadImages = function(){
    for(var i=0, images=[]; src=arguments[i]; i++){
        images.push(new Image());
        images.last().src = src;
    }
};

Then add this code wherever your onload code runs. Maybe something like this:

Event.observe(window, 'load', function(){
    Prototype.preloadImages('header/home_over.gif','another/image/to/preload.gif');
});

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.

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