当我更改页面底部某个元素的内容时,页面会滚动到此元素的开头。例如:
$container.html($container.html());
这在FF中运行良好,没有任何页面滚动。以前有人解决过这样的问题吗?
UDP:
我从第一个答案开始尝试:
$container.css('height',$container.height()+'px');$container.html($container.html.());$container.css('height','auto');
但是3行会导致页面跳转,所以如果只设置容器高度,效果很好。但我的竞争高度可以不同,它是从服务器加载的。
现在我有了插入内容后查找高度的想法,并将其设置为"自动":但是父容器的Chrome返回高度(innerHeight,outerHeight):
var $c = this.options.container;
$c.css('height', $c.height()+'px');
$c.html(data.content || '');
var $child = $c.children();
console.log($child.height(), $child.innerHeight(), $child.outerHeight());
$c.css('height', $child.height()+'px');
FF中返回的高度正确。
如果我大胆猜测的话,那就是铬重新绘制得更快,容器在装满其他东西之前会在空的时候收缩。您可以尝试将高度固定为特定值,更改内容,然后再次将其设置为自动(如果这是默认设置)。类似的东西(未经测试):
$container.css('height',$container.height()+'px');
$container.html($container.html());
$container.css('height','auto');
我发现这种情况通常是因为我忘记在点击处理程序上返回false,w/jQuery就像这样:
$('a').click(function(){$container.html($container.html.());return false;});
尽管如此,您的$container.html($container.html.());我觉得很奇怪。
找到解决方案。
var $c = this.options.container;
//fix Chrome page jumping
$.browser.webkit && $c.css('height', $c.height()+'px');
$c.html(data.content || '');
//fix Chrome page jumping
if ($.browser.webkit) {
setTimeout(function(){
$c.css('height', $c.children().height()+'px');
}, 1);
};