JavaScript/drag:修订间差异
来自康健生活
小 文字替换 -“{{leftrightflex|”替换为“{{Double-columns|” 标签:ReplaceText 移动版编辑 移动版网页编辑 |
|||
| (未显示同一用户的9个中间版本) | |||
| 第7行: | 第7行: | ||
释放目标时触发的事件: | 释放目标时触发的事件: | ||
* ondragenter - 当被鼠标拖动的对象进入其容器范围内时触发此事件 | * ondragenter - 当被鼠标拖动的对象进入其容器范围内时触发此事件 | ||
* ondragover - | * ondragover - 当用户将对象拖到有效的放置目标上时,连续触发目标元素 | ||
* ondragleave - 当被鼠标拖动的对象离开其容器范围内时触发此事件 | * ondragleave - 当被鼠标拖动的对象离开其容器范围内时触发此事件 | ||
* ondrop - 在一个拖动过程中,释放鼠标键时触发此事件 | * ondrop - 在一个拖动过程中,释放鼠标键时触发此事件 | ||
| 第13行: | 第13行: | ||
== 事件 == | == 事件 == | ||
=== ondragover === | === ondragover === | ||
Syntax | |||
{| class="wikitable" | |||
|- | |||
| Inline HTML || <syntaxhighlight lang="html5"> | |||
<ELEMENT ondragover = "handler" ... ></syntaxhighlight> || All platforms | |||
|- | |||
| Event property || <syntaxhighlight lang="javascript"> | |||
object.ondragover = handler</syntaxhighlight> || js | |||
|- | |||
| Named script || <syntaxhighlight lang="html5"> | |||
<SCRIPT FOR = object EVENT = ondragover></syntaxhighlight> || Internet Explorer only | |||
|} | |||
当被拖拽元素处于一个列表中,它和即将被替换的元素都被绑定 ondragover 事件。 | 当被拖拽元素处于一个列表中,它和即将被替换的元素都被绑定 ondragover 事件。 | ||
<syntaxhighlight lang="html5"> | <syntaxhighlight lang="html5"> | ||
| 第35行: | 第49行: | ||
设置一些参数,以期待在 <code>onDragOver</code> 阶段拿出来使用,但实际取出来的却是空值。 | 设置一些参数,以期待在 <code>onDragOver</code> 阶段拿出来使用,但实际取出来的却是空值。 | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript"> | ||
_.dataTransfer.getData(format);</syntaxhighlight>|{{consoleout|<pre>'' | _.dataTransfer.getData(format);</syntaxhighlight>|{{consoleout|<pre>'' | ||
</pre>}}}} | </pre>}}}} | ||
随后查阅,有人的解释是说 dataTransfer 的存储机制是在专为 drag 设置的数据存储 stroe 有读取限制,根据 [https://www.w3.org/html/wg/spec/dnd.html#the-dragevent-and-datatransfer-interfaces | 随后查阅,有人的解释是说 dataTransfer 的存储机制是在专为 drag 设置的数据存储 stroe 有读取限制,根据 w3c 关于 <code>drag data store</code> 相关的资料记录<ref>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.</ref>、<ref>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.</ref> | ||
<pre> | <pre> | ||
| 第56行: | 第70行: | ||
Read-only mode | 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.</pre> | After the drop. The list of items representing dragged data can be read, including the data. No new data can be added.</pre> | ||
== 示例 == | |||
{{hc|head=drag.js|output=<syntaxhighlight lang="javascript"> | |||
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); | |||
}; | |||
</syntaxhighlight>}} | |||
== 参考 == | |||