鱼跃于渊

First know how, Second know why !
posts - 0, comments - 1, trackbacks - 0, articles - 49

用ajax做 很炫的预览链接小窗口

Posted on 2008-09-24 16:04 鱼跃于渊 阅读(341) 评论(0)  编辑  收藏 所属分类: 平时小练习

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>

<style type="text/css">
 #preWin {
  background-color: #ff9 ;
  width : 400px ;
  height : 400px ;
  font-size: 9pt ;
  padding: 5px ;
  position : absolute ;
  visibility : hidden ;
  top : 10px ;
  left : 10px ;
  border : 1px #cc0 solid ;
  clip : auto ;
  overflow : hidden ;
 }
 #preWin h1, #preWin h2{
  font-size : 1.0em ;
 }
</style>

<script type="text/javascript">
 window.onload = initAll ;
 var xhr = false ;
 var xPos ;
 var yPos ;
 
 function initAll(){
  document.getElementById("test01").onmouseover = test ;
 }
 
 function test(evt){
  makePrev(evt) ;
  return false ;
 }
 function makePrev(evt){
  
  var url = evt ? evt.target : window.event.srcElement ;
  var evt = evt ? evt : window.event ;
  xPos = evt.clientX ;
  yPos = evt.clientY ;
  
  if(window.XMLHttpRequest){
  xhr = new XMLHttpRequest() ;
  }else{
   if(window.ActiveXObject){
    try{
     xhr = new ActiveXObject("Microsoft.XMLHttpRequest") ;
    }catch(e){}
   }
  }
  
  if(xhr){
   xhr.onreadystatechange = showContents ;
   xhr.open("GET", url, true) ;
   xhr.send(null) ;
  }else{
   document.getElementById("preWin").innerHTML = "sorry ! but your pc coundn't create a xhr object !" ;
  }
 }
 
 function showContents(){
  if(xhr.readyState == 4){
   if(xhr.status == 200){
    var msg = xhr.responseText ;
   }else{
    var msg = "error status = " + xhr.status ;
   }
   var preWin = document.getElementById("preWin") ;
   preWin.style.top=parseInt(yPos) + 2 + "px" ;
   preWin.style.left=parseInt(xPos) + 2 + "px" ;
   preWin.style.visibility = "visible" ;
   preWin.innerHTML = msg ;
   preWin.onmouseout = hidePreview ;
  }
 }
 
 function hidePreview(){
  document.getElementById("preWin").style.visibility = "hidden" ;
 }
 
 
</script>

</head>
<body>
<center>
 <a id="test01" href="xxx.html">ajax preWin</a>
 <div id="preWin"></div>
</center>
</body>
</html>

xxx.html 文件为 :

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
 <h1>preview</h1><h2>A Gentle Introduction to JavaScript</h2>
 fjdsla jdfljsldfjlas djflsajd fljsdlf jlsdjf lsajdlfj sladjf asdf<br>
 fdlsajfl jasldfjlasjdflasjdlfjasldjflas jldlsa jlsdjflsdfkl<br>
 asdjf lksjdlfjasodfuwjeugsj;gjslfdgjsfjdgoufgjs;ldsf<br>
 sjdlfugfgsaodjfoasdjfoasjdofjasidf<p>
 sadlrefjldasdjflasjdlfjasdf
 asdggfdfgdfgfdgdfgdfgdfg
 ferfgfg  
 sadgd
 ferdfgfg    gfedgdfgdfgfdg
 sfdgfggeregrerg</p>
</body>
</html>


只有注册用户登录后才能发表评论。


网站导航: