微信小程序實現拖拽功能
更新時間:2019年09月26日 11:49:00 作者:前端_李嘉豪
這篇文章主要介紹了微信小程序實現拖拽功能,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下

<view class='collectBox'
bindtap='addCollect'
wx:if="{{write[0]+write[1] > 0}}"
bindtouchmove="touchmove"
catch:touchmove
style="left:{{write[0]}}px;top:{{write[1]}}px;">
<image src='../../images/icon/addcollect.png'></image>
</view>
// pages/cateDetaile/cateDetaile.js
const app = getApp()
Page({
/**
* 頁面的初始數據
*/
data: {
// 拖拽參數
writePosition: [80, 90], //默認定位參數
writesize: [0, 0],// X Y 定位
window: [0, 0], //屏幕尺寸
write: [0, 0], //定位參數
scrolltop: 0,//據頂部距離
},
/**
* 生命周期函數--監(jiān)聽頁面加載
*/
onLoad: function (options) {
// 在頁面中定義插屏廣告
let that = this;
that.getSysdata();
},
//計算默認定位值
getSysdata: function () {
var that = this;
wx.getSystemInfo({
success: function (e) {
that.data.window = [e.windowWidth, e.windowHeight];
var write = [];
write[0] = that.data.window[0] * that.data.writePosition[0] / 100;
write[1] = that.data.window[1] * that.data.writePosition[1] / 100;
console.log(write,45)
that.setData({
write: write
}, function () {
// 獲取元素寬高
wx.createSelectorQuery().select('.collectBox').boundingClientRect(function (res) {
console.log(res.width)
that.data.writesize = [res.width, res.height];
}).exec();
})
},
fail: function (e) {
console.log(e)
}
});
},
//開始拖拽
touchmove: function (e) {
var that = this;
var position = [e.touches[0].pageX - that.data.writesize[0] / 2, e.touches[0].pageY - that.data.writesize[1] / 2 - this.data.scrolltop];
that.setData({
write: position
});
},
onPageScroll(e) {
this.data.scrolltop = e.scrollTop;
},
})
總結
以上所述是小編給大家介紹的微信小程序實現拖拽功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!
相關文章
uniapp使用mui-player插件播放m3u8/flv視頻流示例代碼
在小程序里播放視頻是很常見的功能,下面這篇文章主要給大家介紹了關于uniapp使用mui-player插件播放m3u8/flv視頻流的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-06-06
javascript中關于&& 和 || 表達式的小技巧分享
我將會介紹和解析12個簡單但是強大的JavaScript技巧. 這些技巧所有的JavaScript程序員都可以馬上使用, 你不需要成為JavaScript高手才能理解這些.下面我們開始本系列的第一篇文章,介紹下強大的&& 和 || 表達式2015-04-04

