亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

vue.js實(shí)現(xiàn)可拖拽菜單的方法:【import "@/assets/second.css";export default {name: "HelloWorld",directives: {move(el, bindings) {...】。


vue.js如何實(shí)現(xiàn)可拖拽菜單


在給出正式的實(shí)現(xiàn)代碼之前,我們要先來了解一點(diǎn)相關(guān)知識點(diǎn)。


知識點(diǎn)一:

vue中的自定義指令 directive

// 注冊一個全局自定義指令 `v-focus`
Vue.directive('focus', {
  // 當(dāng)被綁定的元素插入到 DOM 中時……
  inserted: function (el) {
    // 聚焦元素
    el.focus()
  }
})
 
// 注冊局部自定義指令
 
directives: {
  focus: {
    // 指令的定義
    inserted: function (el) {
      el.focus()
    }
  }
}
// 在此我們用的是局部

知識點(diǎn)二:js

onmousedown             :鼠標(biāo)按下事件
clientX                 :時鼠標(biāo)指針相對于瀏覽器頁面(或客戶區(qū))的水平坐標(biāo)
document.getElementById :通過id獲取節(jié)點(diǎn)
offsetWidth             :獲取的是盒子最終的寬
onmousemove             :鼠標(biāo)移動事件 
onmouseup               :鼠標(biāo)釋放事件

效果圖:


vue.js如何實(shí)現(xiàn)可拖拽菜單


vue.js如何實(shí)現(xiàn)可拖拽菜單


頁面代碼:

<template>
  <el-container>
    <el-main>
      <div class="myBox">
        <div id="silderLeft">
          <div class="menuList">側(cè)欄菜單區(qū)</div>
          <div class="moveBtn" v-move></div>
        </div>
        <div class="silderRight">右邊自適應(yīng)大小,黃色的為拖拽的按鈕</div>
      </div>
    </el-main>
  </el-container>
</template>
 
<script>
import "@/assets/second.css";
export default {
  name: "HelloWorld",
  directives: {
    move(el, bindings) {
      el.onmousedown = function(e) {
        var init = e.clientX;
        var parent = document.getElementById("silderLeft");
        var initWidth = parent.offsetWidth;
        document.onmousemove = function(e) {
          var end = e.clientX;
          var newWidth = end - init + initWidth;
          parent.style.width = newWidth + "px";
        };
        document.onmouseup = function() {
          document.onmousemove = document.onmouseup = null;
        };
      };
    }
  },
  data() {
    return {
      msg: "我是第二頁"
    };
  },
  methods: {},
  mounted() {},
  created() {},
  updated() {}
};
</script>
 
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>

樣式代碼:

.myBox{
    width: 100%;;
    height: 700px;
    border: 1px solid red;
    display: flex;
}
#silderLeft{
    width: 250px;
    height: 100%;    
    background-color: #999;
    position: relative;
    /* overflow-y: auto; */
}
/* 拖動條 */
.moveBtn{
    height: 100%;
    width: 10px;
    /* opacity: 0; */
    position: absolute;
    right: 0px;
    top: 0;
    cursor: col-resize;
    background-color: yellow;
}
.menuList{
    background-color: yellowgreen;
    /* height: 120%; */
}
.silderRight{
    height: 100%;
    background-color: sandybrown;
    flex: 1;
}

可以修改自定義命令,設(shè)置一個最小拖拽寬度

directives: {
  move(el, bindings) {
    el.onmousedown = function(e) {
      var init = e.clientX;
      console.log('init',init);
      var parent = document.getElementById("sidebar");
      var initWidth = parent.offsetWidth;
      document.onmousemove = function(e) {
        var end = e.clientX;
        // end - init表示鼠標(biāo)移動的距離
        // end為鼠標(biāo)移動的寬度,可用于設(shè)置最小寬度
        if(end > 250){
          var newWidth = end - init + initWidth;
          parent.style.width = newWidth + "px";
        }else{
          end = 250;
          // 最小寬度242
          parent.style.width = 242 + "px";
        }
      };
      document.onmouseup = function() {
        document.onmousemove = document.onmouseup = null;
      };
    };
  }
}



分享到:
標(biāo)簽:vue.js菜單 可拖拽菜單
用戶無頭像

網(wǎng)友整理

注冊時間:

網(wǎng)站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運(yùn)動步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績評定2018-06-03

通用課目體育訓(xùn)練成績評定