隐藏调整大小属性的角斜杠,但功能应该正常工作。

huangapple go评论63阅读模式
英文:

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 -->

&lt;div&gt;
  &lt;p&gt;Let the user resize both the height and the width of this div element.&lt;/p&gt;
  &lt;p&gt;To resize: Click and drag the bottom right corner of this div element.&lt;/p&gt;
&lt;/div&gt;

<!-- 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 -->

&lt;div&gt;
  &lt;p&gt;Let the user resize both the height and the width of this div element.&lt;/p&gt;
  &lt;p&gt;To resize: Click and drag the bottom right corner of this div element.&lt;/p&gt;
&lt;/div&gt;

<!-- 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(&quot;resizableDiv&quot;);

// Add Event when MouseDown detected
resizableDiv.addEventListener(&quot;mousedown&quot;, function(event) {
  // If sub-element clicked, skip resize (try mousedown on &lt;p&gt;)
  if (event.target === resizableDiv) {
    event.preventDefault();
    resizeDiv(event); // resize
  }
});

// Resize
const resizeDiv = (event) =&gt; {
  // 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) =&gt; {
    // 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 = () =&gt; {
    // Removing the event listeners when resizing is stopped
    document.documentElement.removeEventListener(&quot;mousemove&quot;, resize);
    document.documentElement.removeEventListener(&quot;mouseup&quot;, stopResize);
  };

  // Adding event listeners for mousemove and mouseup events
  document.documentElement.addEventListener(&quot;mousemove&quot;, resize);
  document.documentElement.addEventListener(&quot;mouseup&quot;, stopResize);
};

<!-- language: lang-css -->

div {
  border: 2px solid;
  padding: 20px;
  width: 300px;
  overflow: hidden;
  cursor: nw-resize;
}

div &gt; * {
  cursor: initial;
}

<!-- language: lang-html -->

&lt;div id=&quot;resizableDiv&quot;&gt;
  &lt;p&gt;Let the user resize both the height and the width of this div element.&lt;/p&gt;
  &lt;p&gt;To resize: Click and drag the bottom right corner of this div element.&lt;/p&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月15日 15:41:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76480175.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定