temporary_user_name
temporary_user_name

Reputation: 37068

How to make a scrollbar appear?

Here's what I'm trying to make. I want a div which appears as a box of fixed dimensions, into which various javascript functions will be placing text over time. When the number of lines of text exceeds the height of the box, I'd like a scrollbar to appear alongside the box, so that you can scroll through the text inside it while the box itself remains a fixed height.

How could this be accomplished?

Upvotes: 3

Views: 720

Answers (5)

Vinoth Kumar
Vinoth Kumar

Reputation: 93

<style type="text/css">
.holeEvent {
   height: 50px;
   overflow-y: hidden;
}
.holeEvent2 {
   height: 50px;
   overflow-y: scroll;
}
</style>

<div class="holeEvent2"></div>

Upvotes: 0

Vinoth Kumar
Vinoth Kumar

Reputation: 93

<script type="text/javascript">
$(function() { 
   $('.holeEvent').hover(function() {
      $(this).removeClass('holeEvent').addClass('holeEvent2');
   },
   function(){
      $(this).removeClass('holeEvent2').addClass('holeEvent');
   });
});
</script>

Upvotes: 0

Derek 朕會功夫
Derek 朕會功夫

Reputation: 94319

Use overflow in CSS:

document.querySelector("div").style.overflowY = "scroll";  //For Y
document.querySelector("div").style.overflowX = "scroll";  //For X

document.querySelector("div").style.overflow = "scroll";   //For both ;)

P.S.

If you want it to show the scroll bar only when necessary, you can do this instead:

document.querySelector("div").style.overflow = "auto";    //JavaScript

//-or-

div{
    overflow: auto;                       /*CSS*/
}

Upvotes: 3

The System Restart
The System Restart

Reputation: 2881

using css you can do this:

div{
   overflow-y: scroll
}

you can also overflow-y: auto as @tw16

Upvotes: 6

tw16
tw16

Reputation: 29575

As you say the scrollbar should only appear when there is too much text, I would use:

div{
    overflow-y: auto
}

Live example: http://jsfiddle.net/RMpFu/

Upvotes: 4

Related Questions