不引人注目的鼠标图像交换与预加载

Posted

tags:

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

Needs 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"); //all links inside the element with this id will receive mouseover behavior
  31.  
  32. /*
  33.   takes two image links "about.gif" and "about-over.gif" and swaps them on mouseover and mouseout
  34.   Any image link in html page that you want to recieve mouseover behavior make sure image exists that
  35.   has the name of the original image with "-over" appended to the end of the filename.
  36.   */

以上是关于不引人注目的鼠标图像交换与预加载的主要内容,如果未能解决你的问题,请参考以下文章

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

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

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

在fancybox画廊内的鼠标悬停上交换图像?

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

关于js----------------分享前端开发常用代码片段