我有一个绝对定位的侧面板,我需要通过拖动此边框来更改其宽度。我还需要更改边框悬停上的光标。是否可以在不添加另一个 div 进行拖动的情况下做到这一点?
这是标记:
#right_panel {
position: absolute;
border-left: solid 3px #ccc;
width: 100px;
height: 100%;
right: 0;
background-color: #f0f0f0;
}
<body>
<div id="right_panel"></div>
</body>
我不需要完整的解决方案。答 是(有文档参考)/否答案就足够了。我不需要助手的回答
div
。我已经有一个了:
var m_pos;
function resize(e){
var parent = resize_el.parentNode;
var dx = m_pos - e.x;
m_pos = e.x;
parent.style.width = (parseInt(getComputedStyle(parent, '').width) + dx) + "px";
}
var resize_el = document.getElementById("resize");
resize_el.addEventListener("mousedown", function(e){
m_pos = e.x;
document.addEventListener("mousemove", resize, false);
}, false);
document.addEventListener("mouseup", function(){
document.removeEventListener("mousemove", resize, false);
}, false);
#right_panel {
position: absolute;
width: 96px;
padding-left: 4px;
height: 100%;
right: 0;
background-color: #f0f0f0;
}
#resize {
background-color: #ccc;
position: absolute;
left: 0;
width: 4px;
height: 100%;
cursor: w-resize;
}
<body>
<div id="right_panel">
<div id="resize"></div>
</div>
</body>
这又是我想要的功能,只是我想删除多余的
div
。
当然可以在没有额外 div 的情况下做到这一点。使用 css 和
::after
创建边框并更改光标。使用 MouseEvent.offsetX
来确定是否处理元素中的单击。
在您的示例中,您希望单击主 div,但仅单击前 4 个像素。您可以通过在点击处理程序中检查
e.offsetX < 4
来做到这一点:
const BORDER_SIZE = 4;
const panel = document.getElementById("right_panel");
let m_pos;
function resize(e){
const dx = m_pos - e.x;
m_pos = e.x;
panel.style.width = (parseInt(getComputedStyle(panel, '').width) + dx) + "px";
}
panel.addEventListener("mousedown", function(e){
if (e.offsetX < BORDER_SIZE) {
m_pos = e.x;
document.addEventListener("mousemove", resize, false);
}
}, false);
document.addEventListener("mouseup", function(){
document.removeEventListener("mousemove", resize, false);
}, false);
#right_panel {
position: absolute;
width: 96px;
padding-left: 4px;
height: 100%;
right: 0;
background-color: #f0f0ff;
}
#right_panel::after {
content: '';
background-color: #ccc;
position: absolute;
left: 0;
width: 4px;
height: 100%;
cursor: ew-resize;
}
<body>
<div id="right_panel"></div>
</body>
希望对你有帮助
var isResizing = false,
lastDownX = 0;
$(function () {
var container = $('#container'),
left = $('#left_panel'),
right = $('#right_panel'),
handle = $('#drag');
handle.on('mousedown', function (e) {
isResizing = true;
lastDownX = e.clientX;
});
$(document).on('mousemove', function (e) {
// we don't want to do anything if we aren't resizing.
if (!isResizing)
return;
var offsetRight = container.width() - (e.clientX - container.offset().left);
left.css('right', offsetRight);
right.css('width', offsetRight);
}).on('mouseup', function (e) {
// stop resizing
isResizing = false;
});
});
body, html {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
#container {
width: 100%;
height: 100%;
}
#left_panel {
position: absolute;
left: 0;
top: 0;
bottom: 0;
right: 100px;
background: grey;
}
#right_panel {
position: absolute;
right: 0;
top: 0;
bottom: 0;
width: 200px;
color:#fff;
background: black;
}
#drag {
position: absolute;
left: -4px;
top: 0;
bottom: 0;
width: 8px;
cursor: w-resize;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<div id="container">
<div id="left_panel"> left content! </div>
<div id="right_panel">
<div id="drag"></div> right content!
</div>
</div>
没有 jQuery:
var isResizing = false;
(function() {
var container = document.getElementById("container"),
left = document.getElementById("left_panel"),
right = document.getElementById("right_panel"),
handle = document.getElementById("drag");
handle.onmousedown = function(e) {
isResizing = true;
};
document.onmousemove = function(e) {
// we don't want to do anything if we aren't resizing.
if (!isResizing) {
return;
}
var offsetRight = container.clientWidth - (e.clientX - container.offsetLeft);
left.style.right = offsetRight + "px";
right.style.width = offsetRight + "px";
}
document.onmouseup = function(e) {
// stop resizing
isResizing = false;
}
})();
body {
font-family: Helvetica, Arial;
font-size: 12px;
}
body,
html {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
#container {
width: 100%;
height: 100%;
}
#left_panel {
position: absolute;
left: 0;
top: 0;
bottom: 0;
right: 100px;
background: grey;
}
#right_panel {
position: absolute;
right: 0;
top: 0;
bottom: 0;
width: 200px;
color: #fff;
background: black;
}
#drag {
position: absolute;
left: -4px;
top: 0;
bottom: 0;
width: 8px;
cursor: w-resize;
}
<body>
<div id="container">
<div id="left_panel"> left content! </div>
<div id="right_panel">
<div id="drag"></div> right content!
</div>
</div>
</body>
2022 答案:
现在可以通过简单地使用 css 中的“resize”属性来完成,
确保该 div 自动溢出,这样调整大小就可以了。
如本片段所示:
#right_panel {
position: absolute;
border-left: solid 3px #ccc;
width: 100px;
height: 100%;
right: 0;
background-color: #f0f0f0;
resize: horizontal;
overflow-x: auto;
}