这是别人的例子,然后我尝试用vue写,一直写不出效果来,
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{margin: 0;padding: 0;}
#box{position: absolute;right: 0;width: 200px;height: 100%;background-color: #007AFF;}
#line{width: 2px;height: 100%; background-color: #000;position: absolute; right: 200px;cursor: e-resize;}
</style>
<script type="text/javascript">
window.onload=function(){
var oBox=document.getElementById('box');
var oLine = document.getElementById('line');
var b='';//声明两个空变量a,b;
var a='';
oLine.onmousedown=function(ev){
var iEvent=ev||event;
var dx=iEvent.clientX;//当你第一次单击的时候,存储x轴的坐标。
var dy=iEvent.clientY;//当你第一次单击的时候,储存Y轴的坐标。
var dw=oBox.offsetWidth;//存储默认的div的宽度。
var dh=oBox.offsetHeight;//存储默认的div的高度。
var disright=oBox.offsetLeft+oBox.offsetWidth;//存储默认div右边距离屏幕左边的距离。
var distop=oBox.offsetHeight+oBox.offsetTop;//存储默认div上边距离屏幕左边的距离。
if(iEvent.clientX<oBox.offsetLeft+10){//同理
b='left';
}
document.onmousemove=function(ev){
var iEvent=ev||event;
if(b=='left'){
oBox.style.width=dw-(iEvent.clientX-dx)+'px';//iEvent.clientX-dx表示第二次鼠标的X坐标减去第一次鼠标的X坐标,得到绿色移动的距离(为负数),再加上原本的div宽度,就得到新的宽度。
oBox.style.left=disright-oBox.offsetWidth+'px';//disright表示盒子右边框距离左边的距离,disright-当前的盒子宽度,就是当前盒子距离左边的距离
oLine.style.left = disright-oBox.offsetWidth-2+'px';//disright表示盒子右边框距离左边的距
if(oBox.offsetWidth<=10){
oBox.style.width='10px';
oBox.style.left=disright-oBox.offsetWidth+'px';//防止抖动
}
}
};
document.onmouseup=function(){
document.onmousedown=null;
document.onmousemove=null;
};
return false;
};
};
</script>
</head>
<body style="height: 100%;">
<div id="line"></div>
<div id="box"></div>
</body>
</html>
这是我的代码
<template>
<div class="tagmanange-box" id="">
<div class="taglibdata-box" id="taglibDataBox">
<tag-lib-data></tag-lib-data>
<div class="drag-btn" id="dragBtn"
@mousedown="mouse_down"
@mousemove="mouse_move"
@mouseup="mouse_up"></div>
</div>
<div class="regulation-box"></div>
<div class="database-box"></div>
</div>
</template>
<script type="text/ecmascript-6">
import tagLibData from './tagLibData';
export default {
components: {
tagLibData,
},
props: [],
name: '',
data() {
return {
b: '',
dw: 0,
dx: 0,
disright: 0,
};
},
methods: {
mouse_down(ev) {
console.log('11');
const tagBox = document.getElementById('taglibDataBox');
const iEvent = ev || event;
this.dx = iEvent.clientX;
this.dw = tagBox.offsetWidth;
this.disright = tagBox.offsetLeft + tagBox.offsetWidth;
if (iEvent.clientX > tagBox.offsetLeft + 10) {
this.b = 'left';
}
},
mouse_move(ev) {
console.log('22');
const tagBox = document.getElementById('taglibDataBox');
const dragBtn = document.getElementById('dragBtn');
if (this.b === 'left') {
const iEvent = ev || event;
const dx = iEvent.clientX; // 当你第一次单击的时候,存储x轴的坐标。
const dy = iEvent.clientY; // 当你第一次单击的时候,储存Y轴的坐标。
const dw = tagBox.offsetWidth; // 存储默认的div的宽度。
const dh = tagBox.offsetHeight; // 存储默认的div的高度
const distop = tagBox.offsetHeight + tagBox.offsetTop;
const disright = tagBox.offsetLeft + tagBox.offsetWidth;
tagBox.style.width = `${this.dw + (iEvent.clientX - this.dx)}px`;
// tagBox.style.left = `${this.disright + tagBox.offsetWidth}px`;
dragBtn.style.left = `${disright + tagBox.offsetWidth - 2}px`;
if (tagBox.offsetWidth <= 10) {
tagBox.style.width = '10px';
tagBox.style.left = `${disright - tagBox.offsetWidth}px`;
}
}
},
mouse_up(ev) {
console.log('33');
this.mouse_down = null;
this.mouse_move = null;
},
},
mounted() {
},
};
</script>
<style scoped>
</style>
心累。。。。。。。。。。。。。。求救各位大佬了
兄弟,你把move 的事件放到你要拖动的div上,你让它怎么动嘛。你把move 事件放document上就好了。另外,up事件用来结束整个拖动的过程,你得琢磨琢磨