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

vue實現(xiàn)div高度可拖拽

 更新時間:2021年10月27日 11:46:28   作者:秦浩鋮  
這篇文章主要為大家詳細介紹了vue實現(xiàn)div高度可拖拽,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了vue實現(xiàn)div高度可拖拽的具體代碼,供大家參考,具體內(nèi)容如下

這里有一個現(xiàn)成的demo,可以實現(xiàn)頁面div的拖拽功能,但是和我想要的效果不是很一樣,所以說后邊有根據(jù)我的實際需求又重新修改了一下,先看一下現(xiàn)在的demo效果。

<template>
  <div id="eagleMapContainer" style="border: 1px solid red;overflow-y: auto;" title="">
    <div id="tz" @mousedown="dragEagle" style="border: 1px solid blue;">
      <div title="拖動調(diào)整大小" id="move_tz" style="border: 1px solid green;"></div>
    </div>
  </div>
</template>

<script>
  export default {
    name: "eagleMap",
    data() {
      return {}
    },
    methods: {
      dragEagle: function (e) {
        var targetDiv = document.getElementById('eagleMapContainer'); 
        //得到點擊時該地圖容器的寬高:
        var targetDivHeight = targetDiv.offsetHeight;
        var startX = e.clientX;
        var startY = e.clientY;
        var _this = this;
        document.onmousemove = function (e) {
          e.preventDefault();
          //得到鼠標(biāo)拖動的寬高距離:取絕對值
          var distX = Math.abs(e.clientX - startX);
          var distY = Math.abs(e.clientY - startY);
          //往上方拖動:
          if (e.clientY < startY) {
            targetDiv.style.height = targetDivHeight + distY + 'px';
          }
          //往下方拖動:
          if (e.clientX < startX && e.clientY > startY) {
            targetDiv.style.height = (targetDivHeight - distY) + 'px';
          }
          if (parseInt(targetDiv.style.height) >= 300) {
            targetDiv.style.height = 300 + 'px';
          }
          if (parseInt(targetDiv.style.height) <= 150) {
            targetDiv.style.height = 150 + 'px';
          }
        }
        document.onmouseup = function () {
          document.onmousemove = null;
        }
      }
    },
  };
</script>

<style scoped>
  #eagleMapContainer {
    position: absolute;
    left: 13%;
    bottom: 10px;
    z-index: 200;
    overflow: hidden;
    visibility: visible;
    width: 200px;
    height: 200px;
  }

  #tz {
    position: absolute;
    right: 1px;
    top: 1px;
    width: 27px;
    height: 20px;
    cursor: ne-resize;
    z-index: 200001;
    background-image: url("");

  }

  #tz:hover {
    background-color: #666;
  }

  #move_tz {
    position: absolute;
    right: 0px;
    top: 0px;
    width: 27px;
    height: 20px;
    cursor: ne-resize;
    z-index: 100;
    background-image: url("");
    background-position: 0px 0px;
  }
</style>

但是這個效果和我想要的不是很一樣,所以得稍微改造了一下。

我想要效果是: 我有一個div,里面包含了很多小方塊列表,因為超出設(shè)置了超出滾動,所以是在有滾動條的div上添加實現(xiàn)高度變化的拖拽。

接下來就是改造一下上邊的demo,簡單點,直接上代碼:

在上邊需要拖拽的div下面添加一個div,就是點到這個div開始實現(xiàn)拖拽功能。

<!-- 拖拉拽的小框 -->
    <div id="tz" @mousedown="dragEagle">
      <div title="拖動調(diào)整大小" id="move_tz"></div>
    </div>

需要根據(jù)拖拽實現(xiàn)高度變化的div設(shè)置一個id,假設(shè)為 “fuDiv”,然后編寫方法。

// 拖拉
      dragEagle(e) {
        var targetDiv = document.getElementById('fuDiv');
        //得到點擊時該地圖容器的寬高:
        var targetDivHeight = targetDiv.offsetHeight;
        var startX = e.clientX;
        var startY = e.clientY;
        var _this = this;
        document.onmousemove = function (e) {
          e.preventDefault();
          //得到鼠標(biāo)拖動的寬高距離:取絕對值
          var distY = Math.abs(e.clientY - startY);

          //往上方拖動:
          if (e.clientY < startY) {
            targetDiv.style.height = targetDivHeight - distY + 'px';
          }
          //往下方拖動:
          if (e.clientX < startX && e.clientY > startY) {
            targetDiv.style.height = (targetDivHeight + distY) + 'px';
          }
          if (parseInt(targetDiv.style.height) >= 320) {
            targetDiv.style.height = 320 + 'px';
          }
          if (parseInt(targetDiv.style.height) <= 160) {
            targetDiv.style.height = 160 + 'px';
          }
        }
        document.onmouseup = function () {
          document.onmousemove = null;
        }
      },

然后給他們設(shè)置一下css樣式,其實這個地方就隨意了,根據(jù)自己喜好來。

  #tz {
    width: 100%;
    height: 5px;
    cursor: s-resize;
    z-index: 200001;
  }
  
  #move_tz {
    width: 100%;
    height: 5px;
    cursor: s-resize;
    z-index: 100;
    background-image: url("");
    background-position: 0px 0px;
  }

最后效果:

效果不是特別的好,還有很多地方是值得優(yōu)化以下的,暫時不寫了。

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

相關(guān)文章

最新評論