jquery-rockmenu.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. rockmenu 菜单选择插件
  3. caratename:chenxihu
  4. caratetime:214-04-06 21:40:00
  5. email:qqqq2900@126.com
  6. homepage:www.xh829.com
  7. */
  8. var rockmenuobj = null;
  9. (function ($) {
  10. function rockmenu(element, options){
  11. var obj = element;
  12. var can = options;
  13. var json = can.data;
  14. var rand = js.getrand();
  15. var me = this;
  16. this.obj = obj;
  17. //初始化
  18. this.init = function(){
  19. if(!obj)return;
  20. obj[can.trigger](function(){
  21. me.setcontent();
  22. return false;
  23. });
  24. };
  25. this.hide = function(){
  26. var o = this.mdivobj;
  27. if(!o)return;
  28. o.hide();
  29. if(can.bgcolor!='')obj.css('background','');
  30. if(can.autoremove)o.remove();
  31. };
  32. this.setcontent = function(){
  33. $('.rockmenu').remove();
  34. rockmenuobj = this;
  35. can.beforeshow(this);
  36. if(json.length<=0)return false;
  37. if(can.bgcolor!='')obj.css('background',can.bgcolor);
  38. if(can.autoremove)$('#rockmenu_'+rand+'').remove();
  39. if(document.getElementById('rockmenu_'+rand+'')){
  40. this.setweizhi();
  41. return false;
  42. }
  43. var len = json.length;
  44. var str = '<div class="rockmenu" id="rockmenu_'+rand+'">';
  45. if(can.arrowup)str+='<div class="arrow-up"></div>';
  46. str+='<div style="background:'+can.background+';" id="rockmenuli_'+rand+'" class="rockmenuli '+can.maincls+'"><ul>';
  47. var s = '',ys='',col,va;
  48. for(var i=0; i<len; i++){
  49. ys= '',
  50. va= json[i][can.display];
  51. if(i==len-1)ys+='border:none;';
  52. col = '';
  53. if(json[i].color)ys+='color:'+json[i].color+';';
  54. if(va==can.value)col='#e1e1e1';
  55. if(json[i].background)col=json[i].background;
  56. if(col)ys+='background:'+col+';';
  57. s = '<li temp="'+i+'" style="'+ys+'">';
  58. var s1 = can.resultbody(json[i], this, i);
  59. if(!s1){
  60. if(json[i].icons)s+='<img src="'+json[i].icons+'" width="'+can.iconswh+'" height="'+can.iconswh+'" align="absmiddle">&nbsp;';
  61. s+=va;
  62. }else{
  63. s+=s1;
  64. }
  65. s+='</li>';
  66. str+=s;
  67. }
  68. str+='</ul></div></div>';
  69. $('body').prepend(str);
  70. var oac = $('#rockmenu_'+rand+'');
  71. can.aftershow(this);
  72. oac.find('li').mouseover(function(){this.className=can.overcls;});
  73. oac.find('li').mouseout(function(){this.className='';});
  74. oac.find('li').click(function(){me.itemsclick(this);});
  75. if(can.width!=0){
  76. $('#rockmenuli_'+rand+'').css('width',''+can.width+'px');
  77. };
  78. js.addbody(rand, 'remove', 'rockmenu_'+rand+'');
  79. this.mdivobj = oac;
  80. this.setweizhi();
  81. };
  82. this.showAt = function(l, t, w){
  83. this.setcontent();
  84. var oac = this.mdivobj;
  85. if(!oac)return;
  86. if(w)this.setWidth(w);
  87. this._reshewhere(l,t);
  88. };
  89. this.offset=function(l,t){
  90. this._reshewhere(l,t);
  91. };
  92. this.getHeight = function(){
  93. return get('rockmenu_'+rand+'').scrollHeight;
  94. };
  95. this._reshewhere=function(l,t){
  96. var oac = this.mdivobj;
  97. var jg = (l+oac.width()+5 - winWb()),jg1=0;
  98. if(jg>0)l=l-jg;
  99. jg1 = t+get('rockmenu_'+rand+'').scrollHeight+10-winHb();
  100. if(jg1>0)t=t-jg1;
  101. if(t<5)t=5;
  102. oac.css({'left':''+l+'px','top':''+t+'px'});
  103. };
  104. this.setValue = function(v){
  105. can.value = v;
  106. };
  107. this.removeItems = function(oi){
  108. $('#rockmenu_'+rand+'').find("li[temp='"+oi+"']").remove();
  109. };
  110. this.setWidth = function(w){
  111. var oac = this.mdivobj;
  112. if(!oac)return;
  113. oac.css({'width':''+w+'px'});
  114. };
  115. this.setweizhi = function(){
  116. var oac = this.mdivobj;
  117. if(can.donghua)oac.slideDown(100);
  118. oac.show();
  119. if(!obj)return;
  120. var off = obj.offset(),
  121. l = off.left+ can.left,
  122. t = off.top+can.top;
  123. this._reshewhere(l,t);
  124. };
  125. //项目单击
  126. this.itemsclick = function(o){
  127. var oi = parseInt($(o).attr('temp'));
  128. can.itemsclick(json[oi],oi,me);
  129. if(can.autohide)this.hide();
  130. };
  131. this.setData = function(da){
  132. can.data= da;
  133. json = da;
  134. can.autoremove = true;
  135. };
  136. this.remove = function(){
  137. this.hide();
  138. }
  139. };
  140. $.rockmenu = function(options, dxo){
  141. var defaultVal = {
  142. data:[],
  143. display:'name',//显示的名称
  144. left:0,
  145. overcls:'li01',
  146. maincls:'',
  147. top:0,
  148. width:0,value:'',
  149. iconswh:16,
  150. itemsclick:function(){},
  151. beforeshow:function(){},
  152. aftershow:function(){},
  153. autoremove:true,
  154. trigger:'click',
  155. autohide:true,
  156. arrowup:false, //是否有箭头
  157. background:'#ffffff',//背景颜色
  158. bgcolor:'',
  159. resultbody:function(){
  160. return '';
  161. },
  162. donghua:false
  163. };
  164. var can = $.extend({}, defaultVal, options);
  165. var menu = new rockmenu(dxo, can);
  166. menu.init();
  167. return menu;
  168. }
  169. $.fn.rockmenu = function(options){
  170. return $.rockmenu(options, $(this));
  171. };
  172. })(jQuery);