WeUI是微信Web服務(wù)開發(fā)的UI套件, 目前包含12個(gè)模塊 (Button, Cell, Toast, Dialog, Progress, Msg, Article, ActionSheet, Icons, Panel, Tab, SearchBar).
Demo頁面: https://weui.io
Github頁面: https://github.com/weui/weui
下面講一講我從WeUI中學(xué)到的CSS技巧.
從這里我開始注意到, WeUI的實(shí)現(xiàn)中, 很多邊框都是用: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屏(視網(wǎng)膜屏)上確保1px真的是1pixel. 詳見Retina屏的移動(dòng)設(shè)備如何實(shí)現(xiàn)真正1px的線?
.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;}
看到這里發(fā)現(xiàn)WeUI大量使用了flex布局方式. 對這個(gè)布局方式我整理了另一篇文章, 見這里.
之前一直比較困惑如何實(shí)現(xiàn)列表項(xiàng)之間的, 左邊有些空缺的邊框. border屬性又不支持只顯示一條邊上的一部分, 難道要插入<hr>
WeUI的實(shí)現(xiàn)方式是: 利用.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_title
的overflow: hidden
(隱藏右邊超出的部分)就可以顯示有空缺的邊框了.
列表項(xiàng)末尾的右箭頭的實(shí)現(xiàn)方式竟然是weui_cell_ft::after
的旋轉(zhuǎn)了45度的border
. 我本以為會(huì)用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;}
在每個(gè)行內(nèi)部嵌入了一個(gè)隱藏的
<input type="radio" class="weui_check" name="radio1">
隱藏的方式是:
.weui_check { position: absolute; left: -9999em;}
在每個(gè)input.weui_check
的后面放置了一個(gè)用于顯示對勾的span
. input.weui_check
和.weui_icon_checked
是兄弟關(guān)系.
<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;}
復(fù)選框如上面的單選框一樣被隱藏了.
<input type="checkbox" class="weui_check" name="checkbox1">
比較出乎我意料的是選中和未被選中的效果都是用iconfont實(shí)現(xiàn)的. 本以為未被選中的效果是用border
實(shí)現(xiàn), 選中效果用一個(gè)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;}
<input class="weui_switch" type="checkbox">
之前覺得這個(gè)效果很難做啊, 看了weui的實(shí)現(xiàn)竟然只用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
提供了邊框, 未選中時(shí)背景色#DFDFDF
(深灰), 選中時(shí)#04BE02
(綠色).
.weui_switch:before
提供了邊框內(nèi)部的淺灰色#FDFDFD
. 被選中時(shí)scale(0)
縮小消失.
.weui_switch:after
提供了圓形按鍵. 被選中時(shí)向右邊移動(dòng)20px
.
效果如下:
<input class="weui_input" type="number" pattern="[0-9]*" placeholder="請輸入qq號">
input
的pattern="[0-9]*
限制了輸入只能是0-9的數(shù)字(pattern
的值是正則表達(dá)式).
input[type="number"]
在Chrome上默認(rèn)會(huì)在最右邊顯示上下箭頭. WeUI通過下面的代碼禁用了箭頭, 這段代碼在Chrome的Dev Tool里面是看不到的, 只能從CSS里面看, 害我找了半天.
.weui_input::-webkit-outer-spin-button,.weui_input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0;}
相關(guān): 在iOS上會(huì)自動(dòng)打開數(shù)字鍵盤.
WeUI用下面這個(gè)簡單的方法實(shí)現(xiàn)了圖片前面的灰層. absolute
定位加上top:0; right:0; bottom:0; left:0;
就會(huì)讓元素被抻開到父元素的邊界.
.weui_uploader_status:before { content: " "; position: absolute; top: 0; right: 0; bottom: 0; left: 0; background-color: rgba(0, 0, 0, 0.5);}
圖片上傳狀態(tài)用了一個(gè)經(jīng)典的(水平+垂直)居中方式, 利用top: 50%
(讓元素的上邊界定位到父元素的50%位置)和transform: translateY(-50%)
(讓元素往上移動(dòng)元素自身高度的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;}
我平時(shí)常用的垂直居中方式如下. 水平居中類似.
.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上會(huì)自動(dòng)觸發(fā)選擇"拍照"還是"照片"的菜單.
方框是用.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);}
<input class="weui_input" type="date" value=""><input class="weui_input" type="datetime-local" value="" placeholder="">
在iOS上, 點(diǎn)選input[type="date"]
會(huì)出現(xiàn)"年-月-日"的選擇框, 點(diǎn)選input[type="datetime-local"]
會(huì)出現(xiàn)"月-日-上午/下午-時(shí)-分"的選擇框.
電話號碼+86位置的右箭頭和分割線是用:before
和:after
繪制的.
<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
就是一個(gè)position:fixed
占滿全屏的透明幕布, 讓用戶無法操作界面.
.weui_toast
才是頁面中間的黑塊.
竟然是純用HTML+CSS(animation+transition)實(shí)現(xiàn)的.
<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">數(shù)據(jù)加載中</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; }}
<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">彈窗標(biāo)題</strong></div> <div class="weui_dialog_bd">自定義彈窗內(nèi)容,居左對齊顯示,告知需要確認(rèn)的信息等</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
實(shí)現(xiàn)的.
略. ( *?ω?)?╰ひ╯
略. ( *?ω?)?╰ひ╯
略. ( *?ω?)?╰ひ╯
<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始終是顯示的, 只不過平時(shí)通過transform: translateY(100%)
隱藏了起來, 顯示時(shí)用translateY(0)
. 這方法無需JS就可以自適應(yīng)任意高度的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);}
一堆iconfont. ( *?ω?)?╰ひ╯
略. ( *?ω?)?╰ひ╯
Navbar:
TabBar:
略. ( *?ω?)?╰ひ╯
無焦點(diǎn)狀態(tài):
有焦點(diǎn)狀態(tài):
<div class="weui_search_bar weui_search_focusing" id="search_bar"> <form class="weui_search_outer"> <!-- 搜索框有焦點(diǎn)時(shí)的搜索圖標(biāo), 搜索框和清空按鈕 --> <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> <!-- 搜索框沒有焦點(diǎn)時(shí)的顯示 --> <label for="search_input" class="weui_search_text" id="search_text"> <i class="weui_icon_search"></i> <span>搜索</span> </label> </form> <!-- 搜索框有焦點(diǎn)時(shí)的取消鍵 --> <a href="javascript:" class="weui_search_cancel" id="search_cancel">取消</a></div>
這里我最好奇的是, 當(dāng)用戶點(diǎn)擊搜索框時(shí), 彈出的鍵盤上右下角的按鍵是"搜索"而不是"換行".
我測試的效果是, 在微信中點(diǎn)擊搜索框時(shí)鍵盤顯示"搜索"按鍵, 在Safari中打開時(shí)則顯示"換行".
這就很詭異了, 說明微信做了什么手腳. 難道與JS有關(guān)?
但是我在網(wǎng)上搜索了下, 發(fā)現(xiàn)只要確保input[type="search"]
被form
包圍, 且form
有action
屬性即可. 示例:
<form action=""> <input type="search" name="search" placeholder="search"></form>
但是WeUI的實(shí)現(xiàn)中, form
并沒有action
屬性, 所以暫時(shí)不知道WeUI是如何做的.
聯(lián)系客服