豎向滑動:
<scroll-view scroll-y="true" style="height: 200rpx;">
<view style="background: red; width: 200px; height: 100px; display: inline-block" ></view>
<view style="background: green; width: 200px; height: 100px; display: inline-block"></view>
<view style="background: blue; width: 200px; height: 100px; display: inline-block"></view>
<view style="background: yellow; width: 200px; height: 100px; display: inline-block"></view>
</scroll-view>
橫向滑動:
white-space
normal: 正常無變化(默認處理方式.文本自動處理換行.假如抵達容器邊界內容會轉到下一行)
pre: 保持HTML源代碼的空格與換行,等同與pre標簽
nowrap: 強制文本在一行,除非遇到br換行標簽
pre-wrap: 同pre屬性,但是遇到超出容器范圍的時候會自動換行
pre-line: 同pre屬性,但是遇到連續空格會被看作一個空格
inherit: 繼承
<scroll-view scroll-x="true" style=" white-space: nowrap; display: flex" >
<view style="background: red; width: 200px; height: 100px; display: inline-block" ></view>
<view style="background: green; width: 200px; height: 100px; display: inline-block"></view>
<view style="background: blue; width: 200px; height: 100px; display: inline-block"></view>
<view style="background: yellow; width: 200px; height: 100px; display: inline-block"></view>
</scroll-view>
左滑刪除
js
var initdata = function (that) {
var list = that.data.list
for (var i = 0; i < list.length; i++) {
list[i].txtStyle = ""
}
that.setData({ list: list })
}
Page({
data: {
delBtnWidth: 180,//刪除按鈕寬度單位(rpx)
list: [
{
txtStyle: "",
icon: "/images/qcm.png",
txt: "左滑刪除"
},
{
txtStyle: "",
icon: "/images/qcm.png",
txt: "左滑刪除"
},
{
txtStyle: "",
icon: "/images/qcm.png",
txt: "左滑刪除"
},
{
txtStyle: "",
icon: "/images/qcm.png",
txt: "左滑刪除"
},
]
},
onLoad: function (options) {
// 頁面初始化 options為頁面跳轉所帶來的參數
this.initEleWidth();
},
touchS: function (e) {
if (e.touches.length == 1) {
this.setData({
//設置觸摸起始點水平方向位置
startX: e.touches[0].clientX
});
}
},
touchM: function (e) {
var that = this
initdata(that)
if (e.touches.length == 1) {
//手指移動時水平方向位置
var moveX = e.touches[0].clientX;
//手指起始點位置與移動期間的差值
var disX = this.data.startX - moveX;
var delBtnWidth = this.data.delBtnWidth;
var txtStyle = "";
if (disX == 0 || disX < 0) {//如果移動距離小于等于0,文本層位置不變
txtStyle = "left:0px";
} else if (disX > 0) {//移動距離大于0,文本層left值等于手指移動距離
txtStyle = "left:-" + disX + "px";
if (disX >= delBtnWidth) {
//控制手指移動距離最大值為刪除按鈕的寬度
txtStyle = "left:-" + delBtnWidth + "px";
}
}
//獲取手指觸摸的是哪一項
var index = e.target.dataset.index;
var list = this.data.list;
list[index].txtStyle = txtStyle;
//更新列表的狀態
this.setData({
list: list
});
}
},
touchE: function (e) {
if (e.changedTouches.length == 1) {
//手指移動結束后水平位置
var endX = e.changedTouches[0].clientX;
//觸摸開始與結束,手指移動的距離
var disX = this.data.startX - endX;
var delBtnWidth = this.data.delBtnWidth;
//如果距離小于刪除按鈕的1/2,不顯示刪除按鈕
var txtStyle = disX > delBtnWidth / 2 ? "left:-" + delBtnWidth + "px" : "left:0px";
//獲取手指觸摸的是哪一項
var index = e.target.dataset.index;
var list = this.data.list;
list[index].txtStyle = txtStyle;
//更新列表的狀態
this.setData({
list: list
});
}
},
//獲取元素自適應后的實際寬度
getEleWidth: function (w) {
var real = 0;
try {
var res = wx.getSystemInfoSync().windowWidth;
var scale = (750 / 2) / (w / 2);//以寬度750px設計稿做寬度的自適應
// console.log(scale);
real = Math.floor(res / scale);
return real;
} catch (e) {
return false;
// Do something when catch error
}
},
initEleWidth: function () {
var delBtnWidth = this.getEleWidth(this.data.delBtnWidth);
this.setData({
delBtnWidth: delBtnWidth
});
},
//點擊刪除按鈕事件
delItem: function (e) {
//寫刪除事件
}
})
wxml文件代碼:
<view class="item-box">
<view class="items">
<view wx:for="{{list}}" wx:key="{{index}}" class="item">
<view bindtouchstart="touchS" bindtouchmove="touchM" bindtouchend="touchE" data-index="{{index}}" style="{{item.txtStyle}}" class="inner txt">
<image class="item-icon" mode="widthFix" src="{{item.icon}}"></image>{{index}}{{item.txt}}</view>
<view data-index="{{index}}" bindtap = "delItem" class="inner del">刪除</view>
</view>
</view>
</view>
wxss文件代碼:
view{
box-sizing: border-box;
}
.item-box{
width: 700rpx;
margin: 0 auto;
padding:40rpx 0;
}
.items{
width: 100%;
}
.item{
position: relative;
border-top: 2rpx solid #eee;
height: 120rpx;
line-height: 120rpx;
overflow: hidden;
}
.item:last-child{
border-bottom: 2rpx solid #eee;
}
.inner{
position: absolute;
top:0;
}
.inner.txt{
background-color: #fff;
width: 100%;
z-index: 5;
padding:0 10rpx;
transition: left 0.2s ease-in-out;
white-space:nowrap;
overflow:hidden;
text-overflow:ellipsis;
}
.inner.del{
background-color: #e64340;
width: 180rpx;text-align: center;
z-index: 4;
right: 0;
color: #fff
}
.item-icon{
width: 64rpx;
vertical-align: middle;
margin-right: 16rpx
}
.thumb{
width: 200px;
height: 200px;
-webkit-overflow-scrolling: touch;
overflow: scroll;
}
- 商城api接口
- 首頁數據獲取
- 分類接口
- 購物車接口
- 商品信息接口
- 搜索接口
- 訂單列表接口
- 店鋪接口
- 收藏接口
- 收貨地址接口
- 生成訂單接口
- 支付接口
- 會員中心接口
- 登錄注冊接口
- 關于我們
- 圖片上傳
- 分銷中心
- 分銷明細
- 代金券
- 平臺紅包列表
- 分銷申請列表
- 我的推廣
- 微信小程序
- 簡介
- 開發前準備
- 目錄結構介紹
- 發起請求
- 網絡請求提交表單
- 代碼及開發所遇到問題總結
- 導航跳轉時所遇到的問題
- 緩存數據與數據取得的問題
- 如何引入外部css
- 如何定義與使用全局變量
- 如何定義新的界面
- 微信小程序支付
- 小程序的手機驗證碼登錄
- 上傳,下載
- 提示框
- app.json配置
- 配置demo
- pages
- window
- tabBar
- networkTimeout
- debug
- page.json
- 緩存
- 特效
- 滑動方式
- 城市切換
- 五星好評
- Switch
- 上拉加載
- wxml 標簽
- 視圖容器
- 基礎內容
- 表單組件
- 導航
- 媒體組件
- 自定義提示框
- 小程序內訪問網頁
- 倒計時顯示
- 微信小程序,如何在返回前一個頁面時,執行前一個頁面的方法
- 在本地可以請求到數據,但手機上是請求不到的
- curl請求失敗
- 代碼同步
- 短信平臺更換