首页 / VUE / vue实现div高度可拖拽
vue实现div高度可拖拽
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了vue实现div高度可拖拽,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3196字,纯文字阅读大概需要5分钟。
内容图文
vue实现div高度可拖拽
这里有一个现成的demo,可以实现页面div的拖拽功能,但是和我想要的效果不是很一样,所以说后边有根据我的实际需求又重新修改了一下,先看一下现在的demo效果。
<template>
<div title="">
<div @mousedown="dragEagle" >
<div title="拖动调整大小" ></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();
//得到鼠标拖动的宽高距离:取绝对值
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,里面包含了很多小方块列表,因为超出设置了超出滚动,所以是在有滚动条的div上添加实现高度变化的拖拽。
接下来就是改造一下上边的demo,简单点,直接上代码:
在上边需要拖拽的div下面添加一个div,就是点到这个div开始实现拖拽功能。
<!-- 拖拉拽的小框 -->
<div @mousedown="dragEagle">
<div title="拖动调整大小" ></div>
</div>
需要根据拖拽实现高度变化的div设置一个id,假设为 “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();
//得到鼠标拖动的宽高距离:取绝对值
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;
}
},
然后给他们设置一下css样式,其实这个地方就随意了,根据自己喜好来。
#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;
}
最后效果:
效果不是特别的好,还有很多地方是值得优化以下的,暂时不写了。
原文:https://www.cnblogs.com/wjw1014/p/15006052.html
内容总结
以上是互联网集市为您收集整理的vue实现div高度可拖拽全部内容,希望文章能够帮你解决vue实现div高度可拖拽所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。