如何使SVG图像无法拖动和点击。

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

How to make svg image not draggable and clickable

问题

I'll provide translations for the code and text you've provided:

我正在开发一个SVG图像滑块,一切都正常,直到我加载带有文本的图像。然后,当我拖动鼠标时,文本被选中,这是正常的。当我使用以下CSS属性来阻止它时:

user-select: none
-moz-user-select: none
-webkit-user-drag: none
-webkit-user-select: none
-ms-user-select: none
pointer-events: none

一切又正常工作了,但在这样做的同时,SVG元素中的每个单击操作都被阻止了。我的问题是如何使元素可拖动并同时启用单击操作。

编辑
---

我提供了解释我的问题的示例代码,希望这能澄清一切。

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

document.getElementById('inspector-wrapper').addEventListener('mousedown', () => {
  const lastObjX = this.imgX; // 获取图像的X、Y
  const lastObjY = this.imgY;
  this.lastX = this.currentX; // 获取开始X、Y
  this.lastY = this.currentY;
  console.log(this.currentX);
  this.interval = setInterval(() => { // 当按住鼠标1键时
    this.definePosition(lastObjX, lastObjY);
  }, 10);

  private definePosition(lastObjX: number, lastObjY: number) {
    const distanceToMoveX = this.currentX - this.lastX;
    if (distanceToMoveX !== 0) {
      this.imgX = lastObjX + distanceToMoveX; // 设置新X
    }
    if (this.isYAvailable()) { // 如果Y可用,设置新值
      const distanceToMoveY = this.currentY - this.lastY;
      if (distanceToMoveY !== 0) {
        this.imgY = lastObjY + distanceToMoveY;
      }
    }
    this.setImgXY();
  }
<!-- language: lang-css -->

#inspector-wrapper
  position: absolute
  overflow: hidden
  background-color: transparent

:host::ng-deep object
  position: absolute
  left: 0
  top: 0
  user-select: none
  -moz-user-select: none
  -webkit-user-drag: none
  -webkit-user-select: none
  -ms-user-select: none
  pointer-events: none

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

<div id="inspector-wrapper">
  <object data="../assets/main.svg" id="test" type="image/svg+xml"></object>
</div>

<!-- end snippet -->

Please note that I've translated the code and provided it in a structured format. If you need further assistance or have specific questions about the code, feel free to ask.

英文:

I'm working on svg image slider and everything works fine until I load image with text. Then when I drag my mouse text gets selected and that's normal. When I block it with css properties:

  user-select: none
  -moz-user-select: none
  -webkit-user-drag: none
  -webkit-user-select: none
  -ms-user-select: none
  pointer-events: none

Everything works again but while doing that every onclick action in svg element gets blocked. My question is how can I make element draggable and enable onclick action in same time.

Edit

I'm providing sample code for my problem, I hope it will clarify everything.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

document.getElementById(&#39;inspector-wrapper&#39;).addEventListener(&#39;mousedown&#39;, () =&gt; {
      const lastObjX = this.imgX; // getting img X,Y
      const lastObjY = this.imgY;
      this.lastX = this.currentX; // getting start X,Y
      this.lastY = this.currentY;
      console.log(this.currentX);
      this.interval = setInterval(() =&gt; { // when holding mouse1
        this.definePosition(lastObjX, lastObjY);
      }, 10);

      private definePosition(lastObjX: number, lastObjY: number) {
        const distanceToMoveX = this.currentX - this.lastX;
        if (distanceToMoveX !== 0) {
          this.imgX = lastObjX + distanceToMoveX; // setting new X
        }
        if (this.isYAvailable()) { // if Y available set new value
          const distanceToMoveY = this.currentY - this.lastY;
          if (distanceToMoveY !== 0) {
            this.imgY = lastObjY + distanceToMoveY;
          }
        }
        this.setImgXY();
      }

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

#inspector-wrapper
  position: absolute
  overflow: hidden
  background-color: transparent

\:host::ng-deep object
  position: absolute
  left: 0
  top: 0
  user-select: none
  -moz-user-select: none
  -webkit-user-drag: none
  -webkit-user-select: none
  -ms-user-select: none
  pointer-events: none

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

&lt;div id=&quot;inspector-wrapper&quot;&gt;
  &lt;object data=&quot;../assets/main.svg&quot; id=&quot;test&quot; type=&quot;image/svg+xml&quot;&gt;&lt;/object&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

document.getElementsByTagName("img")[0].onclick = function() {
  console.log("Pointer events work");
}

img {
  user-select: none;
}

<img src="https://www.gravatar.com/avatar/4db6ced4189e044379de4d14c0208ddf?s=48&d=identicon&r=PG&f=1" draggable=false>
<!-- I needed a valid image, so I used my profile picture -->
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

document.getElementsByTagName(&quot;img&quot;)[0].onclick = function() {
  console.log(&quot;Pointer events work&quot;);
}

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

img {
  user-select: none;
}

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

&lt;img src=&quot;https://www.gravatar.com/avatar/4db6ced4189e044379de4d14c0208ddf?s=48&amp;d=identicon&amp;r=PG&amp;f=1&quot; draggable=false&gt;
&lt;!-- I needed a valid image, so I used my profile picture --&gt;

<!-- end snippet -->

This uses user-select: none to disable selection, draggable=false to disable drag-and-drop, and uses an onclick event to demonstrate pointer events still work.

huangapple
  • 本文由 发表于 2020年1月6日 22:07:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613556.html
匿名

发表评论

匿名网友

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

确定