简体   繁体   English

jQuery Animate在可滚动div内的顶部

[英]jQuery Animate to top inside a scrollable div

I currently have a page on my website that has a scrollable div on the right side of the website. 我目前在我的网站上有一个页面,该页面在网站的右侧具有可滚动的div。 Inside this div there are a bunch of questions the user has to fill out within bootstrap tabs. 在这个div内部,用户必须在引导程序标签中填写一堆问题。 I'm trying to do some validation on these so they cannot move on until they answer them. 我正在尝试对它们进行一些验证,以使它们在回答它们之前无法继续前进。 Part of this validation is that it scrolls to the top of the div where the error message is and highlights the input field they missed. 此验证的一部分是,滚动到错误消息所在的div顶部,并突出显示他们错过的输入字段。

I'm having a bunch of issues with scrolling to the top of the div though. 我在滚动到div顶部时遇到了很多问题。 It's doing all sorts of wonky things. 它正在做各种奇怪的事情。

Originally, I had this 本来我有这个

$('.html, body').animate({
  scrollTop: ($('#scrollToMe').first().offset().top)
},500);

and this works if you are just shooting it to the top of the whole page, but in this case I am not. 如果您只是将其拍摄到整个页面的顶部,则此方法有效,但是在这种情况下,我不是。 I just want to make that scrollable div shoot to the top. 我只想将可滚动的div拍摄到顶部。

So then I tried calling the tab content 所以然后我尝试调用选项卡内容

$('.tab-content').animate({
  scrollTop: ($('#scrollToMe').first().offset().top)
},500);

but this just did a little hop in the div and then shot to the top of the page. 但这只是在div中跳了一下,然后跳至页面顶部。

So I tired yet again and tried calling out the id to see if that would work. 所以我又累了,尝试调出ID以查看是否可行。

 $('#scrollToMe').animate({
  scrollTop: ($('#scrollToMe').first().offset().top)
},500);

and I just got the little hop as before. 我只是像以前一样跳了一跳。

I set up a small jsFiddle to show what's going on http://jsfiddle.net/vkut3/1/ 我设置了一个小jsFiddle来显示http://jsfiddle.net/vkut3/1/

You need to change the scrollable element to be the div instead of the html,body. 您需要将可滚动元素更改为div而不是html,body。 Like this: 像这样:

$('.scrollable').animate({
    scrollTop: ($('.errorMess').first().offset().top)
},500);

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

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