微信小程序开发图片拖拽实例详解
来源:爱站网时间:2020-03-16编辑:网友分享
在软件开发过程中,我们经常会遇到拖拉和监听,微信app小程序可以实现拖拽的效果,那么微信小程序开发图片拖拽实例详解,大家了解吗?爱站技术频道小编这就为大家进行介绍。
在软件开发过程中,我们经常会遇到拖拉和监听,微信app小程序可以实现拖拽的效果,那么微信小程序开发图片拖拽实例详解,大家了解吗?爱站技术频道小编这就为大家进行介绍。
微信小程序开发图片拖拽实例详解

1.编写页面结构:moveimg.wxml
<view class="container">
<view class="cnt">
<image class="image-style" src="../uploads/foods.jpg" style="left:{{ballleft}}px;width:{{screenWidth}}px" bindtouchmove="ballMoveEvent">
</image>
</view>
</view>
2.编写页面样式:moveimg.wxss
.container {
box-sizing:border-box;
padding:1rem;
}
.cnt{
width:100%;
height:15rem;
border: 1px solid #ccc;
position:relative;
overflow: hidden;
}
.image-style{
position: absolute;
top: 0px;
left:0px;
height:100%;
}
3.设置数据:moveimg.js
var app = getApp()
Page({
data: {
ballleft:-20,
screenWidth: 0,
},
onLoad: function() {
var _this = this;
wx.getSystemInfo({
success: function(res) {
_this.setData({
screenHeight: res.windowHeight,
screenWidth: res.windowWidth,
});
}
});
},
ballMoveEvent: function(e) {
var touchs = e.touches[0];
var pageX = touchs.pageX;
console.log('宽度 '+this.data.screenWidth)
console.log('pageX: ' + pageX);
//这里用right和bottom.所以需要将pageX pageY转换
var x = this.data.screenWidth/2 - pageX-20;
if(this.data.screenWidth>385){
if(x>42){x=42;}
}else{
if(x>32){x=32;}
}
if(x<0){x=0;}
console.log('x:' + x)
this.setData({
ballleft: -x
});
}
})
上述就是爱站技术频道小编为大家介绍的微信小程序开发图片拖拽实例详解,如果大家想进一步了解其他专业知识,不妨进入js.aizhan.com进行搜索。
