开发者

div element with scroll at end

I have a div element that at the beginning is hidden. It appears when a but开发者_如何学运维ton is clicked. the problem is that this div has a scroll and when it appears, it is at the end of scroll. How can I fix this?

The code is something like this:

<div style="width: 412px; height: 351px; overflow: auto; position: relative;"> 
   <div style="width: 540px;">
      Here is a form
   </div>
</div>

The first div is that is hidden at the beginning.


On your button that displays the div, add this javascript to set the div to be scrolled to the top.

document.getElementById('id').scrollTop = 0;


Per your comment, if you want to set the scroll without modifying the button's code, you could try running it on page load:

<body onload="javascript: document.getElementById('id').scrollTop = 0;">

Or, you could run it at the bottom of the page:

<script type="text/javascript">
    document.getElementById('id').scrollTop = 0;
</script>


I would suggest showing your code, providing a link, or creating a test page for people to view and debug. It's difficult to see what's going on without some context.

Here's an example that, I believe, shows what you want: http://jsbin.com/emoji4/edit

This uses the jQuery library, so I'm not sure it will meet your needs.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜