当前位置: 首页 > wzjs >正文

东乡族网站建设网址安全检测中心

东乡族网站建设,网址安全检测中心,商城网站建设哪家便宜,网站建设文化包括哪些代码实现 整体页面结构通过一个 dragResize-wrapper 包含左右两个区域&#xff0c;左侧区域包含一个可拖拽的边界。以下是关键代码 HTML 部分 <template><div class"dragResize-wrapper"><div class"dragResize-left"><div class&…

代码实现

整体页面结构通过一个 dragResize-wrapper 包含左右两个区域,左侧区域包含一个可拖拽的边界。以下是关键代码

HTML 部分
<template><div class="dragResize-wrapper"><div class="dragResize-left"><div class="resize" @mousedown="resize"></div></div><div class="dragResize-right"></div></div>
</template>
CSS 部分
<style lang="less" scoped>
.dragResize-wrapper {display: flex;width: 100vw;height: 100vh;.dragResize-left {position: relative;width: 30%;height: 100%;background-color: #1a2029;border-right: 1px solid #ccc;.resize {position: absolute;right: 0;width: 10px;height: 100%;background-color: #1a2029;}.resize:hover {cursor: e-resize;}}.dragResize-right {width: 70%;height: 100%;background-color: #472020;}
}
</style>
JavaScript 部分
<script setup>
import { ref, reactive, onMounted } from "vue";
const dragState = reactive({isDragging: false,startX: 0,leftInitialWidth: 0,minWidth: 100, // 左侧区域最小宽度限制maxWidth: 800, // 左侧区域最大宽度限制
});const resize = (e) => {dragState.isDragging = true;dragState.startX = e.clientX; // 获取鼠标按下时的 X 坐标dragState.leftInitialWidth = e.currentTarget.parentElement.offsetWidth; // 获取左侧区域初始宽度document.body.style.cursor = "e-resize"; // 设置鼠标样式为拖拽样式document.body.style.userSelect = "none"; // 禁止选中文本
};// 鼠标移动事件,执行拖拽调整宽度
const handleMouseMove = (e) => {if (!dragState.isDragging) return; // 如果没有拖拽,直接返回const deltax = e.clientX - dragState.startX; // 计算鼠标移动的距离let newWidth = dragState.leftInitialWidth + deltax; // 计算新的宽度newWidth = Math.max(dragState.minWidth,Math.min(newWidth, dragState.maxWidth)); // 限制宽度在最小和最大值之间document.querySelector(".dragResize-left").style.width = `${newWidth}px`; // 设置左侧区域的新宽度document.querySelector(".dragResize-right").style.width = `calc(100% - ${newWidth}px)`; // 设置右侧区域宽度为剩余空间
};// 鼠标松开事件,结束拖拽
const handleMouseUp = () => {if (dragState.isDragging) {dragState.isDragging = false; // 重置拖拽状态document.body.style.cursor = "default"; // 恢复鼠标样式document.body.style.userSelect = "auto"; // 恢复文本选择}
};onMounted(() => {document.addEventListener("mousemove", handleMouseMove); // 监听鼠标移动事件document.addEventListener("mouseup", handleMouseUp); // 监听鼠标松开事件
});
</script>

定义一个 dragState 对象来跟踪拖拽状态,包括是否正在拖拽、鼠标起始坐标、左侧区域初始宽度以及左右宽度限制。在 resize 函数中,设置拖拽开始时的相关状态和样式。handleMouseMove 函数根据鼠标移动距离计算新的宽度,并在一定范围内调整左右区域的宽度。handleMouseUp 函数用于结束拖拽并恢复样式。在组件挂载后添加鼠标移动和松开的事件监听。

完整实例代码

<template><div class="dragResize-wrapper"><div class="dragResize-left"><div class="resize" @mousedown="resize"></div></div><div class="dragResize-right"></div></div>
</template><script setup>
import { ref, reactive, onMounted } from "vue";
const dragState = reactive({isDragging: false,startX: 0,leftInitialWidth: 0,minWidth: 100, // 左侧区域最小宽度限制maxWidth: 800, // 左侧区域最大宽度限制
});const resize = (e) => {dragState.isDragging = true;dragState.startX = e.clientX; // 获取鼠标按下时的X坐标dragState.leftInitialWidth = e.currentTarget.parentElement.offsetWidth; // 获取左侧区域初始宽度document.body.style.cursor = "e-resize"; // 设置鼠标样式为拖拽样式document.body.style.userSelect = "none"; // 禁止选中文本
};// 鼠标移动事件,执行拖拽调整宽度
const handleMouseMove = (e) => {if (!dragState.isDragging) return; // 如果没有拖拽,直接返回const deltax = e.clientX - dragState.startX; // 计算鼠标移动的距离let newWidth = dragState.leftInitialWidth + deltax; // 计算新的宽度newWidth = Math.max(dragState.minWidth,Math.min(newWidth, dragState.maxWidth)); // 限制宽度在最小和最大值之间document.querySelector(".dragResize-left").style.width = `${newWidth}px`; // 设置左侧区域的新宽度document.querySelector(".dragResize-right").style.width = `calc(100% - ${newWidth}px)`; // 设置右侧区域宽度为剩余空间
};// 鼠标松开事件,结束拖拽
const handleMouseUp = () => {if (dragState.isDragging) {dragState.isDragging = false; //重置拖拽状态document.body.style.cursor = "default"; // 恢复鼠标样式document.body.style.userSelect = "auto"; // 恢复文本选择}
};onMounted(() => {document.addEventListener("mousemove", handleMouseMove); // 监听鼠标移动事件document.addEventListener("mouseup", handleMouseUp); // 监听鼠标松开事件
});
</script><style lang="less" scoped>
.dragResize-wrapper {display: flex;width: 100vw;height: 100vh;.dragResize-left {position: relative;width: 30%;height: 100%;background-color: #1a2029;border-right: 1px solid #ccc;.resize {position: absolute;right: 0;width: 10px;height: 100%;background-color: #1a2029;}.resize:hover {cursor: e-resize;}}.dragResize-right {width: 70%;height: 100%;background-color: #472020;}
}
</style>

http://www.dtcms.com/wzjs/444666.html

相关文章:

  • 定制企业网站开发公司黄冈网站推广软件
  • 高档网站建设公司百度快速排名
  • 做网站排名浏览器下载
  • 信息发布网站开发模板百度世界500强排名
  • 商务网站建设总结教育培训机构前十名
  • 网站开发语言用到网站入口百度
  • 在什么网站做公务员题目百度关键词优化专家
  • 自己可以免费做网站吗杭州优化关键词
  • 企业网站推广的收获与启示seo优化诊断工具
  • 不用服务器做视频网站seo网站优化软件
  • 求职网站怎么做网络营销课程报告
  • 做海报设计的网站网络公司排名
  • 淘宝联盟+做网站上海网络营销公司
  • 南京网站建设公司 雷仁网络网络推广的方式有哪些?
  • wordpress 编辑首页seo中文意思是
  • wordpress附件上传福州短视频seo机会
  • 做旅游行程的网站百度关键词搜索查询
  • 兼容性视图中显示所有网站西安seo工作室
  • 大学两学一做网站网上卖货的平台有哪些
  • 佛山大型的网站制作咨询公司
  • 网站建设运营怎么办广东网站se0优化公司
  • 怎样做网站后台优化cilimao磁力猫在线搜索
  • 常州做网站那家快seo网站排名优化公司
  • 局域网内部如何做网站百度公司排名
  • 如何在电脑上打开自己做的网站企业管理培训课程网课
  • 泛华建设集团网站全球搜官网
  • wdcp wordpress forbidden 403韶关seo
  • 网站的切图是谁来做seo网站的优化流程
  • 凡科做的网站好垃圾东莞做网页建站公司
  • 周到的网站建设推广发稿媒体平台