最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to hide scroll bars until they are needed then remove if not needed - Stack Overflow

programmeradmin7浏览0评论

I have an example here.

Basically i have a scroll bar that is always present, even if it is not currently needed. Problem: I want a scroll bar to appear if the height of my div exceeds 300 pix.

<fieldset id="typography">
<ul>
    <li><label for="Poll Question">Header</label></li>
    <li><input type="text" id="formheader" value="Header"></input></li>
     <div class="scroll">           
        <li><label>Answers</label></li>
        <li id="formanswer1" class="clonedInput"><input type="text" id="formanswer1" value="" /></li>
        <li id="formanswer2" class="clonedInput"><input type="text" id="formanswer2" value="" /></li>
        <li id="formanswer3" class="clonedInput"><input type="text" id="formanswer3" value="" /></li>
    </div>      
    <li><input type="button" id="btnAdd" value="Add Answer" />
    <input type="button" id="btnDel" value="Remove Answer" /></li>
</ul>

I would appriciate any help. i know i need to use JS but im not even sure where to begin.

Question 2: Is there a easy mand that i can use that when i push the add button it scrolls to the very bottom of the scroller?

I have an example here.

Basically i have a scroll bar that is always present, even if it is not currently needed. Problem: I want a scroll bar to appear if the height of my div exceeds 300 pix.

<fieldset id="typography">
<ul>
    <li><label for="Poll Question">Header</label></li>
    <li><input type="text" id="formheader" value="Header"></input></li>
     <div class="scroll">           
        <li><label>Answers</label></li>
        <li id="formanswer1" class="clonedInput"><input type="text" id="formanswer1" value="" /></li>
        <li id="formanswer2" class="clonedInput"><input type="text" id="formanswer2" value="" /></li>
        <li id="formanswer3" class="clonedInput"><input type="text" id="formanswer3" value="" /></li>
    </div>      
    <li><input type="button" id="btnAdd" value="Add Answer" />
    <input type="button" id="btnDel" value="Remove Answer" /></li>
</ul>

I would appriciate any help. i know i need to use JS but im not even sure where to begin.

Question 2: Is there a easy mand that i can use that when i push the add button it scrolls to the very bottom of the scroller?

Share Improve this question edited Feb 18, 2012 at 5:10 nnnnnn 150k30 gold badges209 silver badges247 bronze badges asked Feb 18, 2012 at 4:59 user1082764user1082764 2,0159 gold badges28 silver badges41 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 16

In your CSS just set the height rather than max-height and set overflow : auto, as per this update to your demo: http://jsfiddle/nnnnnn/83659/4/

div.scroll {
  background-color:#00FFFF;
  width:190px;
  height:90px;
  overflow:auto;
}

Question 2: if you call .focus() on your newly added input it should bring it into view and put the cursor into the field: http://jsfiddle/nnnnnn/83659/4/

Alternatively you can use the (non-jQuery) .scrollIntoView() function to scroll the element into view without giving it focus.

Just add a fixed height and overflow: auto:

.scroll {
    background-color: #00FFFF;
    width: 190px;
    height: 100px;
    overflow: auto;
}

Check here http://jsfiddle/83659/2/

发布评论

评论列表(0)

  1. 暂无评论