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=".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>
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./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>
Share
Improve this question
edited Aug 10, 2016 at 13:09
Sebastian Simon
19.6k8 gold badges61 silver badges84 bronze badges
asked Aug 10, 2016 at 13:03
Bhaskar Saurabh VickyBhaskar Saurabh Vicky
651 silver badge13 bronze badges
2 Answers
Reset to default 4using js
let sidebar = document.querySelector(".sidebar");
let content = document.querySelector(".content");
let dragholder = document.querySelector(".dragholder");
function onMouseMove(e){
sidebar.style.cssText = `width: ${ e.pageX }px`;
content.style.cssText = `width: ${ window.innerWidth - e.pageX }px`;
}
function onMouseDown(e){
document.addEventListener('mousemove',onMouseMove);
}
function onMouseUp(e){
document.removeEventListener('mousemove',onMouseMove);
}
dragholder.addEventListener('mousedown', onMouseDown);
dragholder.addEventListener('mouseup', onMouseUp);
content.addEventListener('mouseup', onMouseUp);
document.addEventListener('mouseup', onMouseUp);
body{
margin:0;
padding:0;
}
.container{
display:flex;
}
.sidebar{
background: lightgreen;
height: 100vh;
width: 20%;
position:relative;
}
.dragholder{
position:absolute;
height: 100px;
width: 5px;
background: blue;
right:0;
top:50%;
transform: translate(-50%, -50%);
border-radius: 80px;
}
.dragholder:hover{
cursor: move;
}
.content{
background:red;
height:100vh;
width: 80%;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Drag bar</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<div class="sidebar">
<div class="dragholder"></div>
</div>
<div class="content"></div>
</div>
<script src="./index.js"></script>
</body>
</html>
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./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>