欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JQuery組件基于Bootstrap的DropDownList(完整版)

 更新時間:2016年07月05日 15:06:12   作者:萬倉一黍  
這篇文章主要介紹了JQuery組件基于Bootstrap的DropDownList的完整版,在原有基礎上進行完善功能,感興趣的小伙伴們可以參考一下

在前文 創(chuàng)建基于Bootstrap的下拉菜單的DropDownList的JQuery插件 中,實現(xiàn)了DropDownList的JQuery組件,但是留有遺憾。就是當下拉菜單出現(xiàn)滾動條的時候,滾動條會覆蓋菜單右側(cè)的兩個圓角。使得下拉菜單左側(cè)有2個圓角,右側(cè)沒有,看上去不是很完美。如下圖所示:

本文的內(nèi)容就是如何恢復右側(cè)的圓角 

先看看原本的下拉菜單的HTML結(jié)構:

 <ul class="dropdown-menu dropdown-menu-right" role="menu">
 <li><a href="#">Action</a></li>
 <li><a href="#">Another action</a></li>
 <li><a href="#">Something else here</a></li>
 <li class="divider"></li>
 <li><a href="#">Separated link</a></li>
</ul>

從上面的結(jié)構可以看出,由ul標簽實現(xiàn)下拉菜單的外觀(通過引用dropdown-menu樣式),由li標簽實現(xiàn)菜單項(包括菜單、分隔符、組標題)。來看看ul和li標簽對應的CSS:

.dropdown-menu {
 position: absolute;
 top: 100%;
 left: 0;
 z-index: 1000;
 display: none;
 float: left;
 min-width: 160px;
 padding: 5px 0;
 margin: 2px 0 0;
 font-size: 14px;
 text-align: left;
 list-style: none;
 background-color: #fff;
 -webkit-background-clip: padding-box;
  background-clip: padding-box;
 border: 1px solid #ccc;
 border: 1px solid rgba(0, 0, 0, .15);
 border-radius: 4px;
 -webkit-box-shadow: 0 6px 12px rgba(0, 0, 0, .175);
  box-shadow: 0 6px 12px rgba(0, 0, 0, .175);
}

.dropdown-menu > li > a {
 display: block;
 padding: 3px 20px;
 clear: both;
 font-weight: normal;
 line-height: 1.42857143;
 color: #333;
 white-space: nowrap;
}

.dropdown-menu > li > a:hover,
.dropdown-menu > li > a:focus {
 color: #262626;
 text-decoration: none;
 background-color: #f5f5f5;
}


由于a的樣式是通過.dropdown-menu > li > a來實現(xiàn)的,故要實現(xiàn)a的外觀必須是在含有樣式dropdown-menu的ul里面的li的里面的a的。 

于是,動了一個念頭,在HTML結(jié)構里的ul里面的li里再嵌套一個包含樣式dropdown-menu的ul,ul里面是li,li里面是a。

但是從上面的CSS可以看出,嵌套在里面的ul也會實現(xiàn)菜單的外觀(圓角、投影、浮動等),故在該ul的標簽里強制添加style屬性,把一些樣式強制性的去除(改成inherit,采用默認樣式),這些樣式包括display、position、top、float、padding、border、border-radius、-webkit-box-shadow、box-shadow。 

再說說MaxHeight。本次修改后直接采用CSS的樣式max-height,而減少對菜單高度的判斷。會有疑問,如果瀏覽器不支持max-height怎么辦?一是不支持max-height的瀏覽器比較少(IE6等),二是如果瀏覽器不支持max-height,也就不能很好的支持Bootstrap。故不必考慮瀏覽器是否支持max-height屬性。由于里外有2個ul標簽,我們需要對里面的ul標簽應用max-height屬性,故用UL=Obj.find("ul[style]")語句來找尋里面的ul標簽(因為里面的ul含有style屬性,而外面的ul沒有)。 

再說說JQuery的height方法。當調(diào)用JQuery的height方法來計算隱藏元素高度時,估計是先會顯示元素,然后計算高度,再隱藏元素。這會有兩個問題。一是顯示再隱藏,速度很快,肉眼看不出,但是瀏覽器不會說謊,有時瀏覽器會額外顯示滾動條。二是如果該元素的父元素也是隱藏的,則height方法會返回0。 

完善版的源代碼:

(function($){
 jQuery.fn.DropDownList = function(options) {
 var defaults ={
  InputName:"Q",
  ButtonText:"示例",
  ReadOnly:true,
  MaxHeight:-1,
  onSelect:$.noop(),
 }

 var options = $.extend(defaults,options);

 return this.each(function(){
  var o=options;
  var Obj=$(this);

  var S="<div class='input-group'>";
  S = S + "<input type='text' class='form-control' name='" + o.InputName + "' id='" + o.InputName + "' />";
  S = S + "<div class='input-group-btn'>";
  S = S + "<button type='button' class='btn btn-default dropdown-toggle' data-toggle='dropdown'>" + o.ButtonText + " <span class='caret'></span></button>";
  S = S + "<ul class='dropdown-menu dropdown-menu-right' role='menu' >";
  S = S + "<li><ul class='dropdown-menu ' style='display:inherit;position:inherit;top:0;float:inherit;padding:0;border:0px;border-radius:0px;-webkit-box-shadow: inherit;box-shadow: inherit;'>";

  var SelText,SelData;

  if (o.Sections!== undefined)
  {
   $.each(o.Sections,function(n,value){
   if (n>0) {S=S + "<li class='divider'></li>";}
   if (value.ItemHeader!==undefined) {S = S + "<li class='dropdown-header'>" + value.ItemHeader + "</li>";}
   CreateItem(value);
   });
  }
  else
  {
   CreateItem(o);
  }
 
  function CreateItem(Items)
  {
  $.each(Items.Items,function(n,Item){
   if (Item.ItemData===undefined) {Item.ItemData=Item.ItemText;}
   S=S + "<li><a href='#' ItemData='" + Item.ItemData + "' >" + Item.ItemText + "</a></li>";
   if (Item.Selected==true) {SelText=Item.ItemText;SelData=Item.ItemData;}
  });
  }
 
  S =S + "</ul></li></ul></div></div>";

  Obj.html(S);

  var Input=Obj.find("input");

  if (SelText!="") {SetData(SelText,SelData);}

  Obj.find("a").bind("click", function(e) {
      SetData($(this).html(),$(this).attr("ItemData"));
     });

  if (o.ReadOnly==true)
  {
  Input.bind("cut copy paste keydown", function(e) {e.preventDefault();}); 
  }

  if (o.MaxHeight>0)
  {
  var UL=Obj.find("ul[style]");
  UL.css({'max-height':o.MaxHeight,'overflow':'auto'});
  }

  function SetData(Text,Data)
  {
  Input.val(Text);
  if (o.onSelect) {o.onSelect(o.InputName,Data);}
  }

 });
 }
})(jQuery);


樣張:

 

這樣通過兩層的ul實現(xiàn)了下拉菜單,并且滾動條也沒有覆蓋右側(cè)的兩個圓角。較之上個版本,比較完善。

如果大家還想深入學習,可以點擊這里進行學習,再為大家附3個精彩的專題:

Bootstrap學習教程

Bootstrap實戰(zhàn)教程

Bootstrap插件使用教程

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論