【发布时间】:2017-06-24 12:24:48
【问题描述】:
我正在尝试使用OffsetLeft,但无法正常工作。在 Firefox 中也不会触发拖动事件。
我这里有代码:jsbin link
<div id="parent">
<div draggable="true" id="child" ondrag="dragging(event)" ></div>
</div>
<script>
const parent = document.getElementById('parent');
const child = document.getElementById('child');
function dragging(event) {
console.log("dragging");
parent.style.width = child.offsetLeft;
}
</script>
和css:
/* green box */
#parent{
position:absolute; -- notice the parent is absolute positioned. (not sure if it has an impact on how it works - but i need it to be absolute.
top: 100;
left: 100;
background-color: green;
height: 100px;
width: 100px;
display: flex;
justify-content: flex-end;
}
/* blue bar */
#child{
width: 5px;
background-color: blue;
cursor: e-resize;
}
那么我怎么能做到这一点,同时也是cross-browser compatible?
我需要html5 + js 解决方案 - 因为我将使用另一种名为elm 的编程语言的逻辑。我只能从 DOM 中读取内容,不能对其进行变异。所以我不能使用已经构建的库,比如 Jquery。
感谢您的关注。
【问题讨论】:
-
主要问题是你还需要添加一个单元,像这样
child.offsetLeft + 'px'; -
@LGSon 哈哈,很好,谢谢。通用方法仍然很糟糕,因为拖动蓝线 - 根本不会改变
offsetLeft- 这是反直觉的 - 我需要一种不同的方法。 -
你应该使用
event.pageX...发布了一个答案,现在正在检查为什么FF不会一起玩
标签: html drag-and-drop draggable drag