开发者

Scroll down to Form DIV area?

开发者 https://www.devze.com 2023-04-03 18:42 出处:网络
Every time I add an item - I want to scroll down to Add Item Form form (#form_additem) - how can that be done?

Every time I add an item - I want to scroll down to Add Item Form form (#form_additem) - how can that be done?

Example of HTML Code:

<开发者_JAVA百科div class="item"> 1 </div>
<div class="item"> 2 </div>
<div class="item"> 3 </div>
<div class="item"> 4 </div>
<div id="form_additem"> 
  Add Item: <input type="text" value="" />
</div>

I have tried:

 $('html,body').animate({scrollTop: $("#form_additem").offset().top + "px"});

but it scrolling at the bottom of the page.

Thanks


You might be interested in the scrollTo plugin. Then, it's as easy as: http://jsfiddle.net/pimvdb/rVgna/.

$.scrollTo("#form_additem");


Your code is correct. And maybe scrolling to the very bottom of the page is OK, if #form_additem is the last element before </body>.

Also you can animate scrollTo() function, suggested by pimvdb:

$.scrollTo("#form_additem", 1000);
0

精彩评论

暂无评论...
验证码 换一张
取 消