scrollTop仅在首次点击时有效 [英] scrollTop only works on first click

查看:101
本文介绍了scrollTop仅在首次点击时有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个amcharts图形,当我单击图形中的条形图时,它旁边的div滚动到该条形图中的数据.我的问题是,第一次点击后,我随时都点击它,有时甚至是一个随机位置.我希望能够在单击时滚动到每个数据.

So I have an amcharts graph, and when I click on a bar in the graph, a div next to it scrolls to that data from the bar. My problem is when I click anytime after the first time it goes to the top or sometimes a random position. I want to be able to scroll to each piece of data when clicked.

这是我的scrollTo方法的代码:

This is my code for the scrollTo method:

"listeners": [{
  "event": "clickGraphItem",
  "method": function (event) {
    var buildNumber = event.item.category;
    var id = '#buildNumber' + buildNumber;
    var buildFailureObject = $('#buildNumber' + buildNumber);
    $('#infodiv').animate({ scrollTop: $(buildFailureObject).position().top - 20 }, 2000);
 }
}]

信息div是滚动发生的地方.为了明确起见,第一次单击可以使我进入"buildFailureObject",但是接下来的单击要么使我回到infodiv的顶部,要么有时是一个随机的靠近地点的地方.

The info div is where the scrolling happens. To clarify, the first click works and takes me to the "buildFailureObject", but the next clicks either take me back to the top of the infodiv or sometime a random close by spot.

我已经看过接近我的问题,并且preventDefault并没有帮助我,也没有返回false.

I have looked at questions close to mine and preventDefault has not helped me nor has return false.

感谢您提供的任何帮助.

Thank you for any help you can provide.

推荐答案

好,所以我想出了答案,我需要这样做:

Ok so I figured out the answer, I needed this:

$('#infodiv').animate({scrollTop:($('#infodiv').scrollTop()+ $(buildFailureObject).position().top -20)},2000);

$('#infodiv').animate({scrollTop:($('#infodiv').scrollTop() + $(buildFailureObject).position().top -20)}, 2000);

这篇关于scrollTop仅在首次点击时有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆