最近在做一个聊天会话框,我可以得到数据列表,包括文本,图片和声音,最多20条。
我之前的方法是:
首先,设置<img>的默认src,height,width等属性,以获得父div的高度,因为会话框一打开就必须滚动到底部。当我得到真正的图片时,改变src,height,width,
现在用onload事件监听得到真实高度 realHeight,但是当加载的消息记录中有较多大图片时,频繁地改变scrollTop,会引起明显的窗口抖动
有人都有类似的经验和解决方案? 谢谢,代码如下
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){ //当前窗口为空,滚动到底部 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
相关分类