【问题标题】:How can the ondragend be simulated when the source DOM element is removed?当源 DOM 元素被移除时,如何模拟 ondragend?
【发布时间】:2016-10-22 01:35:35
【问题描述】:

大家好

项目背景

在基于 HTML5 类型脚本的 Angular2 项目中,我们遇到了拖放问题,但此问题可能是由于 DOM 操作问题造成的。在我们的 Web 应用程序中,我们有一个页面,其中需要重新排序组件组并在设计器中的组之间切换。

这个“设计器”输出 HTML 的结构如下:

<div>
    <div class="col1">
        <div class="group1"></div>
        <div class="group2"></div>
    </div>
    <div class="col2">
        <div class="group3"></div>
        <div class="group4"></div>
    </div>
</div>

每个“组”都定义了一个dragstart、dragover 和dragend,它们处理共享dragGroup 的状态。

精简代码

public DragStart(source: Group): void
{
    this.dragGroup = source;
}

public DragOver(source: Group): void
{
    // Reorders the groups appropriately
    // Changes the state of the model, such that angular2 will render the DOM differently
}

public DragEnd(): void
{
    this.dragGroup = null;
}

问题

我们已将问题范围缩小到何时将被拖动的组从 DOM 中删除,最重要的一点是移到另一个组中。

触发拖尾的示例:group1 和 group2 交换

拖动端未触发的示例:将 group1 移动到 col2

查询

  • 为什么第一个示例有效/为什么同一组中的重新排序有效?
  • 是否有“最佳实践”方式可以改变工作流程以获得我想要的结果?
  • 或者有什么厚颜无耻的 hack 可以用来模拟丢失的 dragend?

提前谢谢你。如果需要更多信息,请询问。

【问题讨论】:

    标签: javascript html dom typescript angular


    【解决方案1】:

    好的,

    我自己制定了一个“可怕”的解决方案。一旦有人有更好的解决方案,我会接受他们的回答。

    在最外面的 div(包含所有列的那个)上,我注册了 3 个事件来帮助确定鼠标在拖动过程中的状态:mousemove, mousedown and mouseup。

    以下 Angular2 模板 HTML 说明了解决方案。在普通 HTML 中,它不会在事件周围加上方括号,并且会在每个事件名称前加上“on”前缀。

    <div (mousemove)="CheckIfDragEnded()"
         (mousedown)="SetMouseDown(true)"
         (mouseup)="SetMouseDown(false)">
        <div class="col1">
            <div class="group1"></div>
            <div class="group2"></div>
        </div>
        <div class="col2">
            <div class="group3"></div>
            <div class="group4"></div>
        </div>
    </div>
    

    下面是 TypeScript(同样,这可以很容易地用 JavaScript 编写)

    // Some storage state variable
    private mouseDown: boolean = false;
    
    private SetMouseDown(down: boolean): void
    {
        this.mouseDown = down;
    }
    
    private CheckIfDragEnded(): void
    {
        if (this.mouseDown)
        {
            // Call the drag end method here that should have been triggered by the drag end normally
        }
    }
    

    一个小的补充是检查鼠标按钮也是左键。

    【讨论】:

      猜你喜欢
      • 2022-11-29
      • 2012-02-08
      • 1970-01-01
      • 1970-01-01
      • 2016-01-23
      • 2018-02-03
      • 1970-01-01
      • 2014-05-04
      • 1970-01-01
      相关资源
      最近更新 更多