admin管理员组文章数量:1336631
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 acplished?
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 acplished?
Share Improve this question asked May 13, 2012 at 19:18 temporary_user_nametemporary_user_name 37.1k48 gold badges160 silver badges243 bronze badges5 Answers
Reset to default 6using css you can do this:
div{
overflow-y: scroll
}
you can also overflow-y: auto
as @tw16
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/RMpFu/
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*/
}
<script type="text/javascript">
$(function() {
$('.holeEvent').hover(function() {
$(this).removeClass('holeEvent').addClass('holeEvent2');
},
function(){
$(this).removeClass('holeEvent2').addClass('holeEvent');
});
});
</script>
<style type="text/css">
.holeEvent {
height: 50px;
overflow-y: hidden;
}
.holeEvent2 {
height: 50px;
overflow-y: scroll;
}
</style>
<div class="holeEvent2"></div>
本文标签: javascriptHow to make a scrollbar appearStack Overflow
版权声明:本文标题:javascript - How to make a scrollbar appear? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742236077a2438105.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论