Skip to content

Instantly share code, notes, and snippets.

@1c7
Last active January 8, 2019 05:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1c7/626344b058c08fc82e96664b18145884 to your computer and use it in GitHub Desktop.
Save 1c7/626344b058c08fc82e96664b18145884 to your computer and use it in GitHub Desktop.
微信小程序代码
Page({
data: {
offsetY: 0,
},
onShow(){
this.get_image_offset();
},
get_image_offset(){
if(this.data.offsetY == 0){
var that = this;
var query = wx.createSelectorQuery();
query.select('.image_show').boundingClientRect(function (rect) {
if (rect != undefined && rect != null && rect.top != undefined){
var top = rect.top
console.log(top);
// 因页面滚动,有时候 top 可能是负数,比如 -164
// 从"标签添加页"进入"写文字页"再回来可能就有这样的问题
if (top > 0){
that.setData({
offsetY: top
})
}
}
}).exec();
}
},
// <image class='image_show' src="x" bind:tap='click_image' mode='widthFix'>
click_image(e) {
var p = {
x: e.detail.x,
y: e.detail.y,
show: true,
}
if(this.data.offsetY != 0){
p.y = p.y - this.data.offsetY;
}
this.setData(p)
},
})
@1c7
Copy link
Author

1c7 commented Jan 8, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment