简体   繁体   English

在 mousemove 事件期间无法设置元素样式顶部或左侧

[英]Unable to set element style top or left during mousemove event

TLDR: Here is a JS Fiddle https://jsfiddle.net/qn8jhsaf/10/ TLDR:这是一个 JS Fiddle https://jsfiddle.net/qn8jhsaf/10/

I am attempting to create a simple drag and drop UI using mouse events as I didn't like any of the libraries available for the framework I am using.我正在尝试使用鼠标事件创建一个简单的拖放 UI,因为我不喜欢任何可用于我正在使用的框架的库。 While I have all the events wired up and am getting the application side behavior I want, animating the div moving around isn't working how I would expect.虽然我已经连接了所有事件并获得了我想要的应用程序端行为,但动画 div 移动并没有按照我的预期工作。

I am trying to use vanilla JS to insert a cloned div into the dom, absolutlely position it, and then move it around with tranform: translate as the mouse moves around.我正在尝试使用 vanilla JS 将克隆的 div 插入到 dom 中,绝对是 position 它,然后在鼠标移动时使用 tranform: translate 移动它。

So in onMouseDown, I'm doing just that:所以在 onMouseDown 中,我正在这样做:

const onMouseDown = (e) => {
  isDragging = true
  const rect = element.getBoundingClientRect()
  let node = element.cloneNode(true)
  node.style.position = 'absolute'
  node.style.zIndex = 1000
  node.style.left = rect.x
  node.style.top = rect.y
  console.log(rect.x)
  console.log(node.style.left)
  draggableNode = node
  document.body.append(node)
}

The curious part happens when I call node.style.left = .奇怪的部分发生在我调用node.style.left =时。 It does not throw an error or anything but the value remains stubbornly empty ( "" ) and it does not get translated to the style attribute of the cloned div.它不会抛出错误或任何东西,但该值仍然是顽固的空( "" )并且它不会被转换为克隆 div 的样式属性。 As far as I can tell from documentation this is a supported thing.据我从文档中可以看出,这是受支持的。 I'm not sure what else to try.我不确定还能尝试什么。

The left and top styles need to be strings.左上角 styles 需要是字符串。

  node.style.left = `${rect.x}px`
  node.style.top  = `4{rect.y}px`

FWIW, I swear I tried this at some point but ¯\_(ツ)_/¯ FWIW,我发誓我在某个时候尝试过,但是¯\_(ツ)_/¯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法通过JavaScript设置div.style.left和div.style.top css属性 - Unable to set div.style.left and div.style.top css properties via JavaScript mousemove期间的event.offsetX - event.offsetX during mousemove 如何在mousemove事件(左上,右上,左下和右下)上确定鼠标的方向 - How to determine the direction of mouse on mousemove event (top-left, top-right, bottom-left and bottom-right) 我想在 mousemove 事件期间检测鼠标右键或鼠标左键是否按下 - I want to detect whether the right or left mouse button is down during a mousemove event .classname元素即使鼠标不在其上方也需要mousemove事件。 如何获取事件元素? - .classname element needs mousemove event even when the cursor is not on top of them. How to get event to elements? 在Mousepress期间IE9 Mousemove事件未触发 - IE9 Mousemove event not firing during Mousepress 检测 HTML 画布元素的 mouseMove out 事件 - Detect mouseMove out event for HTML canvas element Mousemove 事件:鼠标相对于父元素的位置 - Mousemove Event: mouse position relative to parent element Mousemove 事件未在页面元素或文档上触发 - Mousemove event not firing on page element or document 如何使用 event.target.style 设置单个元素的样式 - How to set the style of a single element with event.target.style
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM