<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
body {
margin: 0;
position: relative;
height: 100vh;
}
.menu-wrapper {
position: absolute;
left: 0;
top: 0;
height: 100%;
width: 300px;
background-color: #ccc;
}
.resize-bar {
position: absolute;
top: 0;
right: 0px;
width: 10px;
height: 100%;
cursor: col-resize;
background-color: orange;
}
.content {
margin-left: 300px;
height: 100%;
background-color: #999;
}
</style>
</head>
<body>
<div class="menu-wrapper">
<div class="resize-bar"></div>
</div>
<div class="content"></div>
<script>
let resizing = false
const menu = document.querySelector('.menu-wrapper')
const resizeBar = document.querySelector('.resize-bar')
resizeBar.addEventListener('mousedown', () => {
resizing = true
})
window.addEventListener('mousemove', (e) => {
if (resizing) {
e.preventDefault()
e.stopPropagation()
const { screenX } = e
menu.style.width = screenX + 'px'
}
})
window.addEventListener('mouseup', () => {
resizing = false
})
</script>
</body>
</html>
鼠标拖动改变侧边栏的宽度
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...