從WeUI學習到的知識點

WeUI是微信Web服務開發的UI套件, 目前包含12個模塊 (Button, Cell, Toast, Dialog, Progress, Msg, Article, ActionSheet, Icons, Panel, Tab, SearchBar).javascript

Demo頁面: https://weui.iocss

Github頁面: https://github.com/weui/weuihtml

下面講一講我從WeUI中學到的CSS技巧.html5

Button

從這裏我開始注意到, WeUI的實現中, 不少邊框都是用:before, :after繪製的.java

.weui_btn:after {
    content: " ";
    width: 200%;
    height: 200%;
    position: absolute;
    top: 0;
    left: 0;
    border: 1px solid rgba(0, 0, 0, 0.2);
    -webkit-transform: scale(0.5);
    transform: scale(0.5);
    -webkit-transform-origin: 0 0;
    transform-origin: 0 0;
    box-sizing: border-box;
    border-radius: 10px;
}

這麼作是爲了在Retina屏(視網膜屏)上確保1px真的是1pixel. 詳見Retina屏的移動設備如何實現真正1px的線?git

Cell

weui_cell

.weui_cell {
    padding: 10px 15px;
    position: relative;
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-align: center;
    -webkit-align-items: center;
    -ms-flex-align: center;
    align-items: center;
}

看到這裏發現WeUI大量使用了flex佈局方式. 對這個佈局方式我整理了另外一篇文章, 見flex佈局全解析.github

Cell (列表項)

列表項

以前一直比較困惑如何實現列表項之間的, 左邊有些空缺的邊框. border屬性又不支持只顯示一條邊上的一部分, 難道要插入<hr>?web

WeUI的實現方式是: 利用.weui_cells:before.正則表達式

.weui_cell:before {
    content: " ";
    position: absolute;
    left: 15px;
    top: 0;
    width: 100%;
    height: 1px;
    border-top: 1px solid #D9D9D9;
    color: #D9D9D9;
    -webkit-transform-origin: 0 0;
    transform-origin: 0 0;
    -webkit-transform: scaleY(0.5);
    transform: scaleY(0.5);
}

left: 15px(左邊的空缺)配合上.weui_cells_titleoverflow: hidden(隱藏右邊超出的部分)就能夠顯示有空缺的邊框了.微信

列表項末尾的右箭頭的實現方式居然是weui_cell_ft::after的旋轉了45度的border. 我本覺得會用iconfont.

.weui_cells_access .weui_cell_ft:after {
    content: " ";
    display: inline-block;
    -webkit-transform: rotate(45deg);
    transform: rotate(45deg);
    height: 6px;
    width: 6px;
    border-width: 2px 2px 0 0;
    border-color: #C8C8CD;
    border-style: solid;
    position: relative;
    top: -2px;
    top: -1px;
    margin-left: .3em;
}

Radio (單選列表項)

單選列表項

在每一個行內部嵌入了一個隱藏的

<input type="radio" class="weui_check" name="radio1">

隱藏的方式是:

.weui_check {
    position: absolute;
    left: -9999em;
}

在每一個input.weui_check的後面放置了一個用於顯示對勾的span. input.weui_check.weui_icon_checked是兄弟關係.

<span class="weui_icon_checked"></span>
.weui_cells_radio .weui_check:checked + .weui_icon_checked:before {
    display: block;
    content: '\EA08';
    color: #09BB07;
    font-size: 16px;
}

Checkbox (複選列表項)

複選列表項

複選框如上面的單選框同樣被隱藏了.

<input type="checkbox" class="weui_check" name="checkbox1">

比較出乎我意料的是選中和未被選中的效果都是用iconfont實現的. 本覺得未被選中的效果是用border實現, 選中效果用一個check的iconfont配合水平豎直居中定位.

/* 選中效果 */
.weui_cells_checkbox .weui_check:checked + .weui_icon_checked:before {
    content: '\EA06';
    color: #09BB07;
}

/* 未選中效果 */
.weui_cells_checkbox .weui_icon_checked:before {
    content: '\EA01';
    color: #C9C9C9;
    font-size: 23px;
    display: block;
}

Switch (開關)

開關

<input class="weui_switch" type="checkbox">

以前以爲這個效果很難作啊, 看了weui的實現居然只用css就好了!

.weui_switch {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  position: relative;
  width: 52px;
  height: 32px;
  border: 1px solid #DFDFDF;
  outline: 0;
  border-radius: 16px;
  box-sizing: border-box;
  background: #DFDFDF;
}

.weui_switch:checked {
  border-color: #04BE02;
  background-color: #04BE02;
}

.weui_switch:before {
  content: " ";
  position: absolute;
  top: 0;
  left: 0;
  width: 50px;
  height: 30px;
  border-radius: 15px;
  border-top-left-radius: 15px;
  border-top-right-radius: 15px;
  border-bottom-right-radius: 15px;
  border-bottom-left-radius: 15px;
  background-color: #FDFDFD;
  -webkit-transition: -webkit-transform .3s;
  transition: -webkit-transform .3s;
  transition: transform .3s;
  transition: transform .3s, -webkit-transform .3s;
}

.weui_switch:checked:before {
  -webkit-transform: scale(0);
  transform: scale(0);
}

.weui_switch:after {
  content: " ";
  position: absolute;
  top: 0;
  left: 0;
  width: 30px;
  height: 30px;
  border-radius: 15px;
  background-color: #FFFFFF;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
  -webkit-transition: -webkit-transform .3s;
  transition: -webkit-transform .3s;
  transition: transform .3s;
  transition: transform .3s, -webkit-transform .3s;
}

.weui_switch:checked:after {
  -webkit-transform: translateX(20px);
  transform: translateX(20px);
}

其中, .weui_switch提供了邊框, 未選中時背景色#DFDFDF(深灰), 選中時#04BE02(綠色).

.weui_switch:before提供了邊框內部的淺灰色#FDFDFD. 被選中時scale(0)縮小消失.

.weui_switch:after提供了圓形按鍵. 被選中時向右邊移動20px.

效果以下:

Form (表單)

表單

<input class="weui_input" type="number" pattern="[0-9]*" placeholder="請輸入qq號">

inputpattern="[0-9]*限制了輸入只能是0-9的數字(pattern的值是正則表達式).

input[type="number"]在Chrome上默認會在最右邊顯示上下箭頭. WeUI經過下面的代碼禁用了箭頭, 這段代碼在Chrome的Dev Tool裏面是看不到的, 只能從CSS裏面看, 害我找了半天.

.weui_input::-webkit-outer-spin-button,
.weui_input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

相關: 如何隱藏數字輸入框的上下箭頭?

點選input[type="number"]在iOS上會自動打開數字鍵盤.

Upload (上傳)

上傳

WeUI用下面這個簡單的方法實現了圖片前面的灰層. absolute定位加上top:0; right:0; bottom:0; left:0;就會讓元素被抻開到父元素的邊界.

.weui_uploader_status:before {
    content: " ";
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background-color: rgba(0, 0, 0, 0.5);
}

圖片上傳狀態用了一個經典的(水平+垂直)居中方式, 利用top: 50%(讓元素的上邊界定位到父元素的50%位置)和transform: translateY(-50%)(讓元素往上移動元素自身高度的50%).

.weui_uploader_status .weui_uploader_status_content {
    position: absolute;
    top: 50%;
    left: 50%;
    -webkit-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
    color: #FFFFFF;
}

我平時經常使用的垂直居中方式以下. 水平居中相似.

.vertical-center {
  position: relative;
  top: 50%;
  transform: translateY(-50%);
}

最後的上傳按鈕:

<div class="weui_uploader_input_wrp">
    <input class="weui_uploader_input" type="file" accept="image/jpg,image/jpeg,image/png,image/gif" multiple="">
</div>

input[type="file"]在iOS上會自動觸發選擇"拍照"仍是"照片"的菜單.

方框是用.weui_uploader_input_wrp畫出來的, 而加號是用.weui_uploader_input_wrp:before:after.

真正的input利用opacity:0隱藏起來了.

.weui_uploader_input_wrp:before {
    width: 2px;
    height: 39.5px;
}
.weui_uploader_input_wrp:after {
    width: 39.5px;
    height: 2px;
}
.weui_uploader_input_wrp:before,
.weui_uploader_input_wrp:after {
    content: " ";
    position: absolute;
    top: 50%;
    left: 50%;
    -webkit-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
    background-color: #D9D9D9;
}
.weui_uploader_input {
    position: absolute;
    z-index: 1;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}

Form Error (表單報錯)

表單報錯

<input class="weui_input" type="date" value="">
<input class="weui_input" type="datetime-local" value="" placeholder="">

在iOS上, 點選input[type="date"]會出現"年-月-日"的選擇框, 點選input[type="datetime-local"]會出現"月-日-上午/下午-時-分"的選擇框.

Select (選擇)

選擇

電話號碼+86位置的右箭頭和分割線是用:before:after繪製的.

Toast

Toast

<div id="toast" style="display:none">
    <div class="weui_mask_transparent"></div>
    <div class="weui_toast">
        <i class="weui_icon_toast"></i>
        <p class="weui_toast_content">已完成</p>
    </div>
</div>

.weui_mask_transparent就是一個position:fixed佔滿全屏的透明幕布, 讓用戶沒法操做界面.

.weui_toast纔是頁面中間的黑塊.

Toast Loading

居然是純用HTML+CSS(animation+transition)實現的.

<div id="loadingToast" class="weui_loading_toast" style="/* display: none; */">
    <div class="weui_mask_transparent"></div>
    <div class="weui_toast">
        <div class="weui_loading">
            <div class="weui_loading_leaf weui_loading_leaf_0"></div>
            <div class="weui_loading_leaf weui_loading_leaf_1"></div>
            <div class="weui_loading_leaf weui_loading_leaf_2"></div>
            <div class="weui_loading_leaf weui_loading_leaf_3"></div>
            <div class="weui_loading_leaf weui_loading_leaf_4"></div>
            <div class="weui_loading_leaf weui_loading_leaf_5"></div>
            <div class="weui_loading_leaf weui_loading_leaf_6"></div>
            <div class="weui_loading_leaf weui_loading_leaf_7"></div>
            <div class="weui_loading_leaf weui_loading_leaf_8"></div>
            <div class="weui_loading_leaf weui_loading_leaf_9"></div>
            <div class="weui_loading_leaf weui_loading_leaf_10"></div>
            <div class="weui_loading_leaf weui_loading_leaf_11"></div>
        </div>
        <p class="weui_toast_content">數據加載中</p>
    </div>
</div>
.weui_loading_leaf {
  position: absolute;
  top: -1px;
  opacity: 0.25;
}
.weui_loading_leaf:before {
  content: " ";
  position: absolute;
  width: 8.14px;
  height: 3.08px;
  background: #d1d1d5;
  box-shadow: rgba(0, 0, 0, 0.0980392) 0px 0px 1px;
  border-radius: 1px;
  -webkit-transform-origin: left 50% 0px;
          transform-origin: left 50% 0px;
}
.weui_loading_leaf_0 {
  -webkit-animation: opacity-60-25-0-12 1.25s linear infinite;
          animation: opacity-60-25-0-12 1.25s linear infinite;
}
.weui_loading_leaf_0:before {
  -webkit-transform: rotate(0deg) translate(7.92px, 0px);
          transform: rotate(0deg) translate(7.92px, 0px);
}
/* ... */
.weui_loading_leaf_11 {
  -webkit-animation: opacity-60-25-11-12 1.25s linear infinite;
          animation: opacity-60-25-11-12 1.25s linear infinite;
}
.weui_loading_leaf_11:before {
  -webkit-transform: rotate(330deg) translate(7.92px, 0px);
          transform: rotate(330deg) translate(7.92px, 0px);
}
@-webkit-keyframes opacity-60-25-0-12 {
  0% {
    opacity: 0.25;
  }
  0.01% {
    opacity: 0.25;
  }
  0.02% {
    opacity: 1;
  }
  60.01% {
    opacity: 0.25;
  }
  100% {
    opacity: 0.25;
  }
}
/* ... */
@-webkit-keyframes opacity-60-25-11-12 {
  0% {
    opacity: 0.895958333333333;
  }
  91.6767% {
    opacity: 0.25;
  }
  91.6867% {
    opacity: 1;
  }
  51.6767% {
    opacity: 0.25;
  }
  100% {
    opacity: 0.895958333333333;
  }
}

4. Dialog

Dialog

<div class="weui_dialog_confirm" id="dialog1">
    <div class="weui_mask"></div>
    <div class="weui_dialog">
        <div class="weui_dialog_hd"><strong class="weui_dialog_title">彈窗標題</strong></div>
        <div class="weui_dialog_bd">自定義彈窗內容,居左對齊顯示,告知須要確認的信息等</div>
        <div class="weui_dialog_ft">
            <a href="javascript:;" class="weui_btn_dialog default">取消</a>
            <a href="javascript:;" class="weui_btn_dialog primary">肯定</a>
        </div>
    </div>
</div>

你能看到的邊框都是用:after實現的.

5. Progress

Dialog

略. ( *・ω・)✄╰ひ╯

6. Msg

Msg

略. ( *・ω・)✄╰ひ╯

7. Article

Article

略. ( *・ω・)✄╰ひ╯

8. ActionSheet

ActionSheet

<div id="actionSheet_wrap">
    <div class="weui_mask_transition" id="mask" style="display: none;"></div>
    <div class="weui_actionsheet" id="weui_actionsheet">
        <div class="weui_actionsheet_menu">
            <div class="weui_actionsheet_cell">示例菜單</div>
            <div class="weui_actionsheet_cell">示例菜單</div>
            <div class="weui_actionsheet_cell">示例菜單</div>
            <div class="weui_actionsheet_cell">示例菜單</div>
        </div>
        <div class="weui_actionsheet_action">
            <div class="weui_actionsheet_cell" id="actionsheet_cancel">取消</div>
        </div>
    </div>
</div>

值得一提的是, 頁面下方的ActionSheet始終是顯示的, 只不過平時經過transform: translateY(100%)隱藏了起來, 顯示時用translateY(0). 這方法無需JS就能夠自適應任意高度的ActionSheet.

.weui_actionsheet {
    position: fixed;
    left: 0;
    bottom: 0;
    -webkit-transform: translate(0, 100%);
    transform: translate(0, 100%);
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
    z-index: 2;
    width: 100%;
    background-color: #EFEFF4;
    -webkit-transition: -webkit-transform .3s;
    transition: -webkit-transform .3s;
    transition: transform .3s;
    transition: transform .3s, -webkit-transform .3s;
}
.weui_actionsheet_toggle {
    -webkit-transform: translate(0, 0);
    transform: translate(0, 0);
}

9. Icons

Icons

一堆iconfont. ( *・ω・)✄╰ひ╯

10. Panel

Panel

略. ( *・ω・)✄╰ひ╯

11. Tab

Navbar:

NavBar

TabBar:

TabBar

略. ( *・ω・)✄╰ひ╯

12. SearchBar

無焦點狀態:

SearchBar

有焦點狀態:

SearchBar with Candidates

<div class="weui_search_bar weui_search_focusing" id="search_bar">
    <form class="weui_search_outer">
        <!-- 搜索框有焦點時的搜索圖標, 搜索框和清空按鈕 -->
        <div class="weui_search_inner">
            <i class="weui_icon_search"></i>
            <input type="search" class="weui_search_input" id="search_input" placeholder="搜索" required="">
            <a href="javascript:" class="weui_icon_clear" id="search_clear"></a>
        </div>
        <!-- 搜索框沒有焦點時的顯示 -->
        <label for="search_input" class="weui_search_text" id="search_text">
            <i class="weui_icon_search"></i>
            <span>搜索</span>
        </label>
    </form>
    <!-- 搜索框有焦點時的取消鍵 -->
    <a href="javascript:" class="weui_search_cancel" id="search_cancel">取消</a>
</div>

這裏我最好奇的是, 當用戶點擊搜索框時, 彈出的鍵盤上右下角的按鍵是"搜索"而不是"換行".

我測試的效果是, 在微信中點擊搜索框時鍵盤顯示"搜索"按鍵, 在Safari中打開時則顯示"換行".

這就很詭異了, 說明微信作了什麼手腳. 難道與JS有關?

可是我在網上搜索了下, 發現只要確保input[type="search"]form包圍, 且formaction屬性便可. 示例:

<form action="">
  <input type="search" name="search" placeholder="search">
</form>

可是WeUI的實現中, form並無action屬性, 因此暫時不知道WeUI是如何作的.

相關: 如何讓搜索框的鍵盤顯示搜索按鍵?

參考

H5項目常見問題彙總及解決方案
html5手機常見問題與工具分享
A very simple router for the demo of WeUI

相關文章
相關標籤/搜索