位置:海鸟网 > IT > JavaScript >

使用div仿javascript模态窗口

虽然和本blog的内容无关,但是还是写出来吧,希望对大家有帮助
前几天在博客园看到有人说模态窗口在ie7里面显示出现地址栏,其实这本是一件好事,而且ie的模态窗口是ie的函数,ff、opera等都不支持,我评论的原话

只是ie7增强的安全特性罢了,这个世界还真奇怪,有人说ie的安全性不强,天天骂微软,还支持ff说什么用了ff就不会被强奸,微软增强了安全性,又有人说这样增加的安全性不好,真是难伺候 
你要用模态窗口ff是根本就打不开,因为那个是ie特有的函数,ff是没有的,另外楼上说的很对加到trusted就行了,另外再说句话你别生气,就是你水平不行,要想得到最大的兼容性就用div,而且div是最漂亮的,就是写得麻烦点 

既然说了别人水平不行,那就自己做一个吧,也证明我这段话不是白说,这几天正在做自己用的cms,正好用这个替代alert和模态窗口,这个div是可以移动、嵌套的

做下说明:
代码参考新浪的新闻评论,新浪混淆了代码,其实破解很简单(应该说根本不算破解,不知道怎么说了,就叫破解吧)

写得很简单,没做过多的处理,这是有这么个意思,如果要用到的话还是需要进行修改的

测试ie7通过ff有问题(js报错),新浪的代码ff js没报错,但是div的移动有问题,我没有系统的学过js所以新浪的代码看的不是很懂,理论上ff也是可以做到的,有兴趣完善的和我联系吧,我就想作出一个简单的类,做到ie7和ff能够兼容,这东西至少在微软的ajax普及之前还是能起到不少的作用的

源文件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head>
<style type="text/css">
#id1{width:100%;height:100%;background-color:#000;position:absolute;top:0;left:0;z-index:49;display:none;}
#id2{top:200px;position:absolute;z-index:50;display:none;border:3px solid #E5E5E5; left:100px;background-color:white;}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>asfsdfasdfasdf</title>
<script language="javascript" type="text/javascript">
function show(){
id1.style.height=window.screen.height+"px";
id1.style.width=window.screen.width+"px";
id1.style.display='block';
id2.style.display='block'
}
function hide(){
id1.style.display='none'
id2.style.display='none'
}
self.onError=null;                              
currentX = currentY = 0;                               
whichIt = null;                                         
lastScrollX = 0; lastScrollY = 0;                              
NS = (document.layers) ? 1 : 0;                             
IE = (document.all) ? 1: 0;                              
<!-- STALKER CODE -->                              
function heartBeat() {                              
if(IE) { diffY = document.body.scrollTop; diffX = document.body.scrollLeft; }                             if(NS) { diffY = self.pageYOffset; diffX = self.pageXOffset; }           
if(diffY != lastScrollY) {            
percent = .1 * (diffY - lastScrollY);    
if(percent > 0) percent = Math.ceil(percent); 
else percent = Math.floor(percent);  
if(IE) document.all.id2.style.pixelTop += percent;  
if(NS) document.id2.top += percent;                 
lastScrollY = lastScrollY + percent;      
}                       
if(diffX != lastScrollX) {       
percent = .1 * (diffX - lastScrollX);     
if(percent > 0) percent = Math.ceil(percent);       
else percent = Math.floor(percent);                   
if(IE) document.all.id2.style.pixelLeft += percent;        
if(NS) document.id2.left += percent;       
lastScrollX = lastScrollX + percent;                     
}                           
}                         
<!-- /STALKER CODE -->              
<!-- DRAG DROP CODE -->              
function checkFocus(x,y) {         
stalkerx = document.id2.pageX;     
stalkery = document.id2.pageY;                        
stalkerwidth = document.id2.clip.width;              
stalkerheight = document.id2.clip.height;                         
if( (x > stalkerx && x < (stalkerx+stalkerwidth)) && (y > stalkery && y < (stalkery+stalkerheight))) return true;                      
else return false;        
}                     
function grabIt(e) {            
if(IE) {                            
whichIt = event.srcElement; 
while (whichIt.id.indexOf("id2") == -1) {    
whichIt = whichIt.parentElement;                           
if (whichIt == null) { return true; }         
 }                            
whichIt.style.pixelLeft = whichIt.offsetLeft;                              
whichIt.style.pixelTop = whichIt.offsetTop;                          
currentX = (event.clientX + document.body.scrollLeft);                       
currentY = (event.clientY + document.body.scrollTop);                        
} else {                             
window.captureEvents(Event.MOUSEMOVE);    
if(checkFocus (e.pageX,e.pageY)) {                            
whichIt = document.id2;                              
stalkerTouchedX = e.pageX-document.id2.pageX;      
StalkerTouchedY = e.pageY-document.id2.pageY;          
}                              
}                            
return true;                        
}                            
function moveIt(e) {               
if (whichIt == null) { return false; }       
if(IE) {                             
newX = (event.clientX + document.body.scrollLeft);                           
newY = (event.clientY + document.body.scrollTop);                
distanceX = (newX - currentX);    distanceY = (newY - currentY);   
currentX = newX;    currentY = newY;                       
whichIt.style.pixelLeft += distanceX;                          
whichIt.style.pixelTop += distanceY;                      
if(whichIt.style.pixelTop < document.body.scrollTop) whichIt.style.pixelTop = document.body.scrollTop;    if(whichIt.style.pixelLeft < document.body.scrollLeft) whichIt.style.pixelLeft = document.body.scrollLeft; if(whichIt.style.pixelLeft > document.body.offsetWidth - document.body.scrollLeft - whichIt.style.pixelWidth - 20) whichIt.style.pixelLeft = document.body.offsetWidth - whichIt.style.pixelWidth - 20;
if(whichIt.style.pixelTop > document.body.offsetHeight + document.body.scrollTop - whichIt.style.pixelHeight - 5) whichIt.style.pixelTop = document.body.offsetHeight + document.body.scrollTop - whichIt.style.pixelHeight - 5;  
event.returnValue = false;  
} else {   
whichIt.moveTo(e.pageX-StalkerTouchedX,e.pageY-StalkerTouchedY); 
if(whichIt.left < 0+self.pageXOffset) whichIt.left = 0+self.pageXOffset; 
if(whichIt.top < 0+self.pageYOffset) whichIt.top = 0+self.pageYOffset;  
if( (whichIt.left + whichIt.clip.width) >= (window.innerWidth+self.pageXOffset-17)) whichIt.left = ((window.innerWidth+self.pageXOffset)-whichIt.clip.width)-17; 
if( (whichIt.top + whichIt.clip.height) >= (window.innerHeight+self.pageYOffset-17)) whichIt.top = ((window.innerHeight+self.pageYOffset)-whichIt.clip.height)-17; 
return false;} 
return false;  
}   
function dropIt() {     
whichIt = null;     
if(NS) window.releaseEvents (Event.MOUSEMOVE);  
return true;             
 }                     
<!-- DRAG DROP CODE -->  
if(NS) {    
window.captureEvents(Event.MOUSEUP|Event.MOUSEDOWN);
window.onmousedown = grabIt;
window.onmousemove = moveIt; 
window.onmouseup = dropIt; 
}              
if(IE) {                     
document.onmousedown = grabIt;    
document.onmousemove = moveIt;   
document.onmouseup = dropIt;          
 }                         
if(NS || IE) action = window.setInterval("heartBeat()",1)</script> 
</head>
<body>
<p> </p>
<div id="id2">
这里可以放内容,或者添加div用innerhtml进行添加内容就可以了
<br/><input name="Button1" type="button" value="button" onclick="hide()"/>
</div>
<div id="id1">123</div>
<form method="post">
<input name="Button2" type="button" value="button" onclick="show()"/>
</form>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
</body>
</html>