简体   繁体   English

scrollTop仅在首次点击时有效

[英]scrollTop only works on first click

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. 所以我有一个amcharts图形,当我单击图形中的一个条时,它旁边的div滚动到该条中的数据。 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. 我希望能够在单击时滚动到每个数据。

This is my code for the scrollTo method: 这是我的scrollTo方法的代码:

"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);
 }
}]

The info div is where the scrolling happens. info div是滚动发生的地方。 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. 为了明确起见,第一次单击可以使我进入“ buildFailureObject”,但是接下来的单击要么使我回到infodiv的顶部,要么有时是一个随机的地点。

I have looked at questions close to mine and preventDefault has not helped me nor has return false. 我看过接近我的问题,preventDefault并没有帮助我,也没有返回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年);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM