Issue
I am unable to drag the sidebar left or right. I want to increase and decrease the sidebar when it is dragged to the right and the left. When I tried to increase or decrease the width of the sidebar, it remained the same. There were no changes happening in that.
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>
Solution
You have multiple issues with your script.
The #main div overlaps the #sidebar, so the very right edge of the sidebar is not visible, nor accessible by the mouse. So, your mousedown event does not trigger, because it never receives a mousedown.
Two things to do here:
Add z-index: 1 for #main and z-index: 2 for #sidebar.
Also, to make your drag handle div to fit correctly, change it's css to this:
#dragbar {
background-color: black;
height: 100%;
width: 3px;
cursor: col-resize;
position: absolute;
right: 0;
top: 0;
}
See the snippet below for a working version:
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>
Answered By - beerwin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.