首页 > 语言 > JavaScript > 正文

js 右侧浮动层效果实现代码(跟随滚动)

2024-05-06 16:25:13
字体:大 中 小
来源:转载
供稿:网友

因为项目上有这样的需求,在网上也查了些东西,之前是想找个差不多类似的套用一下。后来发觉没有合适的,因时间紧迫就自己动手写了一个简单的 ,示例代码如下 兼容火狐和IE7+

实现代码一、

 

 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " 
  2. <html> 
  3. <head> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
  5. <title>无标题文档</title> 
  6. <!--***********开始*************--> 
  7.  
  8. <script type="text/javascript"> 
  9. //<![CDATA[  
  10. var tips; var theTop = 200/*这是默认高度,越大越往下*/; var old = theTop; 
  11. function initFloatTips() { 
  12. tips = document.getElementById('floatTips'); 
  13. moveTips(); 
  14. }; 
  15. function moveTips() { 
  16. var tt = 50; 
  17. if (window.innerHeight) { 
  18. pos = window.pageYOffset 
  19. } 
  20. else if (document.documentElement && document.documentElement.scrollTop) { 
  21. pos = document.documentElement.scrollTop 
  22. } 
  23. else if (document.body) { 
  24. pos = document.body.scrollTop; 
  25. } 
  26. pos = pos - tips.offsetTop + theTop; 
  27. pos = tips.offsetTop + pos / 10; 
  28. if (pos < theTop) pos = theTop; 
  29. if (pos != old) { 
  30. tips.style.top = pos + "px"; 
  31. tt = 10; 
  32. } 
  33. old = pos; 
  34. setTimeout(moveTips, tt); 
  35. } 
  36. //!]]>  
  37. </script> 
  38.  
  39. <style type="text/css"> 
  40. .floatTips 
  41. { 
  42. position: absolute; 
  43. border: solid 1px #777; 
  44. padding: 3px; 
  45. top: 250px; 
  46. right: 5px; 
  47. width: 30px; 
  48. height: 300px; 
  49. background: #cccccc; 
  50. color: white; 
  51. } 
  52. .showDiv 
  53. { 
  54. position: absolute; 
  55. border: solid 1px #777; 
  56. padding: 3px; 
  57. top: 250px; 
  58. right: 5px; 
  59. width: 300px; 
  60. height: 300px; 
  61. background: #cccccc; 
  62. color: white; 
  63. } 
  64. </style> 
  65. <script type="text/javascript"> 
  66. function FunOnmouseUp() { 
  67. var objFloatTips = $("floatTips"); 
  68. var objActivityContext = $("activityContext"); 
  69. objFloatTips.className = "showDiv"; 
  70. objActivityContext.style.display = ""; 
  71. } 
  72. function FunMouseOut() { 
  73. var objFloatTips = $("floatTips"); 
  74. var objActivityContext = $("activityContext"); 
  75. objFloatTips.className = "floatTips"; 
  76. objActivityContext.style.display = "none"; 
  77. } 
  78.  
  79. function $(objID) { 
  80. return document.getElementById(objID); 
  81. } 
  82. </script> 
  83.  
  84. </head> 
  85. <body onload="initFloatTips()"> 
  86. <div id="floatTips" onmouseover="FunOnmouseUp()" onmouseout="FunMouseOut()" class="floatTips"> 
  87. 最新的活动 
  88. <div id="activityContext" style="display: none"> 
  89. 显示最新的活动 
  90. </div> 
  91. </div> 
  92. <!--**********结束***************--> 
  93. <table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#F7F7F7"> 
  94. <tr> 
  95. <td height="2101"> 
  96. </td> 
  97. </tr> 
  98. </table> 
  99. </body> 
  100. </html> 

如果有时间的话 会稍作美化 然后加上动画效果 应该就能算是个不错的浮动隐藏层了。

方法二、

一、把以下代码插入

标签中:

 

 
  1. <div id="FloatDIV" style="position: absolute;top: 0px; border-right: activeborder 1px solid; border-top: activeborder 1px solid; border-left: activeborder 1px solid; border-bottom: activeborder 1px solid;"> 
  2. 浮动层示例:<br /> 
  3. <a target="_blank" href="tencent://message/?uin=101535223&Site=http://www.vevb.com&Menu=yes"><img border="0" src="http://wpa.qq.com/pa?p=1:101535223:3" alt="有事您说话"></a> 
  4. </div> 

二、把以下代码插到标签之后,设置MarginLeft、MarginTop、Width、Heigth四个变量的值

 

 
  1. <script language="javascript" type="text/javascript"> 
  2. var MarginLeft = 30; //浮动层离浏览器右侧的距离 
  3. var MarginTop = 50; //浮动层离浏览器顶部的距离 
  4. var Width = 120; //浮动层宽度 
  5. var Heigth= 45; //浮动层高度 
  6.  
  7. //设置浮动层宽、高 
  8. function Set() 
  9. { 
  10. document.getElementById("FloatDIV").style.width = Width; 
  11. document.getElementById("FloatDIV").style.height = Heigth; 
  12. } 
  13.  
  14. //实时设置浮动层的位置 
  15. function Move() 
  16. { 
  17. document.getElementById("FloatDIV").style.top = document.documentElement.scrollTop + MarginTop; 
  18. document.getElementById("FloatDIV").style.left = document.documentElement.clientWidth - Width - MarginLeft; 
  19. setTimeout("Move();",100); 
  20. } 
  21.  
  22. Set(); 
  23. Move(); 
  24. </script> 


注:相关教程知识阅读请移步到JavaScript/Ajax教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选