首页 > 语言 > JavaScript > 正文

JavaScript实现鼠标点击后层展开效果的方法

2024-05-06 16:20:03
字体:
来源:转载
供稿:网友

这篇文章主要介绍了JavaScript实现鼠标点击后层展开效果的方法,可实现点击文字缓慢展开层的效果,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了JavaScript实现鼠标点击后层展开效果的方法。分享给大家供大家参考。具体分析如下:

这段JavaScript代码可实现鼠标点击后层展开的功能,代码稍嫌复杂,重在学习研究,有兴趣的可以修改简化一下。

 

 
  1. <html> 
  2. <head> 
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"
  4. <title>层展开折叠</title> 
  5. </head> 
  6. <body> 
  7. <style type="text/css"
  8. DIV.clTopMenu{ 
  9. position:absolute; 
  10. width:101; 
  11. height:150; 
  12. clip:rect(0,101,14,0); 
  13. visibility:hidden; 
  14. z-index:31; 
  15. layer-background-color:#EEEEEE; 
  16. background-color:#00CCFF 
  17. DIV.clTopMenuBottom{ 
  18. position:absolute; 
  19. width:101; 
  20. height:3; 
  21. clip:rect(0,101,3,0); 
  22. top:11; 
  23. layer-background-color:#CECFCE; 
  24. background-color:#000099; 
  25. z-index:2 
  26. DIV.clTopMenuText{ 
  27. position:absolute; 
  28. width:91; 
  29. left:5; 
  30. top:15; 
  31. font-family:"Arial""Helvetica""sans-serif"
  32. font-size:9pt; 
  33. background-color:#EEEEEE; 
  34. z-index:1; 
  35. color: #000000 
  36. }  
  37. a:link { 
  38. color: #000000; 
  39. text-decoration: none 
  40. </style> 
  41. <script language="JavaScript" type="text/javascript"
  42. function lib_bwcheck(){ //Browsercheck (needed) 
  43. this.ver=navigator.appVersion 
  44. this.agent=navigator.userAgent 
  45. this.dom=document.getElementById?1:0 
  46. this.opera5=this.agent.indexOf("Opera 5")>-1 
  47. this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0;  
  48. this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0; 
  49. this.ie4=(document.all && !this.dom && !this.opera5)?1:0; 
  50. this.ie=this.ie4||this.ie5||this.ie6 
  51. this.mac=this.agent.indexOf("Mac")>-1 
  52. this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0;  
  53. this.ns4=(document.layers && !this.dom)?1:0; 
  54. this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5) 
  55. return this 
  56. var bw=new lib_bwcheck() 
  57. nPlace=0 
  58. var nNumberOfMenus=2 
  59. var nMwidth=101 
  60. var nPxbetween=20 
  61. var nFromleft=10  
  62. var nFromtop=20 
  63. var nBgcolor='#CECFCE' 
  64. var nBgcolorchangeto='#6380BC' 
  65. var nImageheight=11  
  66. function makeNewsMenu(obj,nest){ 
  67. nest=(!nest) ? "":'document.'+nest+'.' 
  68. this.css=bw.dom? document.getElementById(obj).style:bw.ie4?document.all[obj].style:bw.ns4?eval(nest+"document.layers." +obj):0;  
  69. this.evnt=bw.dom? document.getElementById(obj):bw.ie4?document.all[obj]:bw.ns4?eval(nest+"document.layers." +obj):0;  
  70. this.scrollHeight=bw.ns4?this.css.document.height:this.evnt.offsetHeight 
  71. this.moveIt=b_moveIt;this.bgChange=b_bgChange; 
  72. this.slideUp=b_slideUp; this.slideDown=b_slideDown; 
  73. this.clipTo=b_clipTo; 
  74. this.obj = obj + "Object"; eval(this.obj + "=this")  
  75. function b_moveIt(x,y){this.x=x; this.y=y; this.css.left=this.x; this.css.top=this.y} 
  76. function b_bgChange(color){if(bw.dom || bw.ie4) this.css.backgroundColor=color; else this.css.bgColor=color} 
  77. function b_clipTo(t,r,b,l){ 
  78. if(bw.ns4){this.css.clip.top=t; this.css.clip.right=r; this.css.clip.bottom=b; this.css.clip.left=l 
  79. }else this.css.clip="rect("+t+","+r+","+b+","+l+")"
  80. function b_slideUp(ystop,moveby,speed,fn,wh){ 
  81. if(!this.slideactive){ 
  82. if(this.y>ystop){ 
  83. this.moveIt(this.x,this.y-5); eval(wh) 
  84. setTimeout(this.obj+".slideUp("+ystop+","+moveby+","+speed+",'"+fn+"','"+wh+"')",speed) 
  85. }else
  86. this.slideactive=falsethis.moveIt(0,ystop); eval(fn) 
  87. function b_slideDown(ystop,moveby,speed,fn,wh){ 
  88. if(!this.slideactive){ 
  89. if(this.y<ystop){ 
  90. this.moveIt(this.x,this.y+5); eval(wh) 
  91. setTimeout(this.obj+".slideDown("+ystop+","+moveby+","+speed+",'"+fn+"','"+wh+"')",speed) 
  92. }else
  93. this.slideactive=falsethis.moveIt(0,ystop); eval(fn) 
  94. function newsMenuInit(){ 
  95. oTopMenu=new Array() 
  96. zindex=10 
  97. for(i=0;i<=nNumberOfMenus;i++){ 
  98. oTopMenu[i]=new Array() 
  99. oTopMenu[i][0]=new makeNewsMenu('divTopMenu'+i) 
  100. oTopMenu[i][1]=new makeNewsMenu('divTopMenuBottom'+i,'divTopMenu'+i) 
  101. oTopMenu[i][2]=new makeNewsMenu('divTopMenuText'+i,'divTopMenu'+i) 
  102. oTopMenu[i][1].moveIt(0,nImageheight) 
  103. oTopMenu[i][0].clipTo(0,nMwidth,nImageheight+3,0) 
  104. if(!nPlace) oTopMenu[i][0].moveIt(i*nMwidth+nFromleft+(i*nPxbetween),nFromtop) 
  105. else
  106. oTopMenu[i][0].moveIt(nFromleft,i*nImageheight+nFromtop+(i*nPxbetween)) 
  107. oTopMenu[i][0].css.zIndex=zindex-- 
  108. oTopMenu[i][0].css.visibility="visible" 
  109. function topMenu(num){ 
  110. if(oTopMenu[num][1].y==nImageheight) oTopMenu[num][1].slideDown(oTopMenu[num][2].scrollHeight+20,10,40,'oTopMenu['+num+'][0].clipTo(0,nMwidth,oTopMenu['+num+'][1].y+3,0)','oTopMenu['+num+'][0].clipTo(0,nMwidth,oTopMenu['+num+'][1].y+3,0)'
  111. else if(oTopMenu[num][1].y==oTopMenu[num][2].scrollHeight+20) oTopMenu[num][1].slideUp(nImageheight,10,40,'oTopMenu['+num+'][0].clipTo(0,nMwidth,oTopMenu['+num+'][1].y+3,0)','oTopMenu['+num+'][0].clipTo(0,nMwidth,oTopMenu['+num+'][1].y+3,0)'
  112. function menuOver(num){oTopMenu[num][1].bgChange(nBgcolorchangeto)} 
  113. function menuOut(num){oTopMenu[num][1].bgChange(nBgcolor)} 
  114. onload=newsMenuInit; 
  115. </script> 
  116. <div id="divTopMenu0" class="clTopMenu" align="center" style="height: 170; left: 20; top: 20"><span class="css3"><a href="#" onMouseOver="menuOver(0)" onMouseOut="menuOut(0)" onClick="topMenu(0); if(bw.dom || bw.ie4)this.blur(); return false">点击展开</a> 
  117. </span>  
  118. <div id="divTopMenuText0" class="clTopMenuText"
  119. <span class="css3"
  120. 鼠标点击后层展开JavaScript鼠标点击后层展开JavaScript。 
  121. </span> 
  122. </div> 
  123. <div id="divTopMenuBottom0" class="clTopMenuBottom" style="top: 11; height: 10"></div> 
  124. </div> 
  125. </div> 
  126. </body> 
  127. </html> 

希望本文所述对大家的javascript程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选