简体   繁体   English

使用HTML5拖放时如何修改拖动效果?

[英]How to modify the dragging effect when using HTML5 drag-and-drop?

I am using angularJS for my web appliaction, and I need to write a component that contains dragging operation. 我正在为我的Web应用程序使用angularJS,我需要编写一个包含拖动操作的组件。 When I tried to accomplish the goal, I found it hard to control the dragging effect around the pointer, since it was not a real element but an image cut (I assume). 当我尝试实现目标时,我发现很难控制指针周围的拖动效果,因为它不是真正的元素,而是图像剪切(我认为)。 For now the dragging image is a copy of the element being drag and contains the hover effect, and even have white background from its parent. 目前,拖动图像是被拖动元素的副本,并包含悬停效果,甚至具有其父元素的白色背景。 (I cut two image and display below) (我剪切了两个图像并在下面显示)

So how can I modify the dragging effct with css or js? 那么,如何使用CSS或JS修改拖动效果?

拖动前img

拖动img

I add a class "is-dragging" for the element being dragged when dragstart event is trigger, and change the style of the element being drag. 我在触发dragstart事件时为被拖动的元素添加了一个“正在拖动”类,并更改了被拖动元素的样式。 But the dragging image just remain the same. 但是拖动的图像保持不变。

html HTML

<div ng-if="current == -1" class="form-library">
    <div ng-repeat="(key, item) in formMap"
         class="form-type-items"
         ng-class="{'is-dragging': dragInfo.type == 'add' && key == dragInfo.data.type}"
         draggable="true"
         ng-dragstart="onDragStart($event, key)"
         ng-dragend="onDragEnd($event)">
         <div class="form-type-icon">
             <i class="{{'iconfont icon-'+item.icon}}"></i>
         </div>
         <span class="form-type-name">{{ item.name }}</span>
    </div>
</div>

scss SCSS

&:hover {
    .form-type-name {
        color: $brand-color-1;
    }
    .form-type-icon {
        .iconfont {
            color: $brand-color-1;
        }
    }
}
&.is-dragging {
   opacity: 0.36;
   background-color: transparent;
   .form-type-name {
       color: $gray-2;
   }
   .form-type-icon {
       .iconfont {
           color: $gray-2;
       }
   }
}

It looks like styles are applied after the dragging starts so the dragging visual copy is a shallow copy of the element from the instance when the dragging just started. 看起来好像在拖动开始后就应用了样式,因此,拖动的可视副本是刚开始拖动时来自实例的元素的浅表副本。 As a work around you can try to first apply styles, and then set draggable attribute on the element, and remove it after an element is been released 解决方法是,您可以尝试先应用样式,然后在元素上设置draggable属性,然后在发布元素后将其删除

<div ng-if="current == -1" class="form-library">
    <div ng-repeat="(key, item) in formMap"
            class="form-type-items"
            ng-class="{'is-dragging': dragInfo.type == 'add' && key == dragInfo.data.type}"
            draggable="true"
            on-mousedown="onMouseDown($event, key)"
            on-mouseup="onMouseUp($event)">
            <div class="form-type-icon">
                <i class="{{'iconfont icon-'+item.icon}}"></i>
            </div>
            <span class="form-type-name">{{ item.name }}</span>
    </div>
</div>


<script>
function onMouseDown(e) {
    // set variable that will apply necessary class to TRUE

    // then add draggable attribute
    e.target.setAttribute('draggable', true)
}


function onMouseUp(e) {
    // set variable for drabble class to FALSE

    // then remove draggable attribute
    e.target.setAttribute('draggable', false)
}

</script>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM