网页设计中怎么在页面中设置一个滚动条

在如图黑色的地方设置一个滚动条是红色区域文字可以根据滚动条的拉动而改变
急求!!!

css属性 overflow-y:auto; DIV里面的内容超过DIV的高度,右边就会自动出现滚动条

1、用一个div,定制成图中的宽度和高度
2、然后再把div的样式设成overflow-y:scroll,当div里的文字超出那个高度的时候,滚动条就出来了。
例如:<div style="width:100px; height:100px; overflow:auto; border:1px solid #000000;"><img src="" style="width:300px; height:300px;"></div>
温馨提示:内容为网友见解,仅供参考
第1个回答  2013-03-27
要么就直接加一个系统默认效果的滚动条,overflow-y:auto;记得div要设置高度;
如果不喜欢系统默认的滚动条,那你可以做一个模拟滚动条,我直接发一个示例给你,你自己对照修改,我想会css的人修改应该不会有问题

<html>
<head>
<title>模拟滚动条</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<STYLE type=text/css>
#scrollerContent {POSITION: absolute}
body {font-size:9pt;color:#660000;text-decoration: none}
</STYLE>
<script>
var upH = 13;//向上的箭头的高度
var upW = 9; //向上的箭头的宽度
var downH = 13;//向下的箭头的高度
var downW = 9;//向下的箭头的宽度
var dragH = 26; //滚动条的高度
var dragW = 9; //滚动条的宽度
var scrollH =68; //滚动体的高度
var speed =9; //滚动的速度
var dom = document.getElementById ? true:false;
var nn4 = document.layers ? true:false;
var ie4 = document.all ? true:false;
var mouseY;
var mouseX;
var clickUp = false;
var clickDown = false;
var clickDrag = false;
var clickAbove = false;
var clickBelow = false;
var timer = setTimeout("",500);
var upL;
var upT;
var downL;
var downT;
var dragL;
var dragT;
var rulerL;
var rulerT;
var contentT;
var contentH;
var contentClipH;
var scrollLength;
var startY;
function down(e){
if((document.layers && e.which!=1) || (document.all && event.button!=1)) return true;
getMouse(e);
startY = (mouseY - dragT);
if(mouseX >= upL && (mouseX <= (upL + upW)) && mouseY >= upT && (mouseY <= (upT + upH))){
clickUp = true;
return scrollUp();
}
else if(mouseX >= downL && (mouseX <= (downL + downW)) && mouseY >= downT && (mouseY <= (downT + downH))){
clickDown = true;
return scrollDown();
}
else if(mouseX >= dragL && (mouseX <= (dragL + dragW)) && mouseY >= dragT && (mouseY <= (dragT + dragH))){
clickDrag = true;
return false;
}
else if(mouseX >= dragL && (mouseX <= (dragL + dragW)) && mouseY >= rulerT && (mouseY <= (rulerT + scrollH))){
if(mouseY < dragT){
clickAbove = true;
clickUp = true;
return scrollUp();
}
else{
clickBelow = true;
clickDown = true;
return scrollDown();
}
}
else{
return true;
}
}
function move(e){
if(clickDrag && contentH > contentClipH){
getMouse(e);
dragT = (mouseY - startY);
if(dragT < (rulerT))
dragT = rulerT;
if(dragT > (rulerT + scrollH - dragH))
dragT = (rulerT + scrollH - dragH);
contentT = ((dragT - rulerT)*(1/scrollLength));
contentT = eval('-' + contentT);
moveTo();
if(ie4)
return false;
}
}
function up(){
clearTimeout(timer);
clickUp = false;
clickDown = false;
clickDrag = false;
clickAbove = false;
clickBelow = false;
return true;
}
function getT(){
if(ie4)
contentT = document.all.scrollerContent.style.pixelTop;
else if(nn4)
contentT = document.scrollerContentClip.document.scrollerContent.top;
else if(dom)
contentT = parseInt(document.getElementById("scrollerContent").style.top);
}
function getMouse(e){
if(ie4){
mouseY = event.clientY + document.body.scrollTop;
mouseX = event.clientX + document.body.scrollLeft;
}
else if(nn4 || dom){
mouseY = e.pageY;
mouseX = e.pageX;
}
}
function moveTo(){
if(ie4){
document.all.scrollerContent.style.top = contentT;
document.all.ruler.style.top = dragT;
document.all.drag.style.top = dragT;
}
else if(nn4){
document.scrollerContentClip.document.scrollerContent.top = contentT;
document.ruler.top = dragT;
document.drag.top = dragT;
}
else if(dom){
document.getElementById("scrollerContent").style.top = contentT + "px";
document.getElementById("drag").style.top = dragT + "px";
document.getElementById("ruler").style.top = dragT + "px";
}
}
function scrollUp(){
getT();
if(clickAbove){
if(dragT <= (mouseY-(dragH/2)))
return up();
}
if(clickUp){
if(contentT < 0){
dragT = dragT - (speed*scrollLength);
if(dragT < (rulerT))
dragT = rulerT;
contentT = contentT + speed;
if(contentT > 0)
contentT = 0;
moveTo();
timer = setTimeout("scrollUp()",25);
}
}
return false;
}
function scrollDown(){
getT();
if(clickBelow){
if(dragT >= (mouseY-(dragH/2)))
return up();
}
if(clickDown){
if(contentT > -(contentH - contentClipH)){
dragT = dragT + (speed*scrollLength);
if(dragT > (rulerT + scrollH - dragH))
dragT = (rulerT + scrollH - dragH);
contentT = contentT - speed;
if(contentT < -(contentH - contentClipH))
contentT = -(contentH - contentClipH);
moveTo();
timer = setTimeout("scrollDown()",25);
}
}
return false;
}
function reloadPage(){
location.reload();
}
function eventLoader(){
if(ie4){
upL = document.all.up.style.pixelLeft;
upT = document.all.up.style.pixelTop;
downL = document.all.down.style.pixelLeft;
downT = document.all.down.style.pixelTop;
dragL = document.all.drag.style.pixelLeft;
dragT = document.all.drag.style.pixelTop;
rulerT = document.all.ruler.style.pixelTop;
contentH = parseInt(document.all.scrollerContent.scrollHeight);
contentClipH = parseInt(document.all.scrollerContentClip.style.height);
}
else if(nn4){
upL = document.up.left;
upT = document.up.top;
downL = document.down.left;
downT = document.down.top;
dragL = document.drag.left;
dragT = document.drag.top;
rulerT = document.ruler.top;
contentH = document.scrollerContentClip.document.scrollerContent.clip.bottom;
contentClipH = document.scrollerContentClip.clip.bottom;
}
else if(dom){
upL = parseInt(document.getElementById("up").style.left);
upT = parseInt(document.getElementById("up").style.top);
downL = parseInt(document.getElementById("down").style.left);
downT = parseInt(document.getElementById("down").style.top);
dragL = parseInt(document.getElementById("drag").style.left);
dragT = parseInt(document.getElementById("drag").style.top);
rulerT = parseInt(document.getElementById("ruler").style.top);
contentH = parseInt(document.getElementById("scrollerContent").offsetHeight);
contentClipH = parseInt(document.getElementById("scrollerContentClip").offsetHeight);
document.getElementById("scrollerContent").style.top = 0 + "px";
}
scrollLength = ((scrollH-dragH)/(contentH-contentClipH));
if(nn4){
document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
window.onresize = reloadPage;
}
document.onmousedown = down;
document.onmousemove = move;
document.onmouseup = up;
}
</script></head>
<BODY
onload="eventLoader()">
<SPAN
id=drag
style="LEFT: 203px;POSITION: absolute; TOP: 116px"><IMG
height=26 src="http://img.alixixi.com/li01.gif" width=9 border=1></SPAN> <SPAN
id=ruler
style="TOP: 116px"></SPAN> <SPAN
id=up
style="LEFT: 203px; POSITION: absolute; TOP: 100px"><IMG
height=13 alt="" src="http://img.alixixi.com/icon_indextool.gif" width=9 border=1></SPAN> <SPAN id=down
style="LEFT: 203px; POSITION: absolute; TOP: 186px"><IMG
height=13 alt="" src="http://img.alixixi.com/icon_indextool.gif" width=9 border=1></SPAN> <SPAN
id=scrollerContentClip
style=" LEFT: 100; VISIBILITY: visible; OVERFLOW: hidden; WIDTH: 100; CLIP: rect(0px 140px 194px 0px); POSITION: absolute; TOP: 100; HEIGHT: 100;border:#CC6600 1px solid"><SPAN
id=scrollerContent style="width: 100px; height: 200px">
ddd
d
d
d

dd
d

d
d

d
d
</SPAN></SPAN>
</body>
</html>追问

不会CSS 可以讲得具体些吗

追答

不会css那就有点麻烦了,需要改的css代码太多,也没法说得具体,给你的只是实例,就相当于给你原理,你得根据原理制作;搞不来模拟滚动条的话就用默认滚动条吧,因为滚动条用css直接换样式的话兼容性不好,所以你还是直接用最简单的默认效果好了,在你需要加滚动条地方加overflow-y:auto;并设置高度,例如:

内容

内容

内容

内容

内容

内容

内容

内容

内容

内容

注:100px是宽度,200px是高度,你根据你自己的部分设置相应的高宽即可

第2个回答  2013-03-27
给div加,给一个简单额例子给你

<div style="width:100px; height:100px; overflow:auto; border:1px solid #000000;"><img src="" style="width:300px; height:300px;"></div>追问

这个要加载什么位置哦

第3个回答  2013-03-27
用一个div,定制成图中的宽度和高度,然后再把div的样式设成overflow-y:scroll,当div里的文字超出那个高度的时候,滚动条就出来了。
第4个回答  2013-03-27
css属性 overflow-y:auto; DIV里面的内容超过DIV的高度,右边就会自动出现滚动条追问

问题是不是要最右边出现滚动条,而是中间白色的框里面出现一个滚动条啊

追答

你的内容是写在DIV中的吧,在DIV设置这个属性,固定高度,超出DIV就会出现滚动条,而不是页面最右边,还可以设置overflow:scroll;滚动条始终出现,在哪个DIV设置,那个DIV就会出现

追问

好的 我去试试

本回答被提问者采纳
相似回答