我无法向左或向右拖动侧边栏。当它被拖到右侧和左侧时,我想增加和减少侧边栏。当我试图增加或减少侧边栏的宽度时,它保持不变。那里没有发生任何变化。
var i = 0;
$('#dragbar').mousedown(function(e) {
e.preventDefault();
$('#mousestatus').html("mousedown" + i++);
$(document).mousemove(function(e) {
$('#position').html(e.pageX + ', ' + e.pageY);
$('#sidebar').css("width", e.pageX + 2);
$('#main').css("left", e.pageX + 2);
})
console.log("leaving mouseDown");
});
$(document).mouseup(function(e) {
$('#clickevent').html('in another mouseUp event' + i++);
$(document).unbind('mousemove');
});
#main {
background-color: BurlyWood;
float: right;
position: absolute;
top: 100px;
bottom: 38px;
right: 0;
left: 200px;
}
#sidebar {
background-color: IndianRed;
width: 200px;
float: left;
position: absolute;
top: 100px;
bottom: 38px;
overflow-y: hidden;
}
#footer {
background-color: PaleGoldenRod;
width: 100%;
height: 38px;
bottom: 0;
position: absolute;
}
#header {
background-color: wheat;
width: 100%;
height: 100px;
}
#dragbar {
background-color: black;
height: 100%;
float: right;
width: 3px;
cursor: col-resize;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="header">
header
<span id="mousestatus"></span>
<span id="clickevent"></span>
</div>
<div id="sidebar">
<span id="position"></span>
<div id="dragbar"></div>
sidebar
</div>
<div id="main">
main
</div>
<div id="footer">
footer
</div>
答案 0 :(得分:1)
您的脚本存在多个问题。
#main
div与#sidebar
重叠,因此侧边栏的右边缘不可见,鼠标也无法访问。所以,你的mousedown事件不会触发,因为它永远不会收到mousedown。
这里有两件事要做:
为z-index: 1
添加#main
,为z-index: 2
添加#sidebar
。
另外,要使拖动手柄div正确适合,请将其css更改为:
#dragbar {
background-color: black;
height: 100%;
width: 3px;
cursor: col-resize;
position: absolute;
right: 0;
top: 0;
}
请参阅下面的代码段了解工作版本:
var i = 0;
$('#dragbar').mousedown(function(e) {
e.preventDefault();
$('#mousestatus').html("mousedown" + i++);
$(document).mousemove(function(e) {
$('#position').html(e.pageX + ', ' + e.pageY);
$('#sidebar').css("width", e.pageX + 2);
$('#main').css("left", e.pageX + 2);
})
console.log("leaving mouseDown");
});
$(document).mouseup(function(e) {
$('#clickevent').html('in another mouseUp event' + i++);
$(document).unbind('mousemove');
});
#main {
background-color: BurlyWood;
float: right;
position: absolute;
top: 100px;
bottom: 38px;
right: 0;
left: 200px;
z-index: 1;
}
#sidebar {
background-color: IndianRed;
width: 200px;
float: left;
position: absolute;
top: 100px;
bottom: 38px;
overflow-y: hidden;
z-index: 2;
}
#footer {
background-color: PaleGoldenRod;
width: 100%;
height: 38px;
bottom: 0;
position: absolute;
}
#header {
background-color: wheat;
width: 100%;
height: 100px;
}
#dragbar {
background-color: black;
height: 100%;
width: 3px;
cursor: col-resize;
position: absolute;
right: 0;
top: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="header">
header
<span id="mousestatus"></span>
<span id="clickevent"></span>
</div>
<div id="sidebar">
<span id="position"></span>
<div id="dragbar"></div>
sidebar
</div>
<div id="main">
main
</div>
<div id="footer">
footer
</div>