小程序开发总结04 – 特殊样式处理
Write By CS逍遥剑仙
我的主页: csxiaoyao.com
GitHub: github.com/csxiaoyaojianxian
Email: sunjianfeng@csxiaoyao.com
QQ: 1724338257
1. 消除btn阴影
微信小程序中的button组件有特定的css,背景可用background:none;
去掉,但边框直接用border:none;
无法去除
解决方法:
使用 button::after{ border: none; }
button::after{ border: none; }
2. 超出省略号处理
/* 单行省略号 */ .text { overflow:hidden; text-overflow:ellipsis; white-space:nowrap; } /* 多行省略号 */ .textarea { display: -webkit-box; word-break: break-all; text-overflow: ellipsis; overflow: hidden; -webkit-box-orient: vertical; -webkit-line-clamp:2; }
3. 特殊布局 – 固定高度内容容器适配
实现部分高度固定,scrollview高度自适应,且能够下拉刷新上拉加载
<view> ... 固定高度200rpx <scroll-view style="height:{{srollHeight}}px;" scroll-y="true" scroll-with-animation="true" enable-back-to-top="true" scroll-top="{{scrollTop}}" bindscrolltolower="bindDownLoad" bindscroll="scroll" bindscrolltoupper="refresh" lower-threshold="200" > items... </scroll-view> <loading hidden="{{hideLoding}}"> 加载中... </loading> </view>
在js中处理高度
Page({ data: { scrollTop: 0, srollHeight: 0, hideLoding: true, }, onLoad(){ wx.getSystemInfo({ success: res => { // 小程序宽度固定为750rpx,可以计算出固定部分的高度200rpx对应的像素值 this.setData({ srollHeight: res.windowHeight - res.windowWidth / 750 * 200 }); } }) }, getList() { ... }, bindDownLoad: function () { this.getList(); }, scroll: function (event) { // this.setData({ // scrollTop: event.detail.scrollTop // }); }, refresh: function (event) { console.log("refresh"); } })
4. 模态框
小程序本身没有模态框组件,需要自己实现
<view class="modal" hidden="{{!showModal}}"> <view class="modal-content"> ...content </view> <view class="modal-close" bindtap="hideModal">x</view> </view>
wxss样式
.modal { position:fixed; width:100%; height:100%; top:0; left: 0; background:rgba(0,0,0,0.4); overflow: hidden; z-index: 999; } .modal .modal-content{ position:relative; width: 90%; margin:35% auto; overflow: hidden; background-color: #fff; height: 550rpx; overflow-y: auto; } /* 圆形关闭按钮 */ .modal .modal-close { width: 100rpx; height:100rpx; font-size:70rpx; color:#bababa; transform:translate(-50%,-50%); bottom:90rpx; position:absolute; left:50%; text-align:center; border:3rpx solid #bababa; border-radius:100%; vertical-align:middle; line-height:90rpx; }