vue 两种拖拽插件及实现

22 篇文章 0 订阅

vuedraggable


vuedraggable 是标准的组件式封装,并且将可拖动元素放进了 transition-group 上面,过渡动画都比较好。

vuedraggable 拖动后可以在updated中监听

vuedraggable 的拖动规则是:拖动元素后到新位置,后面元素依次退一个位置,如:
在这里插入图片描述
安装 vuedraggable :

npm install vuedraggable  --save

在页面引用

import vuedraggable from 'vuedraggable';
 components: {vuedraggable},

Awe-dnd


awe-dnd 和 vuedraggable的不同在于,不是直接双向绑定的列表元素,自己提供了事件,在拖拽结束的时候用来更新列表(不需要手动更新列表,其实内部是实现了双向绑定的)或者是去触发父组件监听的事件。

awe-dnd 拖动后在mounted中this.$dragging.$on监听

awe-dnd拖动规则:只能直接拖动间隔一个位置的两个元素;超过一个位置,用两个中间元素作为桥梁交换位置
例如:
在这里插入图片描述
安装awe-dnd

npm install awe-dnd --save

在main.js 引入

import VueDND from 'awe-dnd'
Vue.use(VueDND)

细心的我提供了源码

VuedraggablePage .vue
<template>
  <el-container>
      <el-row>
        <el-col :span="6">
          <div class="wrapper-text">
            <transition-group>
              <div v-for="item in list" :key="item.text" class="item">
                <p :class="item.text">{{item.text}}</p>
              </div>
            </transition-group>
          </div>
        </el-col>
        <el-col :span="18">
          <vuedraggable class="wrapper" v-model="list">
            <transition-group>
              <div v-for="item in list" :key="item.text" class="item">
                <p :class="item.text">{{item.text}}</p>
              </div>
            </transition-group>
          </vuedraggable>
        </el-col>
      </el-row>
  </el-container>

</template>

<script>
import vuedraggable from 'vuedraggable';

export default {
  name: 'Vuedraggable',
  components: {vuedraggable},
  props: {
  },
  data() {
    return {
      list: [
        {
          text: 'Aquamarine'
        }, {
          text: 'Hotpink'
        }, {
          text: 'Gold'
        }, {
          text: 'Crimson'
        }, {
          text: 'Blueviolet'
        }, {
          text: 'Lightblue'
        }, {
          text: 'Cornflowerblue'
        }, {
          text: 'Skyblue'
        }, {
          text: 'Burlywood'
        }
      ]
    }
  },
  updated() {
    console.log(this.list)
  },
  methods: {
  }
}
</script>
<style scoped lang="scss">
.wrapper {
  margin: 30px;
  display: flex;
  justify-content: center;
  width: 100%;
  .item{
    font-size: 16px;
    font-weight: bold;
    width: 160px;
    height: 90px;
    margin: 10px;
    float: left;
    display: flex;
    justify-content: center;
    align-items: center;
    border: 1px slategrey solid;
  }
}
.wrapper-text {
  padding: 30px;
  .item {
    font-size: 20px;
    font-weight: bold;
    line-height: 40px;
  }

}

@each $color in Aquamarine, Hotpink, Gold, Crimson, Blueviolet, Lightblue, Cornflowerblue, Skyblue, Burlywood {
  .#{$color} {
    color: #{$color};
  }
}
.v-enter,
.v-leave-to {
  opacity: 0;
  transform: translateY(100px);
}

.v-enter-active,
.v-leave-active {
  transition: all 1s ease;
}

.v-move {
  transition: all 1s;
}

.v-leave-active {
  position: absolute;
}
</style>

AweDndPage.vue
<template>
  <el-container>
    <el-row>
      <el-col :span="6">
        <div class="color-list-text">
          <div
              class="color-item"
              v-for="color in colors"
              :key="color.text">
            <div :class="color.text">
              {{color.text}}
            </div>
          </div>
        </div>
      </el-col>
      <el-col :span="18">
        <div class="color-list">
          <div
              class="color-item"
              v-for="color in colors" v-dragging="{ item: color, list: colors, group: 'color' }"
              :key="color.text">
            <div :class="color.text">
              {{color.text}}
            </div>
          </div>
        </div>
      </el-col>
    </el-row>

  </el-container>
</template>

<script>
export default {
  name: 'AweDndPage',
  data () {
    return {
      colors: [
        {
          text: 'Aquamarine'
        }, {
          text: 'Hotpink'
        }, {
          text: 'Gold'
        }, {
          text: 'Crimson'
        }, {
          text: 'Blueviolet'
        }, {
          text: 'Lightblue'
        }, {
          text: 'Cornflowerblue'
        }, {
          text: 'Skyblue'
        }, {
          text: 'Burlywood'
        }]
    }
  },
  mounted () {
    this.$dragging.$on('dragged', ({value}) => {
      console.log(value.list)
    })
    this.$dragging.$on('dragend', (res) => {
      console.error(res)
    })
  },
  methods: {},
}
</script>

<style lang="scss" scoped>
.color-list {
  padding: 30px;

  .color-item {
    font-size: 16px;
    font-weight: bold;
    height: 90px;
    width: 160px;
    float: left;
    border: 1px slategrey solid;
    margin: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
  }

}

.color-list-text {
  padding: 30px;

  .color-item {
    font-size: 20px;
    font-weight: bold;
    line-height: 40px;
  }

}

@each $color in Aquamarine, Hotpink, Gold, Crimson, Blueviolet, Lightblue, Cornflowerblue, Skyblue, Burlywood {
  .#{$color} {
    color: #{$color};
  }
}
</style>

  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
Vue.js是一个流行的JavaScript框架,可以轻松地创建动态的Web应用程序。它被广泛用于构建单页应用程序。在现代Web应用程序中,多语言功能是不可或缺的。多语言功能可以帮助您的网站吸引更多的国际用户,因为它可以让您的网站在不同的语言中呈现。 在Vue.js中,有许多多语言插件可供选择。其中一些最流行的包括vue-i18n、vuex-i18n和vue-multilanguage。在这篇文章中,我们将介绍这些插件的基本用法和如何在Vue.js应用程序中使用它们。 1. vue-i18n vue-i18n是Vue.js中最流行的多语言插件之一。它提供了易于使用的API,可以在应用程序中轻松添加多语言支持。它支持复数形式、日期格式和时间格式等常见的多语言特性。 首先,您需要通过npm安装vue-i18n插件。在终端中输入以下命令: ``` npm install vue-i18n ``` 然后,在您的Vue.js应用程序中添加以下代码: ``` import Vue from 'vue' import VueI18n from 'vue-i18n' Vue.use(VueI18n) const messages = { en: { message: { hello: 'Hello world!' } }, fr: { message: { hello: 'Bonjour le monde!' } } } const i18n = new VueI18n({ locale: 'en', // set locale messages, // set locale messages }) new Vue({ i18n, render: h => h(App) }).$mount('#app') ``` 接下来,您可以在Vue.js组件中使用以下代码来访问多语言翻译: ``` <template> <div>{{ $t("message.hello") }}</div> </template> ``` 这将返回“Hello world!”或“Bonjour le monde!”,具体取决于当前的语言设置。 2. vuex-i18n vuex-i18n是基于Vue.js和Vuex的多语言插件。它提供了一个易于使用的API,可以在应用程序中轻松添加多语言支持。它支持复数形式、日期格式和时间格式等常见的多语言特性。 首先,您需要通过npm安装vuex-i18n插件。在终端中输入以下命令: ``` npm install vuex-i18n ``` 然后,在您的Vue.js应用程序中添加以下代码: ``` import Vue from 'vue' import Vuex from 'vuex' import vuexI18n from 'vuex-i18n' Vue.use(Vuex) const store = new Vuex.Store() Vue.use(vuexI18n.plugin, store) const translationsEn = { hello: 'Hello world!' } const translationsFr = { hello: 'Bonjour le monde!' } Vue.i18n.add('en', translationsEn) Vue.i18n.add('fr', translationsFr) Vue.i18n.set('en') new Vue({ store, render: h => h(App) }).$mount('#app') ``` 接下来,您可以在Vue.js组件中使用以下代码来访问多语言翻译: ``` <template> <div>{{ $t("hello") }}</div> </template> ``` 这将返回“Hello world!”或“Bonjour le monde!”,具体取决于当前的语言设置。 3. vue-multilanguage vue-multilanguage是一个轻量级的多语言插件,它提供了易于使用的API,可以在应用程序中轻松添加多语言支持。它支持复数形式、日期格式和时间格式等常见的多语言特性。 首先,您需要通过npm安装vue-multilanguage插件。在终端中输入以下命令: ``` npm install vue-multilanguage ``` 然后,在您的Vue.js应用程序中添加以下代码: ``` import Vue from 'vue' import Multilanguage from 'vue-multilanguage' Vue.use(Multilanguage, { default: 'en', en: { hello: 'Hello world!' }, fr: { hello: 'Bonjour le monde!' } }) new Vue({ render: h => h(App) }).$mount('#app') ``` 接下来,您可以在Vue.js组件中使用以下代码来访问多语言翻译: ``` <template> <div>{{ $ml.hello }}</div> </template> ``` 这将返回“Hello world!”或“Bonjour le monde!”,具体取决于当前的语言设置。 以上是三种Vue.js多语言插件的基本用法和示例。根据您的应用程序需要和个人喜好,您可以选择其中任何一种多语言插件实现多语言功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值