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 相关文章推荐
Prototype使用指南之array.js
Jan 10 Javascript
深入了解JavaScript中的Symbol的使用方法
Jul 28 Javascript
jQuery鼠标经过方形图片切换成圆边效果代码分享
Aug 20 Javascript
基于JavaScript实现弹出框效果
Feb 19 Javascript
只要1K 纯JS脚本送你一朵3D红色玫瑰
Aug 09 Javascript
老生常谈jquery id选择器和class选择器的区别
Feb 12 Javascript
使用ionic在首页新闻中应用到的跑马灯效果的实现方法
Feb 13 Javascript
React Native之prop-types进行属性确认详解
Dec 19 Javascript
JavaScript中的&quot;=、==、===&quot;区别讲解
Jan 22 Javascript
el-select 下拉框多选实现全选的实现
Aug 02 Javascript
Vue+element+cookie记住密码功能的简单实现方法
Sep 20 Javascript
vue绑定class的三种方法
Dec 24 Vue.js
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(1) php开发环境配置
2010/02/15 PHP
Laravel 5框架学习之向视图传送数据
2015/04/08 PHP
laravel自定义分页效果
2017/07/23 PHP
Javascript 表单之间的数据传递代码
2008/12/04 Javascript
jquery1.4后 jqDrag 拖动 不可用
2010/02/06 Javascript
获取3个数组不重复的值的具体实现
2013/12/30 Javascript
js 实现浏览历史记录示例
2014/04/20 Javascript
jQuery实现指定内容滚动同时左侧或其它地方不滚动的方法
2015/08/08 Javascript
深入理解jQuery()方法的构建原理
2016/12/05 Javascript
JavaScript求一个数组中重复出现次数最多的元素及其下标位置示例
2018/07/23 Javascript
JS实现的冒泡排序,快速排序,插入排序算法示例
2019/03/02 Javascript
Vue+iview+webpack ie浏览器兼容简单处理
2019/09/20 Javascript
[00:42]《辉夜杯》—职业组预选赛12月3日15点 正式打响
2015/12/03 DOTA
Python正则表达式教程之二:捕获篇
2017/03/02 Python
Python实现的排列组合计算操作示例
2017/10/13 Python
Python科学画图代码分享
2017/11/29 Python
Django中的CBV和FBV示例介绍
2018/02/25 Python
Python基于socket模块实现UDP通信功能示例
2018/04/10 Python
python+opencv+caffe+摄像头做目标检测的实例代码
2018/08/03 Python
python实现简单图片物体标注工具
2019/03/18 Python
Flask框架路由和视图用法实例分析
2019/11/07 Python
Python getattr()函数使用方法代码实例
2020/08/10 Python
Python中正则表达式对单个字符,多个字符和匹配边界等使用
2021/01/27 Python
HMV日本官网:全球知名的音乐、DVD和电脑游戏零售巨头
2016/08/13 全球购物
世界著名的顶级牛排:Omaha Steak(奥马哈牛排)
2016/09/20 全球购物
Joules美国官网:出色的英国风格
2017/10/30 全球购物
怀旧收藏品和经典纪念品:Betty’s Attic
2018/08/29 全球购物
亚马逊海外购:亚马逊美国、英国、日本、德国直邮
2021/03/18 全球购物
计算机开发个人求职信范文
2013/09/26 职场文书
高三生物教学反思
2014/01/25 职场文书
英语课外活动总结
2014/08/27 职场文书
幼儿园教师师德师风演讲稿:爱我所爱 无悔青春
2014/09/10 职场文书
学生无故旷课检讨书
2014/09/20 职场文书
检查机关领导群众路线教育实践活动个人整改措施
2014/10/28 职场文书
晚会主持人开场白台词
2015/05/28 职场文书
MySQL池化框架学习接池自定义
2022/07/23 MySQL