英文:
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('inspector-wrapper').addEventListener('mousedown', () => {
      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(() => { // 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 -->
<div id="inspector-wrapper">
  <object data="../assets/main.svg" id="test" type="image/svg+xml"></object>
</div>
<!-- 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("img")[0].onclick = function() {
  console.log("Pointer events work");
}
<!-- language: lang-css -->
img {
  user-select: none;
}
<!-- language: lang-html -->
<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 -->
<!-- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论