jquery.DMenu.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*弹出式菜单*/
  2. //没剑 2008-07-02
  3. //http://regedit.cnblogs.com
  4. /*参数说明*/
  5. //showobj:要显示的菜单ID
  6. //timeout:延时时间,鼠标停留/离开后延时多久开始显示/隐藏菜单
  7. //speed:菜单显示速度,数字越大,显示越慢,默认为100
  8. //调用示例:$("#button").DMenu("#content");
  9. jQuery.fn.DMenu=function(showobj,timeout,speed){
  10. timeout=timeout?timeout:300;
  11. speed=speed?speed:100;
  12. //按钮对象
  13. var button=$(this);
  14. //延时计数器
  15. var timer=null;
  16. //隐藏的浮动层
  17. var hideDiv=$("<div></div>");
  18. //容器对象
  19. var Container=$("<div id=\"Container\"></div>");
  20. Container.hide();
  21. hideDiv.append(Container);
  22. //菜单对象
  23. var jqShowObj=$(showobj);
  24. //隐藏菜单
  25. jqShowObj.hide();
  26. //菜单显示的状态
  27. var display=false;
  28. //按钮的offset
  29. var offset=button.offset();
  30. //菜单区高
  31. var height=jqShowObj.height();
  32. //菜单区宽
  33. var width=jqShowObj.width();
  34. //按钮的高
  35. var btnHeight=button.height();
  36. //按钮的宽
  37. var btnWidth=button.width();
  38. //定位层放到最前面
  39. $(document.body).prepend(hideDiv);
  40. //放到容器中
  41. //Container.append(jqShowObj);
  42. //****显示菜单方法开始****//
  43. var showMenu=function(){
  44. //如果菜单为显示则退出操作
  45. if (display)
  46. {
  47. return false;
  48. }
  49. //设置容器属性
  50. Container.css({
  51. margin:"0 auto",
  52. width:btnWidth+"px",
  53. height:btnHeight+"px"
  54. });
  55. //定位隐藏层
  56. hideDiv.css({
  57. position:"absolute",
  58. top:offset.top+"px",
  59. left:offset.left+(btnWidth/2)-(width/2)+"px",
  60. height:height+"px",
  61. width:width+"px"
  62. }).show();
  63. //给容器加个黑边框
  64. Container.css({
  65. border:"1px solid #666666"
  66. });
  67. //显示定位层
  68. //高宽慢慢增大
  69. Container.animate({
  70. marginTop:btnHeight+4,
  71. height:height+4,
  72. width:width+4,
  73. opacity:'100'},speed,function(){
  74. //动画结束时 start//
  75. //显示菜单
  76. jqShowObj.show();
  77. //添加菜单入容器
  78. Container.append(jqShowObj);
  79. //去除边框
  80. Container.css({
  81. //border:"0px"
  82. });
  83. //显示状态置为true
  84. display=true;
  85. //鼠标移入
  86. jqShowObj.mouseover(function(){
  87. clearTimeout(timer);
  88. });
  89. //鼠标移开
  90. jqShowObj.mouseout(function(){
  91. hideMenu();
  92. });
  93. //动画结束时 end//
  94. });
  95. };
  96. //****显示菜单方法结束****//
  97. //****隐藏菜单方法开始****//
  98. var hideMenu=function(){
  99. clearTimeout(timer);
  100. //延时隐藏菜单
  101. timer=setTimeout(function(){
  102. //显示边框
  103. Container.css({
  104. border:"1px solid #666666"
  105. });
  106. //清空容器
  107. Container.empty();
  108. //收缩容器
  109. Container.animate({
  110. width:btnWidth,height:btnHeight,marginTop:'0', opacity: '0'
  111. }, speed,function(){
  112. //动画结束时 start//
  113. //隐藏容器
  114. Container.hide();
  115. //定位层隐藏
  116. hideDiv.hide();
  117. //显示状态置为false
  118. display=false;
  119. //动画结束时 end//
  120. });
  121. }, timeout);
  122. };
  123. //****隐藏菜单方法结束****//
  124. //绑定按钮鼠标经过事件
  125. button.hover(function(e){
  126. //延时显示菜单
  127. clearTimeout(timer);
  128. timer=setTimeout(function(){
  129. showMenu();
  130. }, timeout);
  131. },function(){
  132. clearTimeout(timer);
  133. //鼠标离开按钮时,如果菜单还是显示状态则隐藏
  134. if(display){
  135. timer=setTimeout(function(){
  136. hideMenu();
  137. },timeout);
  138. }
  139. });
  140. };