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

Posted

tags:

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

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"); //all links inside the element with this id will receive mouseover behavior

        /* 
        takes two image links "about.gif" and "about-over.gif" and swaps them on mouseover and mouseout
        Any image link in HTML page that you want to recieve mouseover behavior make sure image exists that      
        has the name of the original image with "-over" appended to the end of the filename.
        */

以上是关于JavaScript 使用预加载器进行不显眼的鼠标悬停图像交换的主要内容,如果未能解决你的问题,请参考以下文章

使用JavaScript预加载器

网站的 JavaScript 预加载器,它不工作的 IE,

支持事件的图像预加载器 javascript

JQuery - 预加载图像(使用 JQuery / 本机 JavaScript / CSS)

javascript 图像预加载器

javascript 图像预加载器