当前位置:网站首页>拖动元素边缘改变宽度
拖动元素边缘改变宽度
2022-08-02 03:34:00 【IICOOM】
最近有个需求,需要可以手动拖动侧边栏菜单,改变其宽度。如下:
要完成这个功能,我们需要了解以下前端知识:
- 鼠标按下、抬起事件(mousedown、mouseup)
- 鼠标移动事件(mousemove)
- 鼠标按下时在屏幕上的位置 clientX
- 鼠标hover到元素上鼠标样式
- 为了使调整过程比较顺滑,需要用到css transition
好,差不多了,开整。
添加页面元素
<template>
<div class="m_wrapper">
<div class="left">
<div class="side_bar">
Side Menu
<div class="handle"></div>
</div>
</div>
<div class="right">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dignissimos error illum laboriosam laudantium natus quaerat tempora voluptatibus! Beatae dolor doloribus ex facere, iste iure maiores perferendis perspiciatis qui vel. Eligendi?</div>
</div>
</template>
添加元素样式
<style scoped>
.m_wrapper {
display: flex;
padding: 10px;
user-select: none;
}
.m_wrapper .side_bar {
width: 250px;
height: 300px;
background-color: rgba(13, 202, 240, 0.32);
position: relative;
transition: width linear 100ms;
box-sizing: border-box;
}
.m_wrapper .right {
margin-left: 10px;
border: 1px solid #ccc;
}
.handle {
height: 100%;
width: 2px;
position: absolute;
right: -1px;
top: 0;
transition: all linear 200ms;
}
.handle:hover {
background-color: #4578FF;
cursor: col-resize;
}
</style>
添加鼠标移动逻辑
<script>
export default {
name: "MouseMove",
mounted() {
let th = this
// 通过鼠标按下事件来监听鼠标移动事件
document.addEventListener('mousedown', function (e) {
th.x = e.clientX
document.addEventListener('mousemove', th.move)
})
document.addEventListener('mouseup', function () {
document.removeEventListener('mousemove', th.move)
})
this.target = document.querySelector('.side_bar')
},
data() {
return {
target: '',
x: '',
}
},
methods: {
move(e) {
if (e.clientX - this.target.offsetLeft> 400) {
this.target.style.width = '400px'
} else if (e.clientX - this.target.offsetLeft<200) {
this.target.style.width = '200px'
} else {
this.target.style.width = e.clientX - this.target.offsetLeft + 'px'
}
},
}
}
</script>
边栏推荐
- 【数据库】事务的四大特性<详解>
- MPU6050 accelerometer and gyroscope sensor is connected with the Arduino
- 剑指Offer 13.机器人的运动范围 深度优先遍历
- MAC安装Mysql超详细完整教程
- 联阳(ITE)IT66021FN:HDMI转RGB芯片 3D 资料
- Comparative analysis of mobile cloud IoT pre-research and Alibaba Cloud development
- 回溯法 & 分支限界 - 2
- 【Connect the heart rate sensor to Arduino to read the heart rate data】
- bluez5.50+pulseaudio实现蓝牙音响音频播放
- 2020 - AAAI - Image Inpainting论文导读《Learning to Incorporate Structure Knowledge for Image Inpainting》
猜你喜欢
学习(四):显示FPS,和自定义显示调试
vector的使用和模拟实现:
GM7150 CVBS转BT656视频解码芯片详细内容及设计要求
剑指Offer 36.二叉搜索树与双向链表 中序遍历
Introduction and mock implementation of list:list
The use and simulation of vector implementation:
STM32 CAN 介绍以及相关配置
GM8775C MIPI转LVDS调试资料分享
2020 - AAAI - Image Inpainting论文导读《Learning to Incorporate Structure Knowledge for Image Inpainting》
unity学习(五):Excel表格读取和数据类生成
随机推荐
所有子字符串中的元音 —— LeetCode - 2063
path 修补文件命令
使用pyqt弹出消息提示框
MQ-5 combustible gas sensor interface with Arduino
Mac安装MySQL详细教程
【LeetCode】设计链表
TeamCode 产品 UI 全新升级,快来体验吧
剑指Offer 04.二位数组中的查找 线性查找
谷粒商城10——搜索、商品详情、异步编排
实现动态库(DLL)之间内存统一管理
Basic IO (on): file management and descriptors
78XX 79XX多路输出电源
GM7150,振芯科技,视频解码器,CVBS转BT656/601,QFN32,替换TVP5150/CJC5150
基础IO(下):软硬链接和动静态库
HAL库笔记——通过按键来控制LED(基于正点原子STM32F103ZET6精英板)
【Connect the heart rate sensor to Arduino to read the heart rate data】
剑指Offer 47.礼物的最大值 动态规划
联阳IT6561|IT6561FN方案电路|替代IT6561方案设计DP转HDMI音视频转换器资料
AD Actual Combat
【nRF24L01 connects with Arduino to realize wireless communication】