jQuery拖拽通过八个点改变div大小
发布时间 - 2026-01-10 21:56:03 点击率:次jQuery拖拽通过八个点改变div大小,供大家参考,具体内容如下

js:
(function($) {
/**
* 默认参数
*/
var defaultOpts = {
stage: document, //舞台
item: 'resize-item', //可缩放的类名
};
/**
* 定义类
*/
var ZResize = function(options) {
this.options = $.extend({}, defaultOpts, options);
this.init();
}
ZResize.prototype = {
init: function() {
this.initResizeBox();
},
/**
* 初始化拖拽item
*/
initResizeBox: function() {
var self = this;
$(self.options.item).each(function () {
//创建面板
var width = $(this).width();
var height = $(this).height();
var resizePanel = $('<div class"resize-panel"></div>');
resizePanel.css({
width: width,
height: height,
top: 0,
left: 0,
position: 'absolute',
'background-color': 'rgba(0,0,0,0.5)',
cursor: 'move',
display: 'none'
});
self.appendHandler(resizePanel, $(this));
/**
* 创建控制点
*/
var n = $('<div class="n"></div>');//北
var s = $('<div class="s"></div>');//南
var w = $('<div class="w"></div>');//西
var e = $('<div class="e"></div>');//东
var ne = $('<div class="ne"></div>');//东北
var nw = $('<div class="nw"></div>');//西北
var se = $('<div class="se"></div>');//东南
var sw = $('<div class="sw"></div>');//西南
//添加公共样式
self.addHandlerCss([n, s, w, e, ne, nw, se, sw]);
//添加各自样式
n.css({
'top': '-4px',
'margin-left': '-4px',
'left': '50%',
'cursor': 'n-resize'
});
s.css({
'bottom': '-4px',
'margin-left': '-4px',
'left': '50%',
'cursor': 's-resize'
});
e.css({
'top': '50%',
'margin-top': '-4px',
'right': '-4px',
'cursor': 'e-resize'
});
w.css({
'top': '50%',
'margin-top': '-4px',
'left': '-4px',
'cursor': 'w-resize'
});
ne.css({
'top': '-4px',
'right': '-4px',
'cursor': 'ne-resize'
});
nw.css({
top: '-4px',
'left': '-4px',
'cursor': 'nw-resize'
});
se.css({
'bottom': '-4px',
'right': '-4px',
'cursor': 'se-resize'
});
sw.css({
'bottom': '-4px',
'left': '-4px',
'cursor': 'sw-resize'
});
// 添加项目
self.appendHandler([n, s, w, e, ne, nw, se, sw], resizePanel);
//绑定拖拽缩放事件
self.bindResizeEvent(resizePanel, $(this));
//绑定触发事件
self.bindTrigger($(this));
});
self.bindHidePanel();
},
//控制点公共样式
addHandlerCss: function(els) {
for(var i = 0; i < els.length; i++) {
el = els[i];
el.css({
position: 'absolute',
width: '8px',
height: '8px',
background: '#ff6600',
margin: '0',
'border-radius': '2px',
border: '1px solid #dd5500',
});
}
},
/**
* 插入容器
*/
appendHandler: function(handlers, target) {
for(var i = 0; i < handlers.length; i++) {
el = handlers[i];
target.append(el);
}
},
/**
* 显示拖拽面板
*/
triggerResize: function(el) {
var self = this;
el.siblings(self.options.item).children('div').css({
display: 'none'
});
el.children('div').css({
display: 'block'
});
},
/**
* 拖拽事件控制 包含8个缩放点 和一个拖拽位置
*/
bindResizeEvent: function(el) {
var self = this;
var ox = 0; //原始事件x位置
var oy = 0; //原始事件y位置
var ow = 0; //原始宽度
var oh = 0; //原始高度
var oleft = 0; //原始元素位置
var otop = 0;
var org = el.parent('div');
//东
var emove = false;
el.on('mousedown','.e', function(e) {
ox = e.pageX;//原始x位置
ow = el.width();
emove = true;
});
//南
var smove = false;
el.on('mousedown','.s', function(e) {
oy = e.pageY;//原始x位置
oh = el.height();
smove = true;
});
//西
var wmove = false;
el.on('mousedown','.w', function(e) {
ox = e.pageX;//原始x位置
ow = el.width();
wmove = true;
oleft = parseInt(org.css('left').replace('px', ''));
});
//北
var nmove = false;
el.on('mousedown','.n', function(e) {
oy = e.pageY;//原始x位置
oh = el.height();
nmove = true;
otop = parseInt(org.css('top').replace('px', ''));
});
//东北
var nemove = false;
el.on('mousedown','.ne', function(e) {
ox = e.pageX;//原始x位置
oy = e.pageY;
ow = el.width();
oh = el.height();
nemove = true;
otop = parseInt(org.css('top').replace('px', ''));
});
//西北
var nwmove = false;
el.on('mousedown','.nw', function(e) {
ox = e.pageX;//原始x位置
oy = e.pageY;
ow = el.width();
oh = el.height();
otop = parseInt(org.css('top').replace('px', ''));
oleft = parseInt(org.css('left').replace('px', ''));
nwmove = true;
});
//东南
var semove = false;
el.on('mousedown','.se', function(e) {
ox = e.pageX;//原始x位置
oy = e.pageY;
ow = el.width();
oh = el.height();
semove = true;
});
//西南
var swmove = false;
el.on('mousedown','.sw', function(e) {
ox = e.pageX;//原始x位置
oy = e.pageY;
ow = el.width();
oh = el.height();
swmove = true;
oleft = parseInt(org.css('left').replace('px', ''));
});
//拖拽
var drag = false;
el.on('mousedown', function(e) {
ox = e.pageX;//原始x位置
oy = e.pageY;
otop = parseInt(org.css('top').replace('px', ''));
oleft = parseInt(org.css('left').replace('px', ''));
drag = true;
});
$(self.options.stage).on('mousemove', function(e) {
if(emove) {
var x = (e.pageX - ox);
el.css({
width: ow + x
});
org.css({
width: ow + x
});
} else if(smove) {
var y = (e.pageY - oy);
el.css({
height: oh + y
});
org.css({
height: oh + y
});
} else if(wmove) {
var x = (e.pageX - ox);
el.css({
width: ow - x,
// left: oleft + x
});
org.css({
width: ow - x,
left: oleft + x
});
} else if(nmove) {
var y = (e.pageY - oy);
el.css({
height: oh - y,
// top: otop + y
});
org.css({
height: oh - y,
top: otop + y
});
} else if(nemove) {
var x = e.pageX - ox;
var y = e.pageY - oy;
el.css({
height: oh - y,
// top: otop + y,
width: ow + x
});
org.css({
height: oh - y,
top: otop + y,
width: ow + x
});
} else if(nwmove) {
var x = e.pageX - ox;
var y = e.pageY - oy;
el.css({
height: oh - y,
// top: otop + y,
width: ow - x,
// left: oleft + x
});
org.css({
height: oh - y,
top: otop + y,
width: ow - x,
left: oleft + x
});
} else if(semove) {
var x = e.pageX - ox;
var y = e.pageY - oy;
el.css({
width: ow + x,
height: oh + y
});
org.css({
width: ow + x,
height: oh + y
});
} else if(swmove) {
var x = e.pageX - ox;
var y = e.pageY - oy;
el.css({
width: ow - x,
// left: oleft + x,
height: oh + y
});
org.css({
width: ow - x,
left: oleft + x,
height: oh + y
});
} else if(drag) {
var x = e.pageX - ox;
var y = e.pageY - oy;
org.css({
left: oleft + x,
top: otop + y
});
}
}).on('mouseup', function(e) {
emove = false;
smove = false;
wmove = false;
nmove = false;
nemove = false;
nwmove = false;
swmove = false;
semove = false;
drag = false;
});
},
/**
* 点击item显示拖拽面板
*/
bindTrigger: function(el) {
var self = this;
el.on('click', function(e) {
e.stopPropagation();
self.triggerResize(el);
});
},
/**
* 点击舞台空闲区域 隐藏缩放面板
*/
bindHidePanel: function(el) {
var stage = this.options.stage;
var item = this.options.item;
$(stage).bind('click', function() {
$(item).children('div').css({
display: 'none'
});
})
}
}
window.ZResize = ZResize;
})(jQuery);
html:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery拖拽放大缩小插件idrag</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
.item1 {
width: 405px;
height: 291px;
cursor: move;
position: absolute;
top: 30px;
left: 30px;
background-color: #FFF;
border: 1px solid #CCCCCC;
-webkit-box-shadow: 10px 10px 25px #ccc;
-moz-box-shadow: 10px 10px 25px #ccc;
box-shadow: 10px 10px 25px #ccc;
}
.item2 {
width: 200px;
height: 100px;
cursor: move;
position: absolute;
top: 400px;
left: 100px;
background-color: #FFF;
border: 1px solid #CCCCCC;
-webkit-box-shadow: 10px 10px 25px #ccc;
-moz-box-shadow: 10px 10px 25px #ccc;
box-shadow: 10px 10px 25px #ccc;
line-height: 100px;
text-align: center;
}
body {
background-color: #F3F3F3;
}
</style>
</head>
<body>
<div id="mydiv" style="width:800px; height:800px; border-style:solid">
<div id="div1" class="resize-item item1">
<img src="images/dog.png" width="100%" height="100%">
</div>
<div class="resize-item item2">
你是我的小小狗
</div>
</div>
<script src="jquery.min.js"></script>
<script type="text/javascript" src='jquery.ZResize.js'></script>
<script type="text/javascript">
new ZResize({
stage: "#mydiv", //舞台
item: '#div1', //可缩放的类名
});
</script>
</body>
</html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# jQuery拖拽改变div大小
# jQuery八个点改变div大小
# div改变大小
# jQuery 移动端拖拽(模块化开发
# 触摸事件
# webpack)
# jQuery实现div拖拽效果实例分析
# jQuery div拖拽用法实例
# jQuery使用drag效果实现自由拖拽div
# jQuery控制Div拖拽效果完整实例分析
# jquery实现拖拽调整Div大小
# jQuery拖拽div实现思路
# jquery实现div拖拽宽度示例代码
# JQuery Dialog(JS 模态窗口
# 可拖拽的DIV)
# Jquery实现移动端控制DIV拖拽
# 拖拽
# 绑定
# 东南
# 你是
# 具体内容
# 大家多多
# bindTrigger
# bottom
# bindResizeEvent
# length
# el
# bindHidePanel
# els
# nw
# se
# appendHandler
# ne
# px
# margin
# sw
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
Laravel如何创建自定义Facades?(详细步骤)
Win11怎样安装网易有道词典_Win11安装词典教程【步骤】
如何自定义safari浏览器工具栏?个性化设置safari浏览器界面教程【技巧】
香港服务器网站测试全流程:性能评估、SEO加载与移动适配优化
如何快速查询域名建站关键信息?
Laravel如何实现文件上传和存储?(本地与S3配置)
Laravel如何处理和验证JSON类型的数据库字段
成都网站制作公司哪家好,四川省职工服务网是做什么用?
如何在阿里云购买域名并搭建网站?
百度输入法全感官ai怎么关 百度输入法全感官皮肤关闭
高防服务器租用如何选择配置与防御等级?
QQ浏览器网页版登录入口 个人中心在线进入
Laravel如何操作JSON类型的数据库字段?(Eloquent示例)
Laravel怎么实现搜索功能_Laravel使用Eloquent实现模糊查询与多条件搜索【实例】
laravel怎么实现图片的压缩和裁剪_laravel图片压缩与裁剪方法
中山网站推广排名,中山信息港登录入口?
韩国服务器如何优化跨境访问实现高效连接?
Gemini怎么用新功能实时问答_Gemini实时问答使用【步骤】
常州企业网站制作公司,全国继续教育网怎么登录?
如何生成腾讯云建站专用兑换码?
phpredis提高消息队列的实时性方法(推荐)
Laravel怎么实现支付功能_Laravel集成支付宝微信支付
Laravel Eloquent:优雅地将关联模型字段扁平化到主模型中
Swift中循环语句中的转移语句 break 和 continue
矢量图网站制作软件,用千图网的一张矢量图做公司app首页,该网站并未说明版权等问题,这样做算不算侵权?应该如何解决?
Laravel怎么多语言本地化设置_Laravel语言包翻译与Locale动态切换【手册】
java获取注册ip实例
laravel怎么配置和使用PHP-FPM来优化性能_laravel PHP-FPM配置与性能优化方法
网站制作价目表怎么做,珍爱网婚介费用多少?
Laravel怎么进行数据库回滚_Laravel Migration数据库版本控制与回滚操作
北京网站制作费用多少,建立一个公司网站的费用.有哪些部分,分别要多少钱?
Swift中switch语句区间和元组模式匹配
如何在阿里云ECS服务器部署织梦CMS网站?
如何用美橙互联一键搭建多站合一网站?
青岛网站建设如何选择本地服务器?
如何在景安云服务器上绑定域名并配置虚拟主机?
安克发布新款氮化镓充电宝:体积缩小 30%,支持 200W 输出
如何注册花生壳免费域名并搭建个人网站?
laravel怎么为API路由添加签名中间件保护_laravel API路由签名中间件保护方法
活动邀请函制作网站有哪些,活动邀请函文案?
东莞专业网站制作公司有哪些,东莞招聘网站哪个好?
简单实现Android文件上传
zabbix利用python脚本发送报警邮件的方法
如何快速搭建支持数据库操作的智能建站平台?
如何快速搭建虚拟主机网站?新手必看指南
Python自动化办公教程_ExcelWordPDF批量处理案例
用yum安装MySQLdb模块的步骤方法
如何选择PHP开源工具快速搭建网站?
php后缀怎么变mp4格式错误_修改扩展名提示格式不对怎么办【技巧】
高性能网站服务器部署指南:稳定运行与安全配置优化方案

