Component({ /** * 组件样式隔离 */ options: { addGlobalClass: true, multipleSlots: true // 支持多个slot }, properties: { value: { type: String, value: null }, placeholder: { type: String, value: '' } }, /** * 组件的初始数据 */ data: { editing: false, scroll_top: 0, top: 0, }, /** * 组件的方法列表 */ methods: { onEdit: function(e) { this.setData({ editing: true }) if (this.data.scroll_top > 0 || Number(e.detail.height) <= 0) { return; } if(this.data.top > 0){ that.triggerEvent('focus', that.data.top) return } const res = wx.getSystemInfoSync(); let windowHeight = res.windowHeight; let keybordHeight = e.detail.height; const query = wx.createSelectorQuery(); query.select('#' + e.currentTarget.id).boundingClientRect(); query.selectViewport().scrollOffset(); var that = this; query.exec(function(res) { if (!res[0]) { that.data.scroll_top = 0 that.triggerEvent('focus', 0) return; } let bottom = res[0].bottom; if ((windowHeight - keybordHeight) < (bottom + 20)) { that.data.scroll_top = bottom + 80 - windowHeight - keybordHeight that.data.top = that.data.scroll_top that.triggerEvent('focus', that.data.scroll_top) } else { that.triggerEvent('focus', 0) that.data.scroll_top = 0 } }) }, onBlur: function(e) { this.setData({ editing: false }) this.data.scroll_top = 0 this.triggerEvent('focus', 0) }, bindInput: function(e) { this.setData({ value: e.detail.value }) this.triggerEvent('change', this.data.value) } } })