欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue實現(xiàn)可拖拽組件的方法

 更新時間:2021年09月15日 11:30:43   作者:從不甘到不凡  
這篇文章主要為大家詳細(xì)介紹了Vue實現(xiàn)可拖拽組件的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文為大家分享了Vue實現(xiàn)可拖拽、拖拽組件,供大家參考,具體內(nèi)容如下

描述:

組件僅封裝拖拽功能,內(nèi)容通過#header、#default、#footer插槽 自定義

效果: 

代碼:

<template>
  <div
    ref="wrapper"
    class="drag-bar-wrapper"
  >
    <div
      ref="header"
      class="drag-bar-header"
    >
      <!-- 頭部區(qū)域 -->
      <slot name="header" />
    </div>
    <div class="drag-bar-content">
      <!-- 主內(nèi)容區(qū)域 -->
      <slot name="default" />
    </div>
    <div class="drag-bar-footer">
      <!-- 底部區(qū)域 -->
      <slot name="footer" />
    </div>
  </div>
</template>
 
<script>
export default {
  data() {
    return {
      wrapperDom: null,
      headerDom: null,
 
      disX: 0,
      disY: 0,
 
      minLeft: 0,
      maxLeft: 0,
 
      minTop: 0,
      maxTop: 0,
 
      prevLeft: 0,
      prevTop: 0,
    };
  },
  methods: {
    initDrag() {
      this.wrapperDom = this.$refs.wrapper;
      this.headerDom = this.$refs.header;
      this.headerDom.addEventListener('mousedown', this.onMousedown, false);//點擊頭部區(qū)域拖拽
    },
    onMousedown(e) {
      this.disX = e.clientX - this.headerDom.offsetLeft;
      this.disY = e.clientY - this.headerDom.offsetTop;
 
      this.minLeft = this.wrapperDom.offsetLeft;
      this.minTop = this.wrapperDom.offsetTop;
 
      this.maxLeft =
        window.innerWidth - this.minLeft - this.wrapperDom.offsetWidth;
      this.maxTop =
        window.innerHeight - this.minTop - this.wrapperDom.offsetHeight;
 
      const { left, top } = getComputedStyle(this.wrapperDom, false);
      this.prevLeft = parseFloat(left);
      this.prevTop = parseFloat(top);
 
      document.addEventListener('mousemove', this.onMousemove, false);
      document.addEventListener('mouseup', this.onMouseup, false);
      document.body.style.userSelect = 'none'; //消除拖拽中選中文本干擾
    },
    onMousemove(e) {
      let left = e.clientX - this.disX;
      let top = e.clientY - this.disY;
 
      if (-left > this.minLeft) {
        left = -this.minLeft;
      } else if (left > this.maxLeft) {
        left = this.maxLeft;
      }
 
      if (-top > this.minTop) {
        top = -this.minTop;
      } else if (top > this.maxTop) {
        top = this.maxTop;
      }
 
      this.wrapperDom.style.left = this.prevLeft + left + 'px';
      this.wrapperDom.style.top = this.prevTop + top + 'px';
    },
    onMouseup() {
      document.removeEventListener('mousemove', this.onMousemove, false);
      document.removeEventListener('mouseup', this.onMouseup, false);
      document.body.style.userSelect = 'auto'; //恢復(fù)文本可選中
    },
  },
  mounted() {
    this.initDrag();
  }
};
</script>
 
<style scoped>
.drag-bar-wrapper {
  position: fixed;
  z-index: 2;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  flex-direction: column;
}
.drag-bar-header {
  background-color: #eee;
  cursor: move; /*拖拽鼠標(biāo)樣式*/
}
.drag-bar-content {
  background-color: #fff;
}
.drag-bar-footer {
  background-color: #fff;
}
</style>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 解讀vue分頁面打包方式

    解讀vue分頁面打包方式

    這篇文章主要介紹了解讀vue分頁面打包方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • vue router demo詳解

    vue router demo詳解

    這篇文章主要為大家詳細(xì)介紹了vue router demo演示代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • vue單文件組件無法獲取$refs的問題

    vue單文件組件無法獲取$refs的問題

    這篇文章主要介紹了vue單文件組件無法獲取$refs的問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • Vue中使用Scss實現(xiàn)配置、切換主題方式

    Vue中使用Scss實現(xiàn)配置、切換主題方式

    這篇文章主要介紹了Vue中使用Scss實現(xiàn)配置、切換主題方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • 尤大大新活petite-vue的實現(xiàn)

    尤大大新活petite-vue的實現(xiàn)

    打開尤大大的GitHub,發(fā)現(xiàn)多了個叫 petite-vue 的東西,Vue3 和 Vite 還沒學(xué)完呢,又開始整新東西了?本文就來介紹一下
    2021-07-07
  • vue.js自定義組件directives的實例代碼

    vue.js自定義組件directives的實例代碼

    這篇文章主要介紹了vue.js自定義組件directives的實例代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-11-11
  • 詳解vue.js+UEditor集成 [前后端分離項目]

    詳解vue.js+UEditor集成 [前后端分離項目]

    本篇文章主要介紹了詳解vue.js+UEditor集成 [前后端分離項目] ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • vue項目nginx二級域名配置方式

    vue項目nginx二級域名配置方式

    這篇文章主要介紹了vue項目nginx二級域名配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • 使用Webpack提高Vue.js應(yīng)用的方式匯總(四種)

    使用Webpack提高Vue.js應(yīng)用的方式匯總(四種)

    Webpack是開發(fā)Vue.js單頁應(yīng)用程序的重要工具。下面通過四種方式給大家介紹使用Webpack提高Vue.js應(yīng)用,需要的的朋友參考下吧
    2017-07-07
  • vue中axios處理http發(fā)送請求的示例(Post和get)

    vue中axios處理http發(fā)送請求的示例(Post和get)

    本篇文章主要介紹了vue中axios處理http請求的示例(Post和get),這里整理了詳細(xì)的代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10

最新評論