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);
精彩评论