带预加载程序的鼠标悬停图像交换

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了带预加载程序的鼠标悬停图像交换相关的知识,希望对你有一定的参考价值。

Needs to include object detection to be completely unobtrusive
  1. function imageSwap(id) {
  2. var links = document.getElementById(id).getElementsByTagName("a");
  3. var imgLoad = []
  4.  
  5. for(var i = 0; i < links.length; i++) {
  6. attachBehavior(links[i], i);
  7. }
  8.  
  9. function attachBehavior(obj, iter) {
  10.  
  11. var img = obj.getElementsByTagName('img')[0];
  12. var imgSrc = img.getAttribute("src");
  13. var ext = imgSrc.match(/.S{3}$/);
  14. var overSrc = imgSrc.replace(ext, "-over" + ext);
  15.  
  16. // preLoad over states
  17. imgLoad[iter] = new Image();
  18. imgLoad[iter].src = overSrc
  19.  
  20. // use event listeners if appropriate
  21. obj.onmouseover = function(){
  22. img.setAttribute("src", overSrc);
  23. }
  24. obj.onmouseout = function(){
  25. img.setAttribute("src", imgSrc);
  26. }
  27. }
  28. }
  29.  
  30. imageSwap("footer-links");
  31.  
  32. // takes an image in html page of the form about_us_link.gif and swaps it with an image
  33. // about_us_link.gif and replaces it with image named about_us_link-over.gif
  34. // Create a new image for mouseover named the same as the original image
  35. // with "-over" appended to the end but before the extension for example

以上是关于带预加载程序的鼠标悬停图像交换的主要内容,如果未能解决你的问题,请参考以下文章

收藏|分享前端开发常用代码片段

JavaScript 使用预加载器进行不显眼的鼠标悬停图像交换

JavaScript 鼠标悬停图像交换与预加载器不显眼

jquery 将鼠标悬停在图像上以交换上面 div 中的另一个图像

你如何在鼠标悬停(jQuery)上交换 DIV?

分享前端开发常用代码片段