WeUI中的Css详解
WeUI是Web服务开发的UI套件, ⽬前包含12个模块 (Button, Cell, Toast, Dialog, Progress, Msg, Article, ActionSheet, Icons,
Panel, Tab, SearchBar).
下⾯讲⼀讲我从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)
这么做是为了在Retina屏(视⽹膜屏)上确保1px真的是1pixel. 详见
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-item
看到这⾥发现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-
left: 15px(左边的空缺)配合上.weui_cells_title的overflow: 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 Radio (单选列表项)
在每个⾏内部嵌⼊了⼀个隐藏的
<input type="radio" class="weui_check" name="radio1">
隐藏的⽅式是:
.weui_check {
div border属性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_c 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:
其中, .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号">
input的pattern="[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=""> </
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, 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
<div id="toast" > <div class="weui_mask_transparent"></div> <div class="weui_toast"> <i class="weui_icon_toast"></i> <p class="weui_toast_
.weui_mask_transparent就是⼀个position:fixed占满全屏的透明幕布, 让⽤户⽆法操作界⾯.
.weui_toast才是页⾯中间的⿊块.
竟然是纯⽤HTML+CSS(animation+transition)实现的.
<div id="loadingToast" class="weui_loading_toast" > <div class="weui_mask_transparent"></div> <div class="weui_toast"> <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 4. 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_dialo
你能看到的边框都是⽤:after实现的.
5. Progress
略. ( *·ω·)✄╰ひ╯
6. Msg

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。