动态切换图片

// JavaScript Document
pop = {
	triggerClass:'picturepop',
	openPopupLinkClass:'popuplink',
	popupClass:'popup',
	displayPrefix:'Hide',
	popContainer:null,
	init:function(){
		if(!document.getElementById || !document.createTextNode){
			return;
		}
		var allLinks = document.getElementsByTagName('a');
		for(var i=0;i<allLinks.length;i++){
			if(!DOMhelp.cssjs('check',allLinks[i],pop.triggerClass)){
				continue;
			}
			DOMhelp.addEvent(allLinks[i],'click',pop.openPopup,false);
			allLinks[i].onclick = DOMhelp.safariClickFix;
			allLinks[i].preset = allLinks[i].innerHTML;
		}
	},
	openPopup:function(e){
		var t = DOMhelp.getTarget(e);
		if(t.nodeName.toLowerCase() != 'a'){
			t = t.parentNode;
		}
		if(!pop.popContainer){
			t.innerHTML = pop.displayPrefix + t.preset;
			pop.popContainer = document.createElement('div');
			DOMhelp.cssjs('add',pop.popContainer,pop.popupClass);
			DOMhelp.cssjs('add',t,pop.openPopupLinkClass);
			var newimg = document.createElement('img');
			newimg.setAttribute('src',t.getAttribute('href'));
			pop.popContainer.appendChild(newimg);
			document.body.appendChild(pop.popContainer);
			pop.positionPopup(t);
		}else{
			pop.killPopup();
			t.innerHTML = t.preset;
			DOMhelp.cssjs('remove',t,pop.openPopupLinkClass);
		}
		DOMhelp.cancelClick(e);
	},
	positionPopup:function(o){
		var x=0;
		var y=0;
		var h=o.offsetHeight;
		while(o!=null){
			x += o.offsetLeft;
			y += o.offsetTop;
			o = o.offsetParent;
		}
		pop.popContainer.style.left = x + 'px';
		pop.popContainer.style.top = y + h + 'px';
	},
	killPopup:function(e){
		pop.popContainer.parentNode.removeChild(pop.popContainer);
		pop.popContainer=null;
		DOMhelp.cancelClick(e);
	}
}
DOMhelp.addEvent(window,'load',pop.init,false);

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html dir="ltr" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
	<title>Example: Replacing image popups with layer ads</title>
	<style type="text/css">
 		@import 'style/picturepopu.css';
	</style>
	<!-- <script type="text/javascript" src="js/DOMhelp.js"></script>
	<script type="text/javascript" src="js/picturepopu.js"></script> -->
	<script type="text/javascript" src="js/DOMhelp.js"></script>
	<script type="text/javascript" src="Chapter6/picturePopup.js"></script>
</head>
<body>
	  <p>Sometimes it is better not to wake a
	  <a class="picturepop" href="Chapter6/pictures/thumbs/dog7.jpg">Sleeping Dog</a>.</p>
</body>
</html>

/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
DOMhelp={
	//动态添加和移除一个元素的类
	cssjs:function(a,o,c1,c2){
		switch(a){
			case 'swap':
				if(!DOMhelp.cssjs('check',o,c1)){
					o.className.replace(c2,c1);
				}else{
					o.className.replace(c1,c2);
				}
			break;
			case 'add':
				if(!DOMhelp.cssjs('check',o,c1)){
					o.className += o.className ? ' '+c1 : c1;
				}
			break;
			case 'remove':
				var rep = o.className.match(' '+c1) ? ' '+c1 : c1;
				o.className = o.className.replace(rep,'');
			break;
			case 'check':
				var found = false;
				var temparray = o.className.split(' ');
				for(var i=0;i<temparray.length;i++){
					if(temparray[i]==c1){
						found = true;
					}
				}
				return found;
			break;
		}
	},
	safariClickFix:function(){
      		return false;
    },
    //Find the last sibling of the current node
    lastSibling:function(node){
        var tempObj = node.parentNode.lastChild;
        while(tempObj.nodeType != 1 && tempObj.previousSibling != null){
            tempObj = tempObj.previousSibling;
        }
        return (tempObj.nodeType==1) ? tempObj : false;
    },
    //Find the first sibling of the current node
    firstSibling:function(node){
        var tempObj = node.parentNode.firstChild;
        while(tempObj.nodeType != 1 && tempObj.nextSibling != null){
            tempObj = tempObj.nextSibling;
        }
        return (tempObj.nodeType==1) ? tempObj : false;
    },
    //Retrieve the content fo the first text node sibling of the current node
    getText:function(node){
        if(!node.hasChildNodes()){
            return false;
        }
        var reg=/^\s+$/;
        var tempObj = node.firstChild;
        while(tempObj.nodeType != 3 && tempObj.nextSibling != null || reg.test(tempObj.nodeValue)){
            tempObj = tempObj.nextSibling;
        }
        return (tempObj.nodeType == 3) ? tempObj.nodeValue:false;
    },
    //Set the content of the first text node sibling the current node
    setText:function(node,txt){
        if(!node.hasChildNodes()){
            return false;
        }
        var reg = /^s+$/;
        var tempObj = node.firstChild;
        while(tempObj.nodeType != 3 && tempObj.nextSibling != null || reg.test(tempObj.nodeValue)){
            tempObj = tempObj.nextSibling;
        }
        if(tempObj.nodeType == 3){
            tempObj.nodeValue = txt;
        }else{
            return false;
        }
    },
    
    //Find the text pr previous sibling that is an element
    //and not a text node or line break
    //
    //1表示下一个兄弟节点,-1表示上一个兄弟节点
    closestSibling:function(node,direction){
        var tempObj;
        if(direction == -1 && node.previousSibling != null){
            tempObj = node.previousSibling;
            while(tempObj.nodeType != 1 && tempObj.previousSibling != null){
                tempObj = tempObj.previousSibling;
            }
        }else if(direction==1 && node.nextSibling != null){
            tempObj=tempObj.nextSibling;
            while(tempObj.nodeType != 1 && tempObj.textSibling != null){
                tempObj = tempObj.nextSibling;
            }
        }
        return tempObj.nodeType==1 ? tempObj : false;
    },
    //Create a new link containing the given text
    createLink:function(to,txt){
        var tempObj = document.createElement('a');
        tempObj.appendChild(document.createTextNode(txt));
        tempObj.setAttribute('href',to);
        return tempObj;
    },
    //Create a new element containing the given text
    createTextElem:function(elm,txt){
       var tempObj = document.createElement(elm);
       tempObj.appendChild(document.createTextNode(txt));
       return tempObj;
    },
    //Simulate a debugging console to avoid the nees for alerts
    initDebug:function(){
        if(DOMhelp.debug){
            DOMhelp.stopDebug();
        }
        DOMhelp.debug = document.createElement('div');
        DOMhelp.debug.setAttribute('id',DOMhelp.debugWindowId);
        document.body.insertBefore(DOMhelp.debug,document.body.firstChild);
    },
    setDebug:function(bug){
        if(!DOMhelp.debug){
            DOMhelp.initDebug();
        }
        DOMhelp.debug.innerHTML += bug+'\n';
    },
    stopDebug:function(){
        if(DOMhelp.debug){
            DOMhelp.debug.parentNode.removeChild(DOMhelp.debug);
            DOMhelp.debug = null;
        }
    },
    //Cross-browser event handing for IE5+ ,NS6+ and Mozilla/Gecko
    //By Scott Andrew
    addEvent:function(elm,evType,fn,useCapture){
    	if(elm.addEventListener){
    		elm.addEventListener(evType,fn,useCapture);
    		return true;
    	}else if(elm.attachEvent){
    		var r = elm.attatchEvent('on' + evType,fn);
    		return r;
    	}else{
    		elm['on' + evType] = fn;
    	}
    },
    //为了适应在ie下target被srcElement取代
    //button返回值的不同
    getTarget:function(e){
    	var target;
    	if(window.event){
    		target = window.event.srcElement;
    	}else if(e){
    		target = e.target;
    	}else{
    		target = null;
    	}
    	if(target.nodeName.toLowerCase() != 'a'){
    		target = target.parentNode;
    	}
    	return target;
    },
    //由于Safari支持stopPropagation方法,但是使用它就什么也做不了,不会阻止事件继续冒泡
    stopDefault:function(e){
    	if(window.event && window.event.returnValue){
    		window.event.cancelBubble = true;
    	}
    	if(e && e.preventDefault){
    		e.preventDefault();
    	}
    },
    //一般情况下都需要阻止stopPropagation()和preventDefault()
    cancelClick:function(e){
    	if(window.event && window.event.cancelBubble && window.event.returnValue){
    		window.event.cancelBubble = true;
    		window.event.returnValue = false;
    		return ;
    	}
    	if(e && e.stopPropagation && e.preventDefault){
    		e.stopPropagation();
    		e.preventDefault();
    	}
    	
    }
}

*{
		margin:0;
		padding:0;
	}
	body{
		font-size:.8em;
		font-family:arial,sans-serif;
		color:#333;
		background:#fff;
		padding:.5em;
	}
	h1{
		font-size:1.2em;
	}
	p{
		margin-top:.5em;
	}
	.popup{
		padding:.5em;
		background:url(indicator_medium.gif) center center no-repeat #eee;
		border:1px solid #999;
		position:absolute;
		top:0;
		left:0;
	}
	a.popuplink{
		background:#eee;
		padding:0 .5em;	
		border:1px solid #999;
		text-decoration:none;
		font-weight:bold;
		color:#666;
		margin-bottom:-1px;
	}

图片自己可以替换的,在css中需要替换图片,在html代码汇总需要替换图片

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://my.oschina.net/zhangdapeng89/blog/38462

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值