带预加载程序的鼠标悬停图像交换
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了带预加载程序的鼠标悬停图像交换相关的知识,希望对你有一定的参考价值。
Needs to include object detection to be completely unobtrusive
function imageSwap(id) { var links = document.getElementById(id).getElementsByTagName("a"); var imgLoad = [] for(var i = 0; i < links.length; i++) { attachBehavior(links[i], i); } function attachBehavior(obj, iter) { var img = obj.getElementsByTagName('img')[0]; var imgSrc = img.getAttribute("src"); var ext = imgSrc.match(/.S{3}$/); var overSrc = imgSrc.replace(ext, "-over" + ext); // preLoad over states imgLoad[iter] = new Image(); imgLoad[iter].src = overSrc // use event listeners if appropriate obj.onmouseover = function(){ img.setAttribute("src", overSrc); } obj.onmouseout = function(){ img.setAttribute("src", imgSrc); } } } imageSwap("footer-links"); // takes an image in html page of the form about_us_link.gif and swaps it with an image // about_us_link.gif and replaces it with image named about_us_link-over.gif // Create a new image for mouseover named the same as the original image // with "-over" appended to the end but before the extension for example
以上是关于带预加载程序的鼠标悬停图像交换的主要内容,如果未能解决你的问题,请参考以下文章
JavaScript 使用预加载器进行不显眼的鼠标悬停图像交换