超漂亮的鼠标提示,带人性化缺口

本文介绍了一种利用HTML、CSS及JavaScript实现的鼠标悬停显示提示框的效果。该效果可根据鼠标位置动态调整提示框的位置,适用于展示额外信息或帮助说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>鼠标提示符</title>
<style>
* { padding: 0; margin: 0; }
li { }
body { background: #fdf7f7; }

#explain { height: 60px; border-bottom: 1px solid #999999; background: #eee; font-size: 14px; color: #666; text-align: center; line-height: 60px; }
#explain a { color: #990000; font-weight: bold; text-decoration: none; border-bottom: 1px dotted #990000; }
#explain a:hover { border-bottom: 2px solid #990000; }
#explain strong { color: #990000; }
ul { width: 716px; position: absolute; top: 260px; left: 50%; margin-left: -358px; }
li { width: 160px; height: 100px; list-style: none; background: #fff; padding: 3px; border-top: 1px solid #ddd; border-right: 2px solid #ddd; border-bottom: 2px solid #ddd; border-left: 1px solid #ddd; float: left; margin-right: 10px; cursor: pointer; }
img { float: left; }
#topic { width: 270px; background: #fff; padding: 3px; border-top: 1px solid #ddd; border-right: 2px solid #ddd; border-bottom: 2px solid #ddd; border-left: 1px solid #ddd; position: absolute; top: 100px; left: 200px; }
#topic .adorn { width: 7px; height: 11px; overflow: hidden; background: url(http://www.codefans.net/jscss/demoimg/201010/adorn.gif); position: absolute; bottom: 15px; left: -7px; }
#topic .adorn_r { width: 7px; height: 11px; overflow: hidden; background: url(http://www.codefans.net/jscss/demoimg/201010/adorn_r.gif); position: absolute; bottom: 15px; right: -7px; }
#topic .inner_html { padding: 10px; line-height: 20px; font-size: 12px; color: #666; text-indent: 24px; font-family: arial; }
#topic .inner_html a { color: #990000; font-weight: bold; text-decoration: none; border-bottom: 1px dotted #990000; }
#topic .inner_html a:hover { border-bottom: 2px solid #990000; }
</style>
<script type="text/javascript">
var g_aData=
[
 '石川(blue)为大家分享了以下内容:新浪微博效果、DOM、闭包使用技巧、面向对象、高级拖拽、运动特效、AJax、官网导航效果等^_^!',
 '课程内容特别精选了JavaScript的高级DOM操作、AJAX技术应用、OOP思想、继承等知识进行深度剖析,力图为学员揭秘各种网站交互效果,并帮助学员建立正确而清晰的编程思路……',
 '高级页面架构师精品课程是为了让大家制作出较为规范的页面,例如:符合 W3C 标准、标签语义化、模块化布局、能熟练解决浏览器兼容性、能洞晰 CSS 代码性能等问题的朋友们而设。通过这门课程,你可以充分了解到标准带来的好处、页面代码的简洁与 CSS 样式的高重用性……',
 '零基础网页制作精品课程站在完全不懂的学员角度考虑,在 课程安排、课后辅导 等几个方面着手,力求为学员带来一门系统化极强、讲解风格却通俗易懂的 精品入门课程,欢迎朋友们来试听,一探究竟!'
];

var g_oTimerHide=null;

window.onload=function ()
{
 var aLi=document.getElementById('content').getElementsByTagName('li');
 
 bindTopic(aLi);
};

function bindTopic(aElement)
{
 var i=0;
 
 for(i=0;i<aElement.length;i++)
 {
  aElement[i].miaovIndex=i;
  aElement[i].onmouseover=function (ev){showTopic(this.miaovIndex, window.event || ev);};
  aElement[i].onmouseout=function (){hideTopic();};
  aElement[i].onmousemove=function (ev)
  {
   var oEvent=window.event || ev;
   setPosition(oEvent.clientX, oEvent.clientY);
  };
 }
}

function showTopic(index, oEvent)
{
 var oTopic=document.getElementById('topic');
 
 if(g_oTimerHide)
 {
  clearTimeout(g_oTimerHide);
 }
 
 oTopic.getElementsByTagName('div')[1].innerHTML=g_aData[index];
 oTopic.style.display='block';
 
 setPosition(oEvent.clientX, oEvent.clientY);
}

function hideTopic()
{
 var oTopic=document.getElementById('topic');
 
 if(g_oTimerHide)
 {
  clearTimeout(g_oTimerHide);
 }
 g_oTimerHide=setTimeout
 (
  function ()
  {
   oTopic.style.display='none';
  },50
 );
}

function setPosition(x, y)
{
 var top=document.body.scrollTop || document.documentElement.scrollTop;
 var left=document.body.scrollLeft || document.documentElement.scrollLeft;
 
 x+=left;
 y+=top;
 
 var oTopic=document.getElementById('topic');
 var l=x+20;
 var t=y-(oTopic.offsetHeight-20);
 var bRight=true;
 var iPageRight=left+document.documentElement.clientWidth;
 
 if(l+oTopic.offsetWidth>iPageRight)
 {
  bRight=false;
  
  l=x-(oTopic.offsetWidth+20);
  oTopic.getElementsByTagName('div')[0].className='adorn_r';
 }
 else
 {
  oTopic.getElementsByTagName('div')[0].className='adorn';
 }
 
 oTopic.style.left=l+'px';
 oTopic.style.top=t+'px';
}
</script>
</head>

<body>
<div id="explain">可以调整窗口大小,再把鼠标移到图片上查看……</div>
<div id="topic" style="display: none; z-index:2">
 <div class="adorn"></div>
 <div class="inner_html"></div>
</div>

<ul id="content">
 <li><img src="http://www.codefans.net/jscss/demoimg/wall_s1.jpg" alt="www.16sucai.com" longdesc="#" /></li>
 <li><img src="http://www.codefans.net/jscss/demoimg/wall_s2.jpg" alt="www.16sucai.com" longdesc="#" /></li>
 <li><img src="http://www.codefans.net/jscss/demoimg/wall_s3.jpg" alt="www.16sucai.com" longdesc="#" /></li>
 <li><img src="http://www.codefans.net/jscss/demoimg/wall_s4.jpg" alt="www.16sucai.com" longdesc="#" /></li>
</ul>

</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值