首先我们建一个跟pages同级的一个文件夹components,customer就是我们要写的组件,其目录结构如下:
wxml代码如下:
wxss代码如下:
.customer {
position: fixed;
z-index: 1000;
width:200rpx;
height: 200rpx;
}
.btn {
width: 80rpx;
height: 80rpx;
border-radius: 40rpx;
padding: 0;
opacity: 0.8;
background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFEAAABRCAYAAACqj0o2AAAIpElEQVR4nN2dWYwVRRSGfxoQZV9kB8dRZBMUZAgKihAEA4IaCC5RUdyDQX3wBZ9cHjQxIdEEEncFxAcXNCKCAQFBUJE1IDCAgwuMbDJsIzojmDP8lzRN9VZ9unsyX9KBube7qvtMddWp/5yqqXes/9XIkSYAOgDoBKALgOYAWgK4CEBTAA1dt1YF4DiASgCHARwD8DuAch5/5/UYDXKo83IAVwDoRcM1jXjdhQCa+Xx3FMAeAFsAlAL4VfF+Q8nKiB0BlAAYwP9r05xHL5YrLXQdgB8BHEz74dJ+nXsDGAYgrz7jFIC1AJYD2JFWJWm1RDHaKADdUio/Kg6AgTy2AlgMYLN2JdpGlD7u1hxbXhC9ePwE4AsAf2oVrGnEcQDGKpaXFiU8PgWwSKMODSNK65sEoEihrCwZD6APgPcAHEpSb9KBZQiA+6ScvCyhwD8AZvE1tyJJS5wI4KbaYIWENALwCJ3++TZF2RrxcQD903qqnJA+vTVbZSwci/udWgcNWEC6p4fjXhTXiE+yM67LiE/5aFpGnALgyjpuwAID6HFEImqfODEjB7oawF90hOXf05y61aeiczGA9lR60mYI590LwuqJYsRrUx6FRdbaxLmtKDBHQs53OJKKGtSXb0da09fbqA5tDDopzE+U3/oL6rd2hl0AVtI/+zdBOW0ADAJwY0otVN6OaZTbjIQZ8bkUpKsDAOZRXdHkAgCjAdzM11+TMgAv+5UX9BqMS8GA33DOWhXxfPf9VYecK635c2qI9yorSMUAhgNYavrSryW2A/Ci4k3I4PB2yNRKlOvuAHqwG2nH8AE4wEg4YD+AvQC2A9gZYtgJlOM0eYb3cQ5+RnzapR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width: 80rpx;
height: 80rpx;
border-radius: 40rpx;
padding: 0;
opacity: 0.8;
background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFEAAABRCAYAAACqj0o2AAAGk0lEQVR4nOWda4hVVRTHf5608VGiY1qalmmZmmbTDCpJD5Epeyj0MkkKMjQQ+lIEFX3p8SH6mCIVZUQE4hBEmZUo9rS0ssxIsTIpJ/GROY5MaqaxhnXkzJ177j3n7L3P484Pzgedu1//u/fZe6+19r692humkCEeMBqoBy4EhgLnAv316QOcBnoB/wId+rQDB4BW4BCwGziVVTN6Z1DmSOBSYDIwTB9T9uuzDfgF2JNmg9IScQjQBFytPc82/pcxSfOVnvkNsAX4y3XjXA/nscANQCNwlsuCQvgP+Bb4GPjVVSGuRLwMuEmHbF6Qof4R8LPt+tgezoOBOcAMy/naYLI+XwDvAX/bytimiDOBuTqr5hn5ghuAd4ENNuppQ0TpffcCV1rIKy3ki54PTATeAg6blGsqosyGC4EBqctgB/nixwCvAz8mzdEzqMos4OECC+hzjrZjVtIMkop4FzAvaaE5ZZ62KzZJRJTh21xjAvo0a/tiEVfEB4FpTqqfH6ZpOyMTR0Tp6lNrWLwgU+MM7agizqzhIRxGs7a7KlFEHK9rqp7IfG1/RaqJKNP/kh4qoM+SaruwaiIuAuqsV6tYSPsXV6pxJRGvjdKVYyCG0j8KKuQE1aMsYdu+gcA9Fgr/B1gLfA/8qf83Rme+sRbyTxPRYytwpLTMsJ54t/o3TBDr8nPAmoCAwi7gBWBjwUTso7p0o5yIoyysB2XoPg8crPCZN9TqXCSmqj5dKCfinYaNOgm8qF66arwC/F4wIbvpUyriKH2JmvAh0BYj/TJ1hxaFCereDRVxtmFDxPf7Wcw0bTq0i8QtwboGRRyoXjkTWhNaib82MYpmQKNuRLqJOF0jDUw4ZJD2zYIIiOp0xpoVFNGGhcbk3XZYXZpF4Rq/nr6I55ebuhMw2DD9auBEQUQcqbqdEfEKSxlfYLjXPpFgYsqSzrAVX8SJlioyQL8hE76Mu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vcn2riYd5BuD4t0k6fPDzZu8rR5u3FR7pRFF8+5ulPWZ4OapOYGzwLnkI0qoLXbjV3dsz1O7XZ5u2dbFtA7bWfsKphopz5Z3/h+Uq/OL+SN76UM0eDMJocu0CC7NUR5Sxrxl1n8lEjN/QpGHn6PRfy/cr5QxJWAJllzygwvISVy9lnO2cjnjml8ucyssraTOG0RSyaI3zL7PRbgf1uHMiduQlWMAAAAAElFTkSuQmCC);
background-repeat: no-repeat;
background-size: cover;
}
.btn::after {
border: none;
}
js代码如下:
Component({
/**
* 组件的属性列表
*/
properties: {
},
/**
* 组件的初始数据
*/
data: {
top: 800,
left: 570,
},
/**
* 组件的方法列表
*/
methods: {
//组件实现拖动效果
setTouchMove: function (e) {
var that = this
if (e.touches[0].clientX < (wx.getSystemInfoSync().windowWidth - 80) && e.touches[0].clientX > 0 && e.touches[0].clientY < (wx.getSystemInfoSync().windowHeight - 120) && e.touches[0].clientY > 0) {
wx.getSystemInfo({
success: function (res) {
let X = (e.touches[0].clientX * (750 / res.windowWidth));
// 将高度乘以换算后的该设备的rpx与px的比例
let Y = (e.touches[0].clientY * (750 / res.windowWidth));
// 将高度乘以换算后的该设备的rpx与px的比例
that.setData({
left: X,
top: Y
})
}
})
}
},
// 拨打电话组件
phone() {
wx.makePhoneCall({
phoneNumber: this.properties.newPhone,
success: function () {
console.log("拨打电话成功!")
},
fail: function () {
console.log("拨打电话失败!")
}
})
}
},
lifetimes: {
attached() {
// 在组件实例进入页面节点树时执行
},
detached() {
// 在组件实例被从页面节点树移除时执行
},
created() {
}
}
})
至此,组件我们已经写完了,接下来要做的就是在页面引入我们所写的组件。
我们先在要引组件的页面json文件里添加一下组件路径:
{
"usingComponents": {
"customer": "/components/customer/customer"
}
}
然后在要引组件的页面wxml文件里加入这一行代码:
注意:这里的标签名要与json文件里定义的名字一致
到了这一步,我们已经大功告成了。页面效果如下所示:
组件可以实现自由拖动,涉及个人隐私,页面内容打了马赛克。