bootstrap的less源碼學習之button組件控制

本來想看看柵格系統,但後來發現有點多,而且個人有些懶了,就看看button組件算了。做個筆記,緩解下枯燥煩悶的自學。

minxin文件夾中的buttons.less 文件規定按鈕 僞類,按鈕顏色,禁用選項,主要是用來給上一級文件夾中對應buttons.less調用。其中內容如下:

.button-variant(@color; @background; @border) {
  color: @color;
  background-color: @background;
  border-color: @border;

  &:focus,
  &.focus {
    color: @color;
    background-color: darken(@background, 10%);
        border-color: darken(@border, 25%);
  }
  &:hover {
    color: @color;
    background-color: darken(@background, 10%);
        border-color: darken(@border, 12%);
  }
  &:active,
  &.active,
  .open > .dropdown-toggle& {
    color: @color;
    background-color: darken(@background, 10%);
        border-color: darken(@border, 12%);

    &:hover,
    &:focus,
    &.focus {
      color: @color;
      background-color: darken(@background, 17%);
          border-color: darken(@border, 25%);
    }
  }
  &:active,
  &.active,
  .open > .dropdown-toggle& {
    background-image: none;
  }
  &.disabled,
  &[disabled],
  fieldset[disabled] & {
    &:hover,
    &:focus,
    &.focus {
      background-color: @background;
          border-color: @border;
    }
  }

  .badge {
    color: @background;
    background-color: @color;
  }
}

上邊less中設置一個button-variant類,裏面全都是關於a標籤的一些常用顏色屬性color、background-color、border-color,以及常見的鏈接僞類,:active是鼠標點擊和釋放過程之間 ,:focus是獲取焦點,&[disabled]是選擇禁止項。


再來看看less文件夾下buttons.less,規定按鈕基礎樣式,默認備用按鈕,按鈕模擬a鏈接標籤(可能是我菜,搞不懂爲何如此使用),按鈕大小,按鈕(inline-block)轉成塊級元素(block),兄弟按鈕之間添加高度,特殊屬性的input默認樣式重寫

.btn {
  display: inline-block;
  margin-bottom: 0; // For input.btn
  font-weight: @btn-font-weight;
  text-align: center;
  vertical-align: middle;
  touch-action: manipulation;
  cursor: pointer;
  background-image: none; // Reset unusual Firefox-on-Android default style; see https://github.com/necolas/normalize.css/issues/214
  border: 1px solid transparent;
  white-space: nowrap;
  .button-size(@padding-base-vertical; @padding-base-horizontal; @font-size-base; @line-height-base; @btn-border-radius-base);
  .user-select(none);


  &,
  &:active,
  &.active {
    &:focus,
    &.focus {
      .tab-focus();
    }
  }


  &:hover,
  &:focus,
  &.focus {
    color: @btn-default-color;
    text-decoration: none;
  }


  &:active,
  &.active {
    outline: 0;
    background-image: none;
    .box-shadow(inset 0 3px 5px rgba(0,0,0,.125));
  }


  &.disabled,
  &[disabled],
  fieldset[disabled] & {
    cursor: @cursor-disabled;
    .opacity(.65);
    .box-shadow(none);
  }


  a& {
    &.disabled,
    fieldset[disabled] & {
      pointer-events: none; // Future-proof disabling of clicks on `<a>` elements
    }
  }
}

上面.btn是默認按鈕樣式,和mixin中的less區別,它規定按鈕鼠標形態,英文單詞是否換行,垂直居中(vertical-align: middle;要和inline-block配合),touch-action: manipulation;規定用戶能否以及如何操作頁面上的指定區域,背景圖片,邊框等等具體的屬性。

.btn-default {
  .button-variant(@btn-default-color; @btn-default-bg; @btn-default-border);
}
備用按鈕,裏面有些我們國內不常用的警告,危險,成功什麼的。調用mixin文件同名less文件的類

.btn-link {
  color: @link-color;
  font-weight: normal;
  border-radius: 0;

  &,
  &:active,
  &.active,
  &[disabled],
  fieldset[disabled] & {
    background-color: transparent;
    .box-shadow(none);
  }
  &,
  &:hover,
  &:focus,
  &:active {
    border-color: transparent;
  }
  &:hover,
  &:focus {
    color: @link-hover-color;
    text-decoration: @link-hover-decoration;
    background-color: transparent;
  }
  &[disabled],
  fieldset[disabled] & {
    &:hover,
    &:focus {
      color: @btn-link-disabled-color;
      text-decoration: none;
    }
  }
}

模擬鏈接類,我默默地看着常見的鏈接僞類,懸停,下劃線,背景色,邊框,設置透明,無背景,無邊框......

.btn-lg {
  // line-height: ensure even-numbered height of button next to large input
  .button-size(@padding-large-vertical; @padding-large-horizontal; @font-size-large; @line-height-large; @btn-border-radius-large);
}
.btn-sm {
  // line-height: ensure proper height of button next to small input
  .button-size(@padding-small-vertical; @padding-small-horizontal; @font-size-small; @line-height-small; @btn-border-radius-small);
}
.btn-xs {
  .button-size(@padding-xs-vertical; @padding-xs-horizontal; @font-size-small; @line-height-small; @btn-border-radius-small);
}

bootstrap官方使用文檔中,有大中小規格,就是上面了,這裏不過是具體到按鈕調用的同樣是mixin文件夾button.less中規定好的.button-size類:
// Button sizes
.button-size(@padding-vertical; @padding-horizontal; @font-size; @line-height; @border-radius) {
  padding: @padding-vertical @padding-horizontal;
  font-size: @font-size;
  line-height: @line-height;
  border-radius: @border-radius;
}
裏面設置好內邊距,字體大小,行距,邊框圓角

接下來就簡單了,按鈕轉塊級元素,兄弟按鈕垂直方向給個外邊距,特殊屬性的input進行寬度重寫

.btn-block {
  display: block;
  width: 100%;
}

// Vertically space out multiple block buttons
.btn-block + .btn-block {
  margin-top: 5px;
}

// Specificity overrides
input[type="submit"],
input[type="reset"],
input[type="button"] {
  &.btn-block {
    width: 100%;
  }
}


到此,button組件less文件表層研究完畢,裏面使用的無非就是平時網頁經常用到的按鈕顏色,鼠標事件,背景圖片,規定好大中小尺寸,圓角等一切屬性。簡單吧?嗯,表面上看挺簡單的,當然,如果不是爲了學習移動端佈局,我還不知道有這麼個東西。

果然自己渣渣不是環境不好,而是知道的太少!

PS:比起zepto.js源碼,我覺得很爽!!!!



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章