WeUI中的Css詳解


 

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

Demo頁面: https://weui.io

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

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

Button

從這里我開始注意到, WeUI的實現中, 很多邊框都是用:before:after繪制的.

.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的線?

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布局方式. 對這個布局方式我整理了另一篇文章, 見這里.

Cell (列表項)

列表項

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

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的后面放置了一個用於顯示對勾的spaninput.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; ">#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; ">#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; ">#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; ">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%); ">#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%; ">#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是如何做的.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM