英文:
Hide corner slashes of resize property but functionality should work
问题
我已经使用以下代码实现了调整大小的功能。我需要在使用调整大小属性时去掉那些角落的斜杠,但功能应该继续正常。我们可以实现吗?
div {
border: 2px solid;
padding: 20px;
width: 300px;
resize: horizontal;
overflow: auto;
}
<div>
<p>让用户调整此div元素的高度和宽度。</p>
<p>要调整大小:单击并拖动此div元素的右下角。</p>
</div>
英文:
I have implemented the resize functionality using the following code. I need to get rid of that corner slashes while using the resize property but the functionality should work. can we achieve it?
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
div {
border: 2px solid;
padding: 20px;
width: 300px;
resize: horizontal;
overflow: auto;
}
<!-- language: lang-html -->
<div>
<p>Let the user resize both the height and the width of this div element.</p>
<p>To resize: Click and drag the bottom right corner of this div element.</p>
</div>
<!-- end snippet -->
答案1
得分: 0
以下是翻译好的部分:
使用 CSS
:: -webkit-resizer 选择器 - MDN
不幸的是,像 Firefox 这样的浏览器也不支持这一功能,但目前在所有其他现代浏览器中应该可以正常工作。
div {
border: 2px solid;
padding: 20px;
width: 300px;
resize: horizontal;
overflow: auto;
}
div::-webkit-resizer {
display: none;
}
<div>
<p>允许用户同时调整此 div 元素的高度和宽度。</p>
<p>要调整大小:单击并拖动此 div 元素的右下角。</p>
</div>
使用 JavaScript
如果要在所有浏览器中隐藏调整大小的手柄,那么作为替代方案,您需要实现自己的逻辑来在点击并拖动元素时更改其宽度。以下是一个简短的示例,演示了如何实现此功能:
// 获取 Div 元素
const resizableDiv = document.getElementById("resizableDiv");
// 在检测到鼠标按下事件时添加事件
resizableDiv.addEventListener("mousedown", function(event) {
// 如果点击的是子元素,请跳过调整大小(尝试在 <p> 上按下鼠标)
if (event.target === resizableDiv) {
event.preventDefault();
resizeDiv(event); // 调整大小
}
});
// 调整大小
const resizeDiv = (event) => {
// 解构事件对象以获取 clientX 和 clientY
const { clientX, clientY } = event;
// 获取 resizableDiv 的计算宽度和高度
const { width, height } = getComputedStyle(resizableDiv);
const resize = (event) => {
// 根据初始值和鼠标移动计算新的宽度和高度
const newWidth = parseInt(width, 10) + event.clientX - clientX;
const newHeight = parseInt(height, 10) + event.clientY - clientY;
// 将新的宽度和高度应用于 resizableDiv
Object.assign(resizableDiv.style, { width: `${newWidth}px`, height: `${newHeight}px` });
};
const stopResize = () => {
// 停止调整大小时移除事件监听器
document.documentElement.removeEventListener("mousemove", resize);
document.documentElement.removeEventListener("mouseup", stopResize);
};
// 添加鼠标移动和鼠标释放事件的事件监听器
document.documentElement.addEventListener("mousemove", resize);
document.documentElement.addEventListener("mouseup", stopResize);
};
div {
border: 2px solid;
padding: 20px;
width: 300px;
overflow: hidden;
cursor: nw-resize;
}
div > * {
cursor: initial;
}
<div id="resizableDiv">
<p>允许用户同时调整此 div 元素的高度和宽度。</p>
<p>要调整大小:单击并拖动此 div 元素的右下角。</p>
</div>
英文:
With CSS
::-webkit-resizer selector - MDN
Unfortunately, Firefox - like many others - does not support this either, but currently it should work in all other modern browsers.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
div {
border: 2px solid;
padding: 20px;
width: 300px;
resize: horizontal;
overflow: auto;
}
div::-webkit-resizer {
display: none;
}
<!-- language: lang-html -->
<div>
<p>Let the user resize both the height and the width of this div element.</p>
<p>To resize: Click and drag the bottom right corner of this div element.</p>
</div>
<!-- end snippet -->
With JavaScript
If you want to hide the resize handle in all browsers, then as an alternative solution, you will need to implement your own logic to change the width of the element when it is clicked and dragged. Here's a short example to demonstrate how you can achieve this:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
// Get Div
const resizableDiv = document.getElementById("resizableDiv");
// Add Event when MouseDown detected
resizableDiv.addEventListener("mousedown", function(event) {
// If sub-element clicked, skip resize (try mousedown on <p>)
if (event.target === resizableDiv) {
event.preventDefault();
resizeDiv(event); // resize
}
});
// Resize
const resizeDiv = (event) => {
// Destructuring the event object to get clientX and clientY
const { clientX, clientY } = event;
// Getting the computed width and height of the resizableDiv
const { width, height } = getComputedStyle(resizableDiv);
const resize = (event) => {
// Calculating the new width and height based on the initial values and mouse movement
const newWidth = parseInt(width, 10) + event.clientX - clientX;
const newHeight = parseInt(height, 10) + event.clientY - clientY;
// Applying the new width and height to the resizableDiv
Object.assign(resizableDiv.style, { width: `${newWidth}px`, height: `${newHeight}px` });
};
const stopResize = () => {
// Removing the event listeners when resizing is stopped
document.documentElement.removeEventListener("mousemove", resize);
document.documentElement.removeEventListener("mouseup", stopResize);
};
// Adding event listeners for mousemove and mouseup events
document.documentElement.addEventListener("mousemove", resize);
document.documentElement.addEventListener("mouseup", stopResize);
};
<!-- language: lang-css -->
div {
border: 2px solid;
padding: 20px;
width: 300px;
overflow: hidden;
cursor: nw-resize;
}
div > * {
cursor: initial;
}
<!-- language: lang-html -->
<div id="resizableDiv">
<p>Let the user resize both the height and the width of this div element.</p>
<p>To resize: Click and drag the bottom right corner of this div element.</p>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论