【问题标题】:Preload images for links that change the background image [duplicate]为更改背景图像的链接预加载图像[重复]
【发布时间】:2016-11-18 00:36:06
【问题描述】:

我正在尝试重新创建一个行为类似于此页面的页面:

http://www.farmleague.us/team/

当我使用此代码将鼠标悬停在我的链接上时,我成功地使用 jquery 来更改背景图像:

<script>
// Since we're using these multiple times, assign them to variables
var $link = jQuery('#block-yui_3_17_2_2_1468449172411_5050');
var $image = jQuery('#collection-5786be4ed482e9c3a905d937');
// Get the original background image
var originalBackground = $image.css("background-image",       "url(https://static1.squarespace.com/static/57771b9e15d5dbc2f8fc084d/57771c55e3df28c63c9e687f/57771ca7440243196bc6801b/1467423990309/Gator.jpg?format=1500w)");
// jQuery hover supports TWO functions: first is "mouseover", second is "mouseout"
$link.hover(
// On mouseover, replace image
function() { 
    $image.css("background-image",  "url(https://static1.squarespace.com/static/57771b9e15d5dbc2f8fc084d/57771c55e3df2 8c63c9e687f/57771d441b631b48a1362df6/1467424076131/patagoniawater.jpg?format=750w)"); 
},
// On mouseout, put original image back
function() {
    $image.css("background-image",  "url(https://static1.squarespace.com/static/57771b9e15d5dbc2f8fc084d/57771c55e3df28c63c9e687f/57771ca7440243196bc6801b/1467423990309/Gator.jpg?format=1500w)", originalBackground);
}
); </script>   

但是,当我第一次将鼠标悬停在链接上时,会有一瞬间背景变为默认背景颜色,该颜色设置为透明。一旦我将鼠标悬停在链接上一次,背景图像就会来回变化,中间没有背景颜色,所以我猜如果我加载了图像,我将永远看不到背景颜色。

任何帮助将不胜感激!

【问题讨论】:

    标签: jquery image


    【解决方案1】:

    这里有一些快速而肮脏的东西,应该让你开始。

    $(document).ready(function() {
      preloadImages();
    })
    
    function preloadImages() {
      var images = [
        'img/image1.jpg',
        'img/image2.jpg',
        'img/image3.jpg'
      ]
    
      images.each(function(){
        $('<img />').attr('src',this).appendTo('body').hide();      
      });
    }
    

    【讨论】:

      猜你喜欢
      • 2017-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-03
      • 1970-01-01
      • 2012-03-28
      • 2014-02-01
      相关资源
      最近更新 更多