js实现鼠标拖拽缩放div实例代码
作者:看淡江湖 时间:2024-05-13 10:38:12
封装为了jq插件,如下
drag.js
;(function ($) {
$.fn.dragDiv = function (options) {
var def = {
maxW:600,// 可伸缩的最大宽度
minW:50// 可伸缩的最小宽度
};// 参数默认值
var opts = $.extend(def,options);// 扩展参数,使用默认值或传参
//设置最大/最小宽度
var max_width = opts.maxW,
min_width = opts.minW;
//记录鼠标相对left盒子x轴位置
var mouse_x = 0;
var left = $(this).parent('div')[0];
//鼠标移动事件
function mouseMove(e) {
var e = e || window.event;
var left_width = e.clientX - mouse_x;// 可伸缩div的宽度
left_width = left_width < min_width ? min_width : left_width;
left_width = left_width > max_width ? max_width : left_width;
left.style.width = left_width + "px";
}
//终止事件
function mouseUp() {
document.onmousemove = null;
document.onmouseup = null;
}
$(this).mousedown(function (e) {
var e = e || window.event;
//阻止默认事件
e.preventDefault();
mouse_x = e.clientX - left.offsetWidth;// 可伸缩div距离左侧边界的宽度
document.onmousemove = function () {
mouseMove()
};
document.onmouseup = function () {
mouseUp()
};
})
}
})(jQuery)
html文件
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>鼠标进行伸缩div</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.dragdom {
background: #cccccc;
width: 100px;
height: 600px;
margin: 0 auto;
position: relative;
}
.dragdom .drag {
border: 1px transparent solid;
width: 0px;
height: 100%;
position: absolute;
top: 0;
right: 0;
cursor: e-resize;
}
</style>
</head>
<body>
<div class="dragdom"><div class="drag"></div></div>
</body>
</html>
<script type="text/javascript" src="./jquery.min.js"></script>
<script type="text/javascript" src="./drag.js"></script>
<script>
$(function () {
$('.drag').dragDiv();
})
</script>
以上所述是小编给大家介绍的js实现鼠标拖拽缩放div详解整合网站的支持!
来源:https://blog.csdn.net/hhy1006894859/article/details/88655510
标签:js,div拖拽
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
PHP/ThinkPHP实现批量打包下载文件的方法示例
2024-05-11 09:49:00
python import模块时有错误红线的原因
2021-03-19 13:59:30
![](https://img.aspxhome.com/file/2023/2/109412_0s.png)
Python 格式化输出字符串的方法(输出字符串+数字的几种方法)
2021-05-03 21:25:00
Oracle与MySQL删除字段时对索引和约束的处理
2008-12-26 16:41:00
python中列表的常见操作梳理总结(一)
2022-08-25 10:57:03
![](https://img.aspxhome.com/file/2023/9/135089_0s.png)
巧用Dreamweaver MX制作阴影字
2009-08-31 17:36:00
![](https://img.aspxhome.com/file/UploadPic/20072/200723113255757s.gif)
python实现三种随机请求头方式
2022-01-17 17:06:39
![](https://img.aspxhome.com/file/2023/5/81575_0s.png)
python调用百度API实现人脸识别
2021-06-11 23:52:23
![](https://img.aspxhome.com/file/2023/2/68472_0s.png)
python3.6使用tkinter实现弹跳小球游戏
2022-04-09 14:37:13
vue 请求后台数据的实例代码
2024-05-05 09:23:47
Java正则表达式基础语法详解
2023-07-10 14:42:14
PHP实现sha-256哈希算法实例代码
2023-05-25 01:05:23
Django RestFramework 全局异常处理详解
2023-12-15 03:16:42
Django利用Cookie实现反爬虫的例子
2023-05-11 19:48:01
如何批量生成MySQL不重复手机号大表实例代码
2024-01-23 13:41:02
mysql增量备份及断点恢复脚本实例
2024-01-27 04:54:34
![](https://img.aspxhome.com/file/2023/7/72447_0s.png)
Python、PyCharm安装及使用方法(Mac版)详解
2021-04-19 22:45:42
![](https://img.aspxhome.com/file/2023/4/109334_0s.png)
MySql常用命令总结
2009-09-16 10:51:00
Go并发调用的超时处理的方法
2024-04-28 09:15:40
对Server.UrlEncode进行字符反编译
2009-06-22 12:54:00