vue.js - How to programmatically trigger a draggable "move" event by a click in Vue?
问题描述
I have a set of cards that are arranged vertically and are currently draggable. When you drag a card over another, you see this card as a semi-transparent card hovering over the target position, before you let go, the cards underneath swap positions.
I want to know how to trigger that behavior(swap positions/show animation of moving up/down) without dragging eg. click arrows up/down.
To clarify: "trigger event" as in not call the method that responds to the moving event(ending) but make the cards move as if a mouse/finger dragged the cards in a direction(up/down).
For now I have added a position switch that reorders the array but it does not trigger the visual aspect eg. dragging. You just see a flash and then cards are ordered differently.
See visual behavior
Draggable component
<draggable v-model="things" :move="checkPosition" :options={animation:500}">
<div v-for="(thing, index) in things">
<div class="up" @clickSort"('up', index)">
<div class="down" @clickSort"('up', index)">
</div>
</draggable>
Relevant JS methods
watch: {
things: {
// this fires when you sort by dragging, as well as button click
// loop over things
// update vuex
}
}
checkPosition(event) {
if (event.related) {
// code that keeps draggable item pos in scope of existing elements
// some are hidden
return !event.related.classList.contains('locked');
}
}
clickDrag(direction, itemPos) {
// manual array sorting, no draggable event eg. hover/motion/transition
}
解决方案
为了创建这个过渡效果,你必须使用 Vue 提供的 Enter/Leave Transition https://vuejs.org/v2/guide/transitions.html
当您想要触发动画时,您需要定义 Vue 将应用于您的组件的 css 动画样式,例如 ondrop 事件。
推荐阅读
- python - python中的变量%d和+=运算符
- excel - 如果通过 ole 触发 Excel 宏不会重命名数据系列
- r - R中的seasonplot函数不是函数、字符或符号
- android - Firebase 的 exists() 函数行为突然
- go - jsonrpc 服务器接受请求的小写方法名称(用于大写注册服务)
- php - 如何让 4 组 3 个按钮同时保持点击状态?
- c# - Azure 移动服务脱机同步在脱机添加项目时不会脱机删除
- tsql - 我怎样才能简化这个 SQL CASE 语句?
- javascript - Coldfusion 和异步 ajax 请求
- uiimageview - 如何在 swift 4 或更高版本中裁剪具有可选区域的图像?