由于项目中并未引入前端开发框架easyui、ext。没有现成的控件可以使用,今天时间算是充裕的时候,自己写了一个可以拖拽、放大缩小的例子。欢迎大家指正。

不啰嗦,上代码:

依赖的文件:jquery.js 需要大家下载一个,或者找个在线的jquery

比如:http://libs.baidu.com/jquery/1.11.1/jquery.min.js

拖动思路:拖动思路很简单就是监听鼠标的mousedown和mouseup事件,

放大缩小思路: 改变div的大小,我画了个图来看下把:

依据上诉思路我针对div鼠标滑过各个变拖动,做出了如下判断:

有了上述的分析之后,写这个例子就看上去清晰了很多。

代码分为两部分:1、html;2、js文件

1、html

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery拖放</title>
<script type="text/javascript" src="jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="dd.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
body { background-color: #eee; }
.box {width: 200px; height: 100px; 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;}
.main_tabletop{width: 100%;height:20px;background:#ffee00;}
</style>
</head>
<body>
<div class="box">
<div class="main_tabletop">我是可以拖动的标题</div>
左、右、下、左下、右下都可放大缩小
</div>
</body>
</html>

2、js文件源码

$(function() {
$(document).mousemove(function(e) {
if (!!this.move) {
var posix = !document.move_target ? {'x': 0, 'y': 0} : document.move_target.posix,
callback = document.call_down || function() {
$(this.move_target).css({
'top': e.pageY - posix.y,
'left': e.pageX - posix.x
});
}; callback.call(this, e, posix);
}
}).mouseup(function(e) {
if (!!this.move) {
var callback = document.call_up || function(){};
callback.call(this, e);
$.extend(this, {
'move': false,
'move_target': null,
'call_down': false,
'call_up': false
});
}
}); var $box = $('.box .main_tabletop').mousedown(function(e) {
var $p = $(this).parent();
var $pp = $p[0];
var offset = $p.offset();
$pp.posix = {'x': e.pageX - offset.left, 'y': e.pageY - offset.top};
$.extend(document, {'move': true, 'move_target':$pp }); });
$('.box').bind(
{'mousemove':function(e){
$(this).css({cursor: "default"});
var offset = $(this).offset(), resize=true;
var x = e.clientX, y = e.clientY, t = offset.top, l = offset.left, w = $(this).width(), h = $(this).height(), ww = $('.main_tabletop').height(), b = 10;
if(x<(l+b) && y > (t+ww) && y < (t+h-b)){
$(this).css({cursor: "w-resize"});
$(this).unbind("mousedown").bind({"mousedown":function(e){
var $p = $(this);
var posix = {
'w': $p.width(),
'h': $p.height(),
'x': e.pageX,
'y': e.pageY
}; $.extend(document, {'move': true, 'call_down': function(e) {
$p.css({
'width': Math.max(30, posix.x-e.pageX + posix.w),
'left': Math.max(30, e.pageX)
});
}});
}});
}else if(x<(l+w) && x>(l+w-b) && y > (t+ww) && y < (t+h-b)){
$(this).css({cursor: "e-resize"});
$(this).unbind("mousedown").bind({"mousedown":function(e){
var $p = $(this);
var posix = {
'w': $p.width(),
'x': e.pageX
};
resizeBox($p, posix, e);
}});
}else if(y<(t+h) && y>(t+h-b) && x>(l+b) && x<(l+w-b)){
$(this).css({cursor: "s-resize"});
$(this).unbind("mousedown").bind({"mousedown":function(e){
var $p = $(this);
var posix = {
'h': $p.height(),
'y': e.pageY
};
resizeBox($p, posix, e);
}
});
}else if(x<(l+b) && y>(t+h-b) && y<(t+h)){
$(this).css({cursor: "sw-resize"});
$(this).unbind("mousedown").bind({"mousedown":function(e){
var $p = $(this);
var posix = {
'w': $p.width(),
'h': $p.height(),
'x': e.pageX,
'y': e.pageY
}; $.extend(document, {'move': true, 'call_down': function(e) {
$p.css({
'width': Math.max(30, posix.x-e.pageX + posix.w),
'height': Math.max(30, e.pageY - posix.y + posix.h),
'left': Math.max(30, e.pageX)
});
}});
}});
}else if(y<(t+h) && y>(t+h-b) && x<(l+w) && x>(l+w-b)){
$(this).css({cursor: "se-resize"});
$(this).unbind("mousedown").bind({"mousedown":function(e){
var $p = $(this);
var posix = {
'w': $p.width(),
'h': $p.height(),
'x': e.pageX,
'y': e.pageY
};
$.extend(document, {'move': true, 'call_down': function(e) {
$p.css({
'width': Math.max(30, e.pageX - posix.x + posix.w),
'height': Math.max(30, e.pageY - posix.y + posix.h)
});
}});
}
});
}else if(y<(t+ww) && x>l && x<(l+w)){
$(this).css({cursor: "move"});
$(this).unbind("mousedown");
}
},
"mouseup":function(){
$(this).unbind("mousedown");
}
});
function resizeBox($p,posix,e){
$.extend(document, {'move': true, 'call_down': function(e) {
$p.css({
'width': Math.max(30, e.pageX - posix.x + posix.w),
'height': Math.max(30, e.pageY - posix.y + posix.h)
});
}});
}
});

最新文章

  1. IOS开发基础知识--碎片35
  2. PHP实现RESTful风格的API实例(二)
  3. CodeForces - 426A(排序)
  4. Java 字节流实现文件读写操作(InputStream-OutputStream)
  5. css 动画效果
  6. Linux下修改Oracle监听地址
  7. [转]JS继承的5种实现方式
  8. IComparer&lt;T&gt; 接口Linq比较接口
  9. 转载:js实现上传图片时 点击浏览后 就可以看到缩略图 很实用
  10. gcc编译器对宽字符的识别
  11. 限制QLineEdit的数值输入范围(一共4种限制器:QDoubleValidator, QIntValidator, QRegExpValidator, 和QRegularExpressionValidator)
  12. 如何用70行Java代码实现深度神经网络算法
  13. .NET和JAVA 反射对比
  14. 本地Git仓库同步到Bitbucket 远程Git仓库
  15. spring AOP源码分析(二)
  16. 约束4:唯一约束,Check约束和null
  17. IDC 知识库
  18. tar 压缩解压命令详解
  19. nvidia显卡驱动
  20. java 基础 --Collection(Map)

热门文章

  1. 读取Devexpress内部的图标
  2. javascript之聊天室(单机)来自于冷的锋刃
  3. 利用opencv训练样本分类
  4. python 学习笔记十七 django深入学习二 form,models
  5. WebForm Application Viewstate 以及分页(功能性的知识点)
  6. T检验与F检验的区别_f检验和t检验的关系
  7. 深入了解try catch
  8. Oracle(创建index)
  9. Bootstrap3中.container和.container-fluid区别
  10. servlet(二)