简体   繁体   English

使用预加载的Gif在悬停时更改img src

[英]Change img src on hover with preloaded gifs

I'm looking for a quick and easy way to preload gifs and then use these gifs on hover when you hover over an image swamping out the src of the image for the gif. 我正在寻找一种快速,简单的方法来预加载gif,然后在将鼠标悬停在图片上方,从而将图片的src淹没在gif上时,在悬停时使用这些gif。

I was originally loading the gifs in the HTML but it makes for a slow load. 我最初是在HTML中加载gif的,但是加载速度很慢。 my original code where the "src" for an image is changed on hover with "data-alt-src" is below: 我的原始代码如下:悬停时使用“ data-alt-src”更改了图像的“ src”:

EDIT 编辑

I've figured out how to preload the gif src into the DOM as an image while hiding it from being displayed. 我已经弄清楚了如何将gif src作为图像预加载到DOM中,同时又不显示它。 I would like for each image to be displayed into its respected HTML figure. 我希望将每个图像显示为其受尊重的HTML图形。 Right now its loading all three gifs into the first tag. 现在,它将所有三个gif加载到第一个标签中。 how can i load it so only one gif loads into each . 我怎样才能加载它,所以每个只有一个gif加载。 I have a working fiddle . 我有一个工作的小提琴 any help is appreciated! 任何帮助表示赞赏!

HTML 的HTML

<figure>
  <img src="http://reneinla.com/tasha-goldthwait/style/images/stills/FORD-SUPER-BOWL.jpg" data-alt-src="http://reneinla.com/tasha-goldthwait/style/images/gifs/giphy.gif"/>
</figure>
<figure>
  <img src="http://reneinla.com/tasha-goldthwait/style/images/stills/OPERATOR.jpg" data-alt-src="http://reneinla.com/tasha-goldthwait/style/images/gifs/giphy.gif"/>
</figure>

Javascript Java脚本

var sourceSwap = function () {
    var $this = $(this);
    var newSource = $this.data('alt-src');
    $this.data('alt-src', $this.attr('src'));
    $this.attr('src', newSource);
}

$(function() {
    $('img[data-alt-src]').each(function() { 
        new Image().src = $(this).data('alt-src');
    }).fadeIn().hover(sourceSwap, sourceSwap);
});

WORKING FIDDLE 工作场所

I was able to properly preload the gifs with the following code below, checking the console for a successful preload of the gifs. 我可以使用以下代码正确预加载gif,并检查控制台是否成功预加载了gif。

Javascript Java脚本

function preloader()
{
 // counter
  var i = 0;

  // create object
  imageObj = new Image();

 // set image list
  images = new Array();
  images[0]="http://reneinla.com/tasha-goldthwait/style/images/gifs/giphy.gif"
  images[1]="http://reneinla.com/tasha-goldthwait/style/images/gifs/LEAGUE-OF-LEGENDS.gif"
  images[2]="http://reneinla.com/tasha-goldthwait/style/images/gifs/OPERATOR.gif"

 // start preloading
  for(i=0; i<=2; i++) 
  {
      imageObj.src=images[i];
      console.log(images[i]);
  }
} 

jQuery(document).ready(function () {
    preloader();
});

FIDDLE HERE (loads the gifs in browser console - just to make sure the code works!) 在此处查找 (将gif加载到浏览器控制台中-只是为了确保代码可以正常工作!)

I'm having trouble connecting the preloaded gifs, using this as a reference to go through the array of preloaded gifs and swapping out the src with the gif src on hover, but I havent had success. 我在连接预加载的gif时遇到问题,无法以此作为参考来遍历预加载的gif数组,并在悬停时将src与gif src换出,但是我没有成功。 Any insight and/or direction would be really appreciated as i'm new to coding and would love to learn how to do this. 当我是编码新手时,任何见识和/或指导都将受到感激,并且很乐意学习如何做到这一点。 thanks! 谢谢!

HTML 的HTML

<figure>
  <img src="http://reneinla.com/tasha-goldthwait/style/images/stills/FORD-SUPER-BOWL.jpg"/>
</figure>
<figure>
  <img src="http://reneinla.com/tasha-goldthwait/style/images/stills/OPERATOR.jpg" />
</figure>

Javascript Java脚本

function preload(arrayOfImages) {
    $(arrayOfImages).each(function(){
            $(this).hover(function(){
            $('<img/>')[0].src = this;
        });
    });
}



preload([
    "http://reneinla.com/tasha-goldthwait/style/images/gifs/giphy.gif",
      "http://reneinla.com/tasha-goldthwait/style/images/gifs/LEAGUE-OF-LEGENDS.gif",
      "http://reneinla.com/tasha-goldthwait/style/images/gifs/OPERATOR.gif"
]);

WORKING FIDDLE 工作场所

EDIT so basically (i write this for my understanding too), im currently loading images and gifs at the same time, fiddle 1. I would like to preload the gifs so that when i hover over one of the images the corresponding preloaded gif gets swapped into the img src. 基本上是这样编辑的 (我也为我的理解而写),即时消息当前同时加载图像和gif,提琴1。我想预加载gif,以便当我将鼠标悬停在其中一张图像上时,相应的预加载的gif被交换了。进入img src。 Is this possible? 这可能吗? I'm able to load the gifs in fiddle 2, but i am having a hard time connecting the available gifs that have been preloaded into the src attribute of the corresponding image. 我可以在小提琴2中加载gif,但是我很难连接已经预加载到相应图像的src属性中的可用gif。 Any ideas? 有任何想法吗?

I have using id of the images as numericals whose corresponding index in the array (arrayOfImages) is the src of your gif; 我使用图像的ID作为数字,其在数组中的对应索引(arrayOfImages)是gif的src;

Hope this helps 希望这可以帮助

 let images = document.getElementsByTagName('img'); for(i=0 ; i<images.length; i++) { images[i].onmouseover = function() {getIdAndLoadGif(this)}; } function getIdAndLoadGif(e) { loadGif(e.getAttribute('id')); } function loadGif(imgId) { document.getElementById(imgId).src= arrayOfImages[imgId]; } let arrayOfImages = ["http://reneinla.com/tasha-goldthwait/style/images/gifs/giphy.gif", "http://s3.amazonaws.com/barkpost-assets/50+GIFs/17.gif", "http://reneinla.com/tasha-goldthwait/style/images/gifs/OPERATOR.gif"]; 
 figure img{ width: 50%; height: auto; } 
 <figure> <img src="http://reneinla.com/tasha-goldthwait/style/images/stills/FORD-SUPER-BOWL.jpg" id="0"/> </figure> <figure> <img src="https://assets.babycenter.com/ims/2016/09/iStock_83513033_4x3.jpg" id="1"/> </figure> <figure> <img src="http://reneinla.com/tasha-goldthwait/style/images/stills/OPERATOR.jpg" id="2"/> </figure> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM