unicreateSelectorQueryselect#searchIdboundingClientRectfunctionrect consoleinforecttttttttrect; let searchBottom = rectbottom; thisscreenTop = parseFloatthisscreenHeight-parseFloatre
这个问题可能是由于上下文的改变导致的。在回调函数中,this指针可能不再指向期望的对象,从而导致无法获取到正确的值。
解决这个问题的方法是将当前的this对象存储到一个变量中,然后在回调函数中使用这个变量来访问正确的对象。
例如:
var that = this;
uni.createSelectorQuery().select('#searchId').boundingClientRect(function(rect){
console.info('rectttttttt',rect);
// let searchBottom = rect.bottom;
that.screenTop = parseFloat(that.screenHeight)-parseFloat(rect.bottom);
console.log('组件在屏幕上的位置:', that.screenTop);
}).exec();
这样就能正确获取到组件在屏幕上的位置了。
原文地址: http://www.cveoy.top/t/topic/iG2G 著作权归作者所有。请勿转载和采集!