javascript实现div的拖动并调整大小类似qq空间个性编辑模块


Posted in Javascript onDecember 12, 2012

经常上qq空间的朋友一定对qq空间的个性编辑模块印象深刻,可以随意的拖动页面上的元素并且调动大小实现动态布局,当然我每次上csdn博客也会在右下角看见一个新闻窗口,这种效果的确很酷,那么我们也来实现一个吧.

实现步骤:
1.首先是动态创建一个类似这样的html结构:

<div style="height:200px;width:200px;overflow:hidden" id="a"> 
<div id="head" style="background-color:blue;height:5%"> 
<span id="move" style="width:90%;height:100%"></span> 
<span id="close" style="overflow:hidden;white-space:nowrap;background-color:red">关闭</span> 
</div> 
<div id="body" style="width:100%;height:90%"></div> 
</div>

2.id为body的为你要放置内容的div容器,move是可移动的span,close是关闭这个窗口(准确说是层).
3.然后将事件绑定到这些对象上.具体看一下代码.
sx.activex.windowex={ 
init:function(step,t,html){ 
var a=document.createElement("div"); 
var head=document.createElement("div"); 
var move=document.createElement("span"); 
var close=document.createElement("span"); 
close.innerText="关闭"; 
var body=document.createElement("div"); 
head.appendChild(move); 
head.appendChild(close); 
a.appendChild(head); 
a.appendChild(body); 
a.style.height="200px"; 
a.style.width="200px"; 
a.style.overflow="hidden"; 
a.style.border="1px red solid"; 
head.style.backgroundColor="blue"; 
head.style.height="5%"; 
move.style.width="90%"; 
move.style.height="100%"; 
close.style.height="100%"; 
close.style.overflow="hidden"; 
close.style.whiteSpace="nowrap"; 
close.style.backgroundColor="yellow"; 
body.style.height="93%"; 
body.style.width="100%"; 
body.style.overflow="auto"; 
a.style.position="absolute"; 
close.style.position="absolute"; 
close.style.cursor="hand"; 
close.style.top=0+"px"; 
close.style.right=0+"px"; 
close.onclick=function(){ 
window.event.cancelBubble=true; 
var q=a.offsetHeight; 
var h=window.setInterval(function(){ 
if(Math.abs(q)>=0){ 
a.style.height=q+"px"; 
q=q-step; 
if(Math.abs(q)<step){ 
//e.style.height=q+"px"; 
window.clearInterval(h); 
//window.setTimeout(function(){ 
//alert(this==window); 
close.style.cursor="normal"; 
a.parentNode.removeChild(a); 
//a.style.lineHeight="0px"; 
//},10); 
} 
}else{ 
window.clearInterval(h); 
//a.style.display="none"; 
} 
},t); 
} 
move.onmousedown=function(){ 
this.move=1; 
this.x=window.event.offsetX; 
//alert(this.x); 
this.y=window.event.offsetY; 
this.setCapture(); 
} 
move.onmousemove=function(){ 
this.style.cursor="move"; 
if(window.event.clientX<=0 || window.event.clientY<=0 || window.event.clientX>=document.body.clientWidth || window.event.clientY>=document.body.clientHeight){return false;} 
if(this.move==1){ this.parentNode.parentNode.style.left=window.event.clientX-this.x+"px"; 
this.parentNode.parentNode.style.top=window.event.clientY-this.y+"px"; 
this.setCapture(); 
} 
} 
move.onmouseup=function(){ 
if(this.move==1){ 
this.move=0; 
//this.style.cursor="normal"; 
this.releaseCapture(); 
} 
} 
a.onmousemove=function(){ 
if(this.move==1){ 
if(window.event.clientX-this.offsetLeft<2 || window.event.clientY-this.offsetTop<2) return false; 
this.style.width=window.event.clientX-this.offsetLeft+"px"; 
this.style.height=window.event.clientY-this.offsetTop+"px"; 
close.style.right="0px"; 
this.setCapture(); 
} 
else{ 
if(window.event.offsetX-this.offsetWidth>-6 && window.event.offsetY-this.offsetHeight>-6) 
this.style.cursor="nw-resize"; 
else 
this.style.cursor="default"; 
} 
} 
a.onmouseup=function(){ 
if(this.move==1){ 
this.move=0; 
this.releaseCapture(); 
} 
} 
a.onmousedown=function(){ 
if(this.style.cursor=="nw-resize"){ 
this.move=1; 
this.setCapture(); 
} 
} 
body.innerHTML=html; 
return a; 
}

代码也不复杂,主要是什么onmousedown,onmousemove,onmouseup的编写.我调整大小的原理当的你鼠标移动到层的右下角时,鼠标指针改变,这时按下鼠标并且移动时,会将当前层setcapture,移动鼠标层会随鼠标的位置而调整大小,松开鼠标releasecapture.

函数的参数step是你按下关闭时每次时间间隔移动的步数,t是时间间隔,html是你要插入到body层里的html代码.
一下给出一个调用例子:

<html> 
<head> 
<title>Untitled Document</title> 
</head> 
<body> <mce:script src="kongjian.js" mce_src="kongjian.js"></mce:script> 
<mce:script type="text/javascript"><!-- 
var a=sx.activex.windowex.init(10,10,"<img src="1.jpg" mce_src="1.jpg" height=500 width=500>"); 
//a.contentEditable=true; 
a.style.bottom="0px"; 
a.style.right="0px"; 
document.body.appendChild(a); 
// --></mce:script> 
</body> 
</html>

代码有bug的地方还请大家多多包涵.
Javascript 相关文章推荐
HTA版JSMin(省略修饰语若干)基于javascript语言编写
Dec 24 Javascript
javascript 哈希表(hashtable)的简单实现
Jan 20 Javascript
JavaScript中也使用$美元符号来代替document.getElementById
Jun 19 Javascript
jquery实现网站超链接和图片提示效果
Mar 21 Javascript
$.each与$().each的区别示例介绍
Mar 20 Javascript
JavaScript获取当前网页标题(title)的方法
Apr 03 Javascript
js类式继承与原型式继承详解
Apr 07 Javascript
lhgcalendar时间插件限制只能选择三个月的实现方法
Jul 03 Javascript
javascript 通过键名获取键盘的keyCode方法
Dec 31 Javascript
jQuery实现浏览器之间跳转并传递参数功能【支持中文字符】
Mar 28 jQuery
Vue-cli项目部署到Nginx服务器的方法
Nov 01 Javascript
详解JavaScript中分解数字的三种方法
Jan 05 Javascript
javascript采用数组实现tab菜单切换效果
Dec 12 #Javascript
javascript仿qq界面的折叠菜单实现代码
Dec 12 #Javascript
日历查询的算法 如何计算某一天是星期几
Dec 12 #Javascript
javascript实现日历控件(年月日关闭按钮)
Dec 12 #Javascript
关于js中alert弹出窗口文本换行问题简单详细说明
Dec 11 #Javascript
用js判断页面是否加载完成实现代码
Dec 11 #Javascript
ajax页面无刷新 IE下遭遇Ajax缓存导致数据不更新的问题
Dec 11 #Javascript
You might like
php缓存技术介绍
2006/11/25 PHP
PHP中动态显示签名和ip原理
2007/03/28 PHP
PHP错误Cannot use object of type stdClass as array in错误的解决办法
2014/06/12 PHP
php查找字符串出现次数的方法
2014/12/01 PHP
php实现商城购物车的思路和源码分析
2020/07/23 PHP
jquery $(this).attr $(this).val方法使用介绍
2013/10/08 Javascript
原生javascript实现的一个简单动画效果
2016/03/30 Javascript
js组件SlotMachine实现图片切换效果制作抽奖系统
2016/04/17 Javascript
设计模式中的facade外观模式在JavaScript开发中的运用
2016/05/18 Javascript
jQuery源码解读之extend()与工具方法、实例方法详解
2017/03/30 jQuery
JS简单验证上传文件类型的方法
2017/04/17 Javascript
Nuxt.js SSR与权限验证的实现
2018/11/21 Javascript
react国际化化插件react-i18n-auto使用详解
2020/03/31 Javascript
在vue中实现嵌套页面(iframe)
2020/07/30 Javascript
Python 随机生成中文验证码的实例代码
2013/03/20 Python
利用Tkinter和matplotlib两种方式画饼状图的实例
2017/11/06 Python
python图像常规操作
2017/11/11 Python
python正则表达式及使用正则表达式的例子
2018/01/22 Python
python获取命令行输入参数列表的实例代码
2018/06/23 Python
使用tensorflow实现线性svm
2018/09/07 Python
Pyqt5实现英文学习词典
2019/06/24 Python
python 调试冷知识(小结)
2019/11/11 Python
python3 os进行嵌套操作的实例讲解
2020/11/19 Python
CSS3实现莲花绽放的动画效果
2020/11/06 HTML / CSS
Gloeilampgoedkoop荷兰:在线购买灯泡
2019/02/16 全球购物
捷克购买家具网站:JENA nábytek
2020/03/19 全球购物
求职简历中自我评价
2014/01/28 职场文书
顶撞老师检讨书
2014/02/07 职场文书
霸气押韵的班级口号
2014/06/09 职场文书
化学教育专业自荐信
2014/07/04 职场文书
我们的节日元宵活动方案
2014/08/23 职场文书
领导班子四风对照检查材料
2014/09/23 职场文书
离婚起诉书范本
2015/05/18 职场文书
丧事答谢词大全
2015/09/30 职场文书
MySQL系列之十四 MySQL的高可用实现
2021/07/02 MySQL
微信小程序APP的生命周期及页面的生命周期
2022/04/19 Javascript