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

鼠标拖拽实现DIV尺寸修改效果实现

实现功能:有水平3个DIV,左侧div和右侧div通过摁下鼠标左键修改div尺寸。

思路:设置一条drap-line线 并绑定方法,当摁下鼠标左键滑动时修改div尺寸,释放鼠标修改尺寸结束。

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<script src="jquery.js"></script>
		<title></title>
	</head>
	<style>
		* {
			margin: 0;
			padding: 0;
		}

		body {
			display: flex;
			width: 100vw;
			height: 100vh;
		}

		.left,
		.right {
			width: 400px;
			background-color: aliceblue;
			height: 100%;
		}

		.center {
			flex: 3;
			background-color: antiquewhite;
			height: 100%;
		}

		.drap-line {
			width: 4px;
			height: 100%;
			background-color: brown;
			cursor: col-resize;
		}
	</style>
	<body>
		<div class="left"></div>
		<div class="drap-line mr4"></div>
		<div class="center"></div>
		<div class="drap-line"></div>
		<div class="right"></div>
		<script>
			$(function(){
				resizeInit() ;
			});
			 //左右两侧resize事件
			    function resizeInit() {
			        var isResizing = false;
			        var DIV;
			        var toward = 'left';
			        $('body').on('mousedown','.drap-line',function (e) {
			            isResizing = true;
			            $(this).hasClass('mr4') ? (DIV = $('.left'), toward = 'left') : (DIV = $('.right'), toward = 'right');
			        });
			        $(document).on('mouseup',function (e) {
			            isResizing = false;
			        });
			        $(document).on('mousemove',function (e) {
			            if (!isResizing) return;
			            var newWidth = 0;
			            if (toward == 'left') {
			                newWidth = e.pageX - DIV.offset().left;
			            } else {
			                newWidth = $(window).width() - e.pageX;
			            }
			
			            if (newWidth < 270) {
			                newWidth = 270;
			            } else if (newWidth > 900) {
			                newWidth = 900;
			            }
			            DIV.width(newWidth);
			        });
			    };
		</script>

	</body>
</html>


http://www.kler.cn/a/597673.html

相关文章:

  • 零基础本地部署 ComfyUI+Flux.1 模型!5 分钟搭建远程 AI 绘图服务器(保姆级教程)
  • 六西格玛遇上Python:统计学的高效实践场
  • 基于SpringBoot的图书借阅小程序+LW参考示例
  • Matplotlib完全指南:数据可视化从入门到实战
  • upload-labs靶场学习记录2
  • 再学:区块链基础与合约初探 EVM与GAS机制
  • java开发接口中,响应速度率成倍数提高的方法与策略
  • 基于BClinux8部署Ceph 19.2(squid)集群
  • SQL——创建临时表方法总结
  • 城市街拍人像自拍电影风格Lr调色教程,手机滤镜PS+Lightroom预设下载!
  • Opencv计算机视觉编程攻略-第一节 图像读取与基本处理
  • 百度SEO和必应SEO优化方法
  • 【css酷炫效果】纯CSS实现科技感网格背景
  • JVM运行时数据区内部结构难记?一个例子优化记忆
  • 摄影工作室预约管理系统基于Spring BootSSM
  • 校园自习室预约小程序(源码+部署教程)
  • 基于Spring Boot的健身房管理系统的设计与实现(LW+源码+讲解)
  • python网络爬虫开发实战之网页数据的解析提取
  • 如何在Spring Boot项目中集成LangChain4J开源开发框架
  • 论文阅读:2024-NAACL Semstamp、2024-ACL (Findings) k-SemStamp