首先,定位元素時,范圍越小越精准,對其他頁面的影響越小,
所以,這個事件我沒有用整個window,而是定位到了that.FormProp.mainTable這個元素,在設置樣式時,也只設置了這個范圍內的元素,
比如:$(">div.vAction>.vActionSelect", that.FormProp.mainTable),這樣寫查找的更快,更精確。
言歸正傳,加上以下代碼,就能避免當窗口縮放時,頁面出現錯亂的現象。這樣,盒子會隨着窗口寬度的改變而調整自己的寬度。
$(that.FormProp.mainTable).scroll(function () {
let tops = parseInt($(">div.vAction>div.vActionSelect", that.FormProp.mainTable).css('top'));//To position the top value of the box
tops = $(that.FormProp.mainTable).scrollTop() - 100;//Here, the top value of the box will change with the position of the scroll bar
$(">div.vAction>.vActionSelect", that.FormProp.mainTable).css('top', top + 'px')//Finally, assign the top value to the box
});