最近在做一個(gè)聊天會(huì)話框,我可以得到數(shù)據(jù)列表,包括文本,圖片和聲音,最多20條。 我之前的方法是:首先,設(shè)置<img>的默認(rèn)src,height,width等屬性,以獲得父div的高度,因?yàn)闀?huì)話框一打開就必須滾動(dòng)到底部。當(dāng)我得到真正的圖片時(shí),改變src,height,width,現(xiàn)在用onload事件監(jiān)聽得到真實(shí)高度 realHeight,但是當(dāng)加載的消息記錄中有較多大圖片時(shí),頻繁地改變scrollTop,會(huì)引起明顯的窗口抖動(dòng)有人都有類似的經(jīng)驗(yàn)和解決方案? 謝謝,代碼如下 function ReplacePicture (imgid, url) { var $img = $('#img-'+imgid).children(); var oldH = parseInt($img.css('height'));
$img.attr('oldHeight', oldH);
$img.attr('src', url);
$img.on('load', function() { if(!currentPosElement){ //當(dāng)前窗口為空,滾動(dòng)到底部
sureToBottom();
} else { var oldH = $(this).attr('oldHeight'); var newH = $(this).css('height').match(/\d+/g);
if (newH == oldH) return;
oldH = parseInt(oldH);
newH = parseInt(newH); var changeH = newH - oldH;
$(document).scrollTop($(document).scrollTop() + changeH);
$(this).attr('oldHeight',newH);
}
});
}
聊天會(huì)話框,圖片加載如何保持滾動(dòng)條位置?
哆啦的時(shí)光機(jī)
2018-10-20 13:11:26