Javascript 单击并滚动到顶部
我的offset.top部分有问题 也就是说,点击下一架战斗机后,方向盘压得太远Javascript 单击并滚动到顶部,javascript,jquery,scroll,Javascript,Jquery,Scroll,我的offset.top部分有问题 也就是说,点击下一架战斗机后,方向盘压得太远 $(".list-fighters li .toogle").click(function(e) { $(".list-fighters li").removeClass("open"); $(this).parent("li").toggleClass("open"); $('html, body').animate({scrollTop: $(this).offset().top}, 1
$(".list-fighters li .toogle").click(function(e) {
$(".list-fighters li").removeClass("open");
$(this).parent("li").toggleClass("open");
$('html, body').animate({scrollTop: $(this).offset().top}, 100);
});
有什么方法可以修复它吗?您需要在超时时间内调用
scrolltop()
,以便给DOM时间更新offset()。top
来自您以前的调用$(“.list”)。removeClass(“open”)代码>修改了页面高度,如下所示:
var deez = this;
setTimeout(function() {
$('html, body').animate({scrollTop: $(deez).offset().top}, 100);
}, 300);
测试后,更新到300毫秒以捕捉高度更新。这都是因为您同时有多个动画。有些是用JavaScript定义的,有些是用CSS定义的
CSS(main.CSS:233)
JavaScript
$('html, body').animate({scrollTop: $(this).offset().top}, 100);
正如您所看到的,您启动的动画将折叠打开的战斗机描述,并需要350ms
来更新其高度。您需要的是等待这段时间,以获得所有元素的适当高度
var that = this;
setTimeout(function() {
$('html, body').animate({scrollTop: $(that).offset().top}, 100);
}, 350);
你能为我们生成一个JSFIDLE示例吗?你指的是页面上的哪个部分要复制:wheel
:转到“fighters”部分,在团队1中有三行人脸图像,单击第一行的a图像,它滚动ok,现在单击第二行的a图像,现在它不能很好地滚动。但是我必须使用这个变量来输入函数。此外,这也没关系:)
var that = this;
setTimeout(function() {
$('html, body').animate({scrollTop: $(that).offset().top}, 100);
}, 350);