这篇文章给大家分享的是有关微信小程序开发图片拖拽的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
微信小程序开发图片拖拽实例详解
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
});
}
})
感谢各位的阅读!关于“微信小程序开发图片拖拽的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。