js+css跟隨滑鼠的提示框(tips)

來源:互聯網
上載者:User

從米克那裡找的寶貝代碼,效果不錯,以前用過純css的tips,不過限制挺大,這個還是蠻不錯的!

效果

代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>滑鼠跟隨提示框</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
body{font:12px/1.8 arial;}
a,a:visited{color:#3366cc;text-decoration:none;}
a:hover{color:#f60;text-decoration:underline;}
.tip{width:200px;border:2px solid #ddd;padding:8px;background:#f1f1f1;color:#666;}
img{border:none;}
</style>
<script type="text/javascript">
  var tip={$:function(ele){
  if(typeof(ele)=="object")
    return ele;
  else if(typeof(ele)=="string"||typeof(ele)=="number")
    return document.getElementById(ele.toString());
    return null;
  },
  mousePos:function(e){
    var x,y;
    var e = e||window.event;
    return{x:e.clientX+document.body.scrollLeft+document.documentElement.scrollLeft,
y:e.clientY+document.body.scrollTop+document.documentElement.scrollTop};
  },
  start:function(obj){
    var self = this;
    var t = self.$("mjs:tip");
    obj.onmousemove=function(e){
      var mouse = self.mousePos(e);  
      t.style.left = mouse.x + 10 + 'px';
      t.style.top = mouse.y + 10 + 'px';
      t.innerHTML = obj.getAttribute("tips");
      t.style.display = '';
    };
    obj.onmouseout=function(){
      t.style.display = 'none';
    };
  }
  }
</script>
</head>
<body>
<ol>
<li>
<a href="#" target="_blank"><img src="http://pic1.taogogo.info/image/16002/" onmouseover="tip.start(this)" tips="項目中用到的一個簡單的JS圖片展示效果,支援大圖小圖原始圖展示" /></a>
</li>
<li>
<a href="#" target="_blank" onmouseover="tip.start(this)" tips="11月16日,我有幸參加了11月份的UCD杭州書友會,寫了點感受與大家分享……">關於使用者研究的一點思考</a>
</li>
<li>
<a href="#" target="_blank" onmouseover="tip.start(this)" tips="早上擠城鐵,擠得要命,來了個猛男,大吼一聲“我是display:block”的,這一行老子獨佔了……">WEB重構拾趣< /a>
</li>
</ol>
<div id="mjs:tip" class="tip" style="position:absolute;left:0;top:0;display:none;"></div>
</body>
</html>

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.