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

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

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

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

這里有一個(gè)現(xiàn)成的demo,可以實(shí)現(xiàn)頁(yè)面div的拖拽功能,但是和我想要的效果不是很一樣,所以說(shuō)后邊有根據(jù)我的實(shí)際需求又重新修改了一下,先看一下現(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="拖動(dòng)調(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'); 
        //得到點(diǎn)擊時(shí)該地圖容器的寬高:
        var targetDivHeight = targetDiv.offsetHeight;
        var startX = e.clientX;
        var startY = e.clientY;
        var _this = this;
        document.onmousemove = function (e) {
          e.preventDefault();
          //得到鼠標(biāo)拖動(dòng)的寬高距離:取絕對(duì)值
          var distX = Math.abs(e.clientX - startX);
          var distY = Math.abs(e.clientY - startY);
          //往上方拖動(dòng):
          if (e.clientY < startY) {
            targetDiv.style.height = targetDivHeight + distY + 'px';
          }
          //往下方拖動(dòng):
          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>

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

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

接下來(lái)就是改造一下上邊的demo,簡(jiǎn)單點(diǎn),直接上代碼:

在上邊需要拖拽的div下面添加一個(gè)div,就是點(diǎn)到這個(gè)div開(kāi)始實(shí)現(xiàn)拖拽功能。

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

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

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

          //往上方拖動(dòng):
          if (e.clientY < startY) {
            targetDiv.style.height = targetDivHeight - distY + 'px';
          }
          //往下方拖動(dòng):
          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;
        }
      },

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

  #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)化以下的,暫時(shí)不寫(xiě)了。

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

相關(guān)文章

最新評(píng)論