js怎么控制滚动条使div在一定区间显示

JavaScript07

js怎么控制滚动条使div在一定区间显示,第1张

js控制滚动条使div在一定区间显示方法步骤。

一,新建一个html文件,命名为test.html,用于讲解如何使用JS控制DIV内容的滚动条。

二,在test.html文件内,创建两个div模块,一个div模板包含另一个div模块。

三,在test.html文件内,分别设置两个div模块的id属性为test,ntest。

四,在css中,分别通过id来设置两个div的样式,定义它们的高度、宽度,外部div将overflow属性设置为auto,即超过宽度隐藏,并出现滚动条。注意,内部div的高度、宽度必须比外部div的宽高大,才会出现滚词条。

五,在js中,通过id获得外部div对象,使用scrollTop属性控制垂直滚动条位置为100px,使用scrollLeft属性控制水平滚动条位置为150px。

六,在浏览器打开test.html文件,查看实现的效果。实现在一定区间内显示。

请确认,谢谢。

解决这个问题的思路:设置下拉菜单的最大宽度,然后文字超出时,出现横向滚动条

原因:因为表格左侧固定列,并且列宽大于了滚动条的横向宽度,导致将横向滚动条,遮盖住了。解决方式:对应表格的滚动条,设置min-width,需要大于固定列的宽度,注意:滚动条是表格哪一级的class出现的,需要对应写这一级容器的滚动条样式。

<!DOCTYPE html>

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=utf-8" />

<title>js滚动条特效</title>

<style type="text/css">

*{ margin:0padding:0}

p{ height:1000px}

#mainBox{ width:400pxheight:500pxborder:1px #bbb solidposition:relativeoverflow:hiddenmargin:50px auto}

#content{ height:2500pxposition:absoluteleft:0top:0background:url(/jscss/demoimg/logo_demo1.gif) }

.scrollDiv{ width:18px position:absolutetop:0background:#666border-radius:10px}

</style>

</head>

<body>

<div id="mainBox">

<div id="content"></div>

</div>

<p></p>

<script type="text/javascript">

var doc=document

var _wheelData=-1

var mainBox=doc.getElementById('mainBox')

function bind(obj,type,handler){

var node=typeof obj=="string"?$(obj):obj

if(node.addEventListener){

node.addEventListener(type,handler,false)

}else if(node.attachEvent){

node.attachEvent('on'+type,handler)

}else{

node['on'+type]=handler

}

}

function mouseWheel(obj,handler){

var node=typeof obj=="string"?$(obj):obj

bind(node,'mousewheel',function(event){

var data=-getWheelData(event)

handler(data)

if(document.all){

window.event.returnValue=false

}else{

event.preventDefault()

}

})

//火狐

bind(node,'DOMMouseScroll',function(event){

var data=getWheelData(event)

handler(data)

event.preventDefault()

})

function getWheelData(event){

var e=event||window.event

return e.wheelDelta?e.wheelDelta:e.detail*40

}

}

function addScroll(){

this.init.apply(this,arguments)

}

addScroll.prototype={

init:function(mainBox,contentBox,className){

var mainBox=doc.getElementById(mainBox)

var contentBox=doc.getElementById(contentBox)

var scrollDiv=this._createScroll(mainBox,className)

this._resizeScorll(scrollDiv,mainBox,contentBox)

this._tragScroll(scrollDiv,mainBox,contentBox)

this._wheelChange(scrollDiv,mainBox,contentBox)

this._clickScroll(scrollDiv,mainBox,contentBox)

},

//创建滚动条

_createScroll:function(mainBox,className){

var _scrollBox=doc.createElement('div')

var _scroll=doc.createElement('div')

var span=doc.createElement('span')

_scrollBox.appendChild(_scroll)

_scroll.appendChild(span)

_scroll.className=className

mainBox.appendChild(_scrollBox)

return _scroll

},

//调整滚动条

_resizeScorll:function(element,mainBox,contentBox){

var p=element.parentNode

var conHeight=contentBox.offsetHeight

var _width=mainBox.clientWidth

var _height=mainBox.clientHeight

var _scrollWidth=element.offsetWidth

var _left=_width-_scrollWidth

p.style.width=_scrollWidth+"px"

p.style.height=_height+"px"

p.style.left=_left+"px"

p.style.position="absolute"

p.style.background="#ccc"

contentBox.style.width=(mainBox.offsetWidth-_scrollWidth)+"px"

var _scrollHeight=parseInt(_height*(_height/conHeight))

if(_scrollHeight>=mainBox.clientHeight){

element.parentNode.style.display="none"

}

element.style.height=_scrollHeight+"px"

},

//拖动滚动条

_tragScroll:function(element,mainBox,contentBox){

var mainHeight=mainBox.clientHeight

element.onmousedown=function(event){

var _this=this

var _scrollTop=element.offsetTop

var e=event||window.event

var top=e.clientY

//this.onmousemove=scrollGo

document.onmousemove=scrollGo

document.onmouseup=function(event){

this.onmousemove=null

}

function scrollGo(event){

var e=event||window.event

var _top=e.clientY

var _t=_top-top+_scrollTop

if(_t>(mainHeight-element.offsetHeight)){

_t=mainHeight-element.offsetHeight

}

if(_t<=0){

_t=0

}

element.style.top=_t+"px"

contentBox.style.top=-_t*(contentBox.offsetHeight/mainBox.offsetHeight)+"px"

_wheelData=_t

}

}

element.onmouseover=function(){

this.style.background="#444"

}

element.onmouseout=function(){

this.style.background="#666"

}

},

//鼠标滚轮滚动,滚动条滚动

_wheelChange:function(element,mainBox,contentBox){

var node=typeof mainBox=="string"?$(mainBox):mainBox

var flag=0,rate=0,wheelFlag=0

if(node){

mouseWheel(node,function(data){

wheelFlag+=data

if(_wheelData>=0){

flag=_wheelData

element.style.top=flag+"px"

wheelFlag=_wheelData*12

_wheelData=-1

}else{

flag=wheelFlag/12

}

if(flag<=0){

flag=0

wheelFlag=0

}

if(flag>=(mainBox.offsetHeight-element.offsetHeight)){

flag=(mainBox.clientHeight-element.offsetHeight)

wheelFlag=(mainBox.clientHeight-element.offsetHeight)*12

}

element.style.top=flag+"px"

contentBox.style.top=-flag*(contentBox.offsetHeight/mainBox.offsetHeight)+"px"

})

}

},

_clickScroll:function(element,mainBox,contentBox){

var p=element.parentNode

p.onclick=function(event){

var e=event||window.event

var t=e.target||e.srcElement

var sTop=document.documentElement.scrollTop>0?document.documentElement.scrollTop:document.body.scrollTop

var top=mainBox.offsetTop

var _top=e.clientY+sTop-top-element.offsetHeight/2

if(_top<=0){

_top=0

}

if(_top>=(mainBox.clientHeight-element.offsetHeight)){

_top=mainBox.clientHeight-element.offsetHeight

}

if(t!=element){

element.style.top=_top+"px"

contentBox.style.top=-_top*(contentBox.offsetHeight/mainBox.offsetHeight)+"px"

_wheelData=_top

}

}

}

}

new addScroll('mainBox','content','scrollDiv')

</script>

</body>

</html>