JavaScript/drag
来自康健生活
引言
在拖动目标上触发事件 (源元素):
- ondragstart - 开始拖动元素时触发
- ondrag - 正在拖动元素时触发
- ondragend - 拖动元素完成后触发
释放目标时触发的事件:
- ondragenter - 当被鼠标拖动的对象进入其容器范围内时触发此事件
- ondragover - 当用户将对象拖到有效的放置目标上时,连续触发目标元素
- ondragleave - 当被鼠标拖动的对象离开其容器范围内时触发此事件
- ondrop - 在一个拖动过程中,释放鼠标键时触发此事件
事件
ondragover
Syntax
| Inline HTML | <ELEMENT ondragover = "handler" ... >
|
All platforms |
| Event property | object.ondragover = handler
|
js |
| Named script | <SCRIPT FOR = object EVENT = ondragover>
|
Internet Explorer only |
当被拖拽元素处于一个列表中,它和即将被替换的元素都被绑定 ondragover 事件。
<ul>
<li onDragOver={e => console.log(e.target)}>被拖拽元素</li>
<li onDragOver={e => console.log(e.target)}>被替换元素</li>
</ul>
当被拖拽元素在拖拽过程中进入到被替换元素的对象容器范围内,触发的是被替换元素的
e.dataTransfer
在做拖拽的时候,我在 onDragStart 阶段使用
_.dataTransfer.setData(format, {
'id': _.target?.id || _.target?.getAttribute('data-id'),
format,
'class': className,
'listClass': format === 'group'
? 'groupArtboardList'
: void 0
});
设置一些参数,以期待在 onDragOver 阶段拿出来使用,但实际取出来的却是空值。
模板:Leftrightflex
随后查阅,有人的解释是说 dataTransfer 的存储机制是在专为 drag 设置的数据存储 stroe 有读取限制,根据 w3c 关于 drag data store 相关的资料记录[1]、[2]
The information in a drag data store used to generate the UI feedback during the drag consists of: A list of zero or more elements. Optionally, a bitmap image and the coordinate of a point within that image. A drag data store is created at the start of a drag-and-drop operation in the read/write mode, and then progresses through the following three modes as the operation proceeds: Read/write mode At the start of the drag. New data can be added to the drag data store. Protected mode During the drag. The formats and kinds in the drag data store list of items representing dragged data can be enumerated, but the data itself is unavailable and no new data can be added. Read-only mode After the drop. The list of items representing dragged data can be read, including the data. No new data can be added.
示例
drag.js
import { decideNumberEquivalence } from '../../utils/number';
/**
* @function getNodeIndex
* @description 获取节点索引
* @param {Event} _ -
* @returns {number}
*/
const getNodeIndexFromParentNode = _ => {
let index = 0;
if (!_ || !_.parentNode) return -1;
// previousElementSibling 属性返回指定元素的前一个兄弟元素
// eslint-disable-next-line no-cond-assign
while (_ && (_ = _.previousElementSibling)) index++;
return index;
};
/**
* @function toDragStart
* @param {Event} _ - 拖拽事件
* @param {string} format -
*/
export const toDragStart = (_, format, cb) => {
_.stopPropagation();
_.dataTransfer.setData(format, _.target?.getAttribute('data-id'));
// 被拖拉节点的背景色变透明
_.target.style.opacity = 0.5;
cb(_.target);
};
/**
* @function toDragEnter
* @param {*} _ -
*/
export const toDragEnter = _ => {
_.stopPropagation();
// _.preventDefault();
};
/**
* @function toDragEnd
* @param {Event} _ -
* @returns -
*/
export const toDragEnd = (_, info, cb) => {
_.stopPropagation();
_.target.style.opacity = 1;
// vdom dom,vdom 更新之前将元素重新插回去
if (info?.parentNode !== info?.node?.parentNode) info?.parentNode.appendChild(info?.node);
cb();
};
/**
* @description 向上找三层父级元素
* @param {Element} parents 元素
* @param {number} _.id
* @param {string} _.class
* @param {string} _.parentClass
* @param {number} count 计数
* @returns {Element|undefined}
*/
function loopSetTargetNode (parents, _, count) {
if (!count || !parents) return parents;
if (parents?.classList.contains(_?.class)
|| (_?.parentClass
&& parents?.classList.contains(_?.parentClass)
&& !decideNumberEquivalence(parents?.getAttribute('data-id'), _?.id))) return parents;
return loopSetTargetNode(parents?.parentNode, _, count - 1);
}
/**
* @function toDragOver
* @description -
* @param {Event} _ -
* @param {string} info.nodeName - 节点标签名称
* @param {Element} info.node - 被拖拽的节点
* @param {toDragOver~callback} cb - 回调函数
* @returns {null}
*/
/**
* @callback toDragOver~callback
* @param {Element|string} target -
* 1. 目标元素
* 2. 目标元素是未打开的父级元素时,父级元素的 id
*/
export const toDragOver = (_, info, cb) => {
_.preventDefault();
// const currentTypeId = event.dataTransfer.getData(format)
let { target } = _;
target = loopSetTargetNode(target, info, 4) || target;
if (!info?.node || target.nodeName !== info?.nodeName || target === info?.node) return;
// 分组间
if (info?.parentClass
&& target?.classList.contains(info?.parentClass)
&& !decideNumberEquivalence(target?.getAttribute('data-id'), info?.id)) {
const listNodeBox = target?.getElementsByClassName(info?.listClass);
// 父级标签没有打开的情况下,只计数不插入
if (!listNodeBox?.length) return cb(target?.getAttribute('data-id'));
/*
* 销毁被拖动元素自身
* !listNodeBox?.length && setTimeout(() => {
* info?.node?.remove();
* }, 1);
*/
// info?.node?.parentNode?.removeChild(info?.node);
const lastChildNode = listNodeBox[0]?.lastChild;
listNodeBox[0]?.appendChild(info?.node);
return cb(lastChildNode);
}
// 存在 info?.parentClass 表明被拖动的是子元素
if (info?.parentClass && target?.classList.contains(info?.parentClass)) return;
if (!target?.classList.contains(info?.class)) return;
/*
* target.style.transform = 'translate3d(0px, 0px, 0px)';
* target.style.transition = 'transform 200ms ease 0s';
*/
console.log('info?.node', _)
if (getNodeIndexFromParentNode(info?.node) < getNodeIndexFromParentNode(target)) target.parentNode.insertBefore(info?.node, target.nextSibling);
else target.parentNode.insertBefore(info?.node, target);
cb(target);
};
- ↑ Ian Hickson, Google, Inc. HTML5 A vocabulary and associated APIs for HTML and XHTML Editor's Draft 25 October 2010[OL],2010:10-25. https://www.w3.org/html/wg/spec/dnd.html#the-dragevent-and-datatransfer-interfaces.
- ↑ Ian Hickson, Google, Inc. HTML5: Edition for Web Authors W3C Working Draft 09 August 2011[OL],2011:8-9. https://www.w3.org/TR/2011/WD-html5-author-20110809/dnd.html#the-drag-data-store.
No categories