M_select.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. ;(function($){
  2. var ser_object2,ser_object2_img,radius_v;
  3. jQuery.fn.extend({
  4. // ***********************
  5. // ***********************
  6. // select下拉框
  7. //
  8. // $("").M_select();
  9. // ***********************
  10. // ***********************
  11. "M_select":function(options){
  12. var ojc_arr=this,ojc;
  13. // select下拉框
  14. for(i=0;i<ojc_arr.length;i++){
  15. var ser_object=$(ojc_arr[i]);
  16. // 当前下拉框初始化的对象
  17. var $ser_object=$(ser_object);
  18. $ser_object.parent().addClass("select-con");
  19. $ser_object.parent().css("z-index","1");
  20. $ser_object.wrap("<section class='sel-hide'></section>");
  21. $ser_object.parent().parent().append('<section class="sel-box"><div class="sel-title-box"><span class="sel-title"></span><span class="sel-sign"></span></div><section class="sel-float-box"></section></section>');
  22. var w=$ser_object.parent().parent().width();
  23. $ser_object.parent().parent().find(".sel-float-box").css("width",w);
  24. var ojc=$ser_object.children();
  25. for(a=0;a<ojc.length;a++){
  26. var t=$(ojc[a]).text();
  27. var v=$(ojc[a]).attr("value");
  28. $ser_object.parent().parent().find(".sel-float-box").append('<div class="sel-float-div sel-Hcolor" value='+v+'>'+t+'</div>');
  29. };
  30. for(a=0;a<ojc.length;a++){
  31. var sel_disabled=$(ojc[a]).attr("disabled");
  32. if(sel_disabled=="disabled"||sel_disabled=="true"){
  33. var div_ojc=$ser_object.parent().parent().find('.sel-float-box').children();
  34. $(div_ojc[a]).removeClass("sel-float-div");
  35. $(div_ojc[a]).addClass("sel-float-div-disabled");
  36. }
  37. };
  38. var sel_textFirst=$ser_object.parent().parent().find(".sel-float-box").children().eq(0).text();
  39. // 默认参数
  40. var defaultOption = {
  41. ico:1,
  42. Img:"",
  43. Img2:"",
  44. Title:"",
  45. inputName:"sel_v",
  46. selected:"0",
  47. radius:"0",
  48. Succee:function(){}
  49. };
  50. // 合并自定义参数和默认参数,出现相同属性名时自定义参数会覆盖默认参数
  51. options = $.extend({},defaultOption,options);
  52. // 判断Test是否为空
  53. if(options.Title){
  54. sel_textFirst=options.Title;
  55. };
  56. $ser_object.parent().parent().find(".sel-title").text(sel_textFirst);
  57. // 判断使用哪种ico样式
  58. var Img,Img2;
  59. if(!(options.Img)&&!(options.Img2)){
  60. if(options.ico==1){
  61. Img="./js/M_select/down1.png";
  62. Img2="./js/M_select/up1.png";
  63. }
  64. if(options.ico==2){
  65. Img="./js/M_select/down2.png";
  66. Img2="./js/M_select/up2.png";
  67. }
  68. if(options.ico==3){
  69. Img="./js/M_select/down3.png";
  70. Img2="./js/M_select/up3.png";
  71. }
  72. if(options.ico==4){
  73. Img="./js/M_select/down4.png";
  74. Img2="./js/M_select/up4.png";
  75. }
  76. if(options.ico==5){
  77. Img="./js/M_select/down5.png";
  78. Img2="./js/M_select/up5.png";
  79. }
  80. }else if(!(options.Img)&&(options.Img2!=null)){
  81. Img=options.Img2;
  82. Img2=options.Img2;
  83. }else if(!(options.Img2)&&(options.Img!=null)){
  84. Img=options.Img;
  85. Img2=options.Img;
  86. }else if((options.Img!=null)&&(options.Img2!=null)){
  87. Img=options.Img;
  88. Img2=options.Img2;
  89. }
  90. // 可传参css
  91. $ser_object.parent().parent().find(".sel-sign").css("background","url("+Img+")");
  92. $ser_object.parent().parent().find(".sel-sign").css("background-size","100% 100%");
  93. $ser_object.parent().parent().find(".sel-box").append('<input type="hidden" name="'+options.inputName+'" value="'+options.selected+'" class="inputHidden">');
  94. $ser_object.parent().parent().find(".sel-box").css("border-radius",options.radius);
  95. $ser_object.parent().parent().find(".sel-float-box").css("border-radius","0 0 "+options.radius +" "+options.radius+" ");
  96. // 获取图片的高
  97. var ico_h=$ser_object.parent().parent().find(".sel-title-box").height();
  98. // 设置图片的宽高
  99. $ser_object.parent().parent().find(".sel-sign").css({'width':+ico_h+'px','height':+ico_h+'px'});
  100. $ser_object.parent().parent().find(".sel-title-box").css("padding-right",ico_h+"px");
  101. // 获取有多少个选项
  102. var selLength=$ser_object.parent().parent().find(".sel-float-box").children().length;
  103. var selListHeight=$ser_object.parent().parent().find(".sel-float-box").children().height();
  104. // 获取选项的高度
  105. var selHeight=selListHeight*10;
  106. // 隐藏下拉框
  107. $(".sel-float-box").hide();
  108. if(selLength>10){
  109. $ser_object.parent().parent().find(".sel-float-box").css("height",selHeight);
  110. $ser_object.parent().parent().find(".sel-float-box").css("overflow-y","scroll");
  111. }
  112. // 根据selected 的值选中(即input value)
  113. var input_val=$ser_object.parent().parent().find(".inputHidden").attr("value");
  114. var div_objoct=$ser_object.parent().parent().find(".sel-float-div");
  115. for(a=0;a<div_objoct.length;a++){
  116. var div_val=$(div_objoct[a]).attr("value");
  117. if(div_val==input_val){
  118. var div_text=$(div_objoct[a]).text();
  119. $ser_object.parent().parent().find(".sel-title").text(div_text);
  120. };
  121. };
  122. // 点击事件
  123. $ser_object.parent().parent().find(".sel-title-box").on("click",function(){
  124. var $ser_object2=$(ser_object2);
  125. // 切换图片
  126. $ser_object2.parent().parent().find(".sel-sign").css("background","url("+ser_object2_img+")");
  127. $ser_object2.parent().parent().find(".sel-sign").css("background-size","100% 100%");
  128. $ser_object2.parent().css("border-radius",radius_v);
  129. $('.sel-box').removeClass("on");
  130. ser_object2=this;
  131. ser_object2_img=Img;
  132. // 圆角值
  133. radius_v=options.radius;
  134. var floatAttr=$(this).parent().find(".sel-float-box").css("display");
  135. if(floatAttr=="block"){
  136. $(".sel-float-box").hide();
  137. var $this=$(this).parent();
  138. $this.find(".sel-float-box").hide();
  139. $this.removeClass("on");
  140. $this.find(".sel-float-box").removeClass("on");
  141. $this.css("border-radius",options.radius);
  142. // 切换图片
  143. $this.parent().find(".sel-sign").css("background","url("+Img+")");
  144. $this.parent().find(".sel-sign").css("background-size","100% 100%");
  145. }else if(floatAttr=="none"){
  146. var $this=$(this).parent();
  147. // 切换图片
  148. $this.parent().find(".sel-sign").css("background","url("+Img2+")");
  149. $this.parent().find(".sel-sign").css("background-size","100% 100%");
  150. $(".sel-float-box").hide();
  151. $this.find(".sel-float-box").show();
  152. $this.addClass("on");
  153. $this.find(".sel-float-box").addClass("on");
  154. $this.css("border-radius",options.radius+" "+options.radius+" 0 0");
  155. }
  156. });
  157. // 选项点击事件
  158. $ser_object.parent().parent().find(".sel-float-div").on("click",function(){
  159. var sel_text=$(this).text();
  160. var sel_value=$(this).attr("value");
  161. var $this=$(this).parent();
  162. // 切换图片
  163. $this.parent().find(".sel-sign").css("background","url("+Img+")");
  164. $this.parent().find(".sel-sign").css("background-size","100% 100%");
  165. $this.hide();
  166. $this.parent().find(".sel-title").text(sel_text);
  167. $this.parent().css("border-radius",options.radius);
  168. $this.parent().find(".sel-title").attr("value",sel_value);
  169. var selected_value=$this.parent().find(".sel-title").attr("value");
  170. $this.parent().find(".inputHidden").attr("value",selected_value);
  171. var v;
  172. var ojc_option=$(this).parent().parent().parent().find("option");
  173. for(a=0;a<ojc_option.length;a++){
  174. v=$(ojc_option[a]).attr("value");
  175. if(v==selected_value){
  176. $(ojc_option).attr("selected",false);
  177. $(ojc_option[a]).attr("selected",true);
  178. }
  179. };
  180. // 判断函数是否为空
  181. if(options.Succee!=null){
  182. options.Succee(selected_value);
  183. }
  184. });
  185. }// for循环语句结束
  186. },
  187. });
  188. // 点击其他位置下拉消失
  189. $(document).on('click', function (e) {
  190. var parent = $(e.target).parents('.select-con').length;
  191. if(!parent) {
  192. var $ser_object2=$(ser_object2);
  193. // // 切换图片
  194. $ser_object2.parent().parent().find(".sel-sign").css("background","url("+ser_object2_img+")");
  195. $ser_object2.parent().parent().find(".sel-sign").css("background-size","100% 100%");
  196. $ser_object2.parent().css("border-radius",radius_v);
  197. // console.log(ser_object2);
  198. $('.sel-float-box').hide();
  199. $('.sel-box').removeClass("on");
  200. };
  201. });
  202. })(jQuery);
  203. // jsion对象初始化插件
  204. function M_select_init(ojt){
  205. ojt.forEach(function(val){
  206. $(val.Name).M_select(val.data);
  207. });
  208. };