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 相关文章推荐
个人总结的一些关于String、Function、Array的属性和用法
Jan 10 Javascript
使用JQuery进行跨域请求
Jan 25 Javascript
字符串的replace方法应用浅析
Dec 06 Javascript
jquery 选取方法都有哪些
May 18 Javascript
js选择并转移导航菜单示例代码
Aug 19 Javascript
js解决select下拉选不中问题
Oct 14 Javascript
iframe里面的元素触发父窗口元素事件的jquery代码
Oct 19 Javascript
JS+CSS实现实用的单击输入框弹出选择框的方法
Feb 28 Javascript
深入理解JavaScript中的浮点数
May 18 Javascript
Swiper实现轮播图效果
Jul 03 Javascript
微信小程序实现漂亮的弹窗效果
May 26 Javascript
VUEX 数据持久化,刷新后重新获取的例子
Nov 12 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 split汉字
2009/06/05 PHP
php 模拟POST提交的2种方法详解
2013/06/17 PHP
PHP使用CURL_MULTI实现多线程采集的例子
2014/07/29 PHP
PHP全局使用Laravel辅助函数dd
2019/12/26 PHP
thinkphp诸多限制条件下如何getshell详解
2020/12/09 PHP
定义select的边框颜色
2008/04/28 Javascript
说明你的Javascript技术很烂的五个原因
2011/04/26 Javascript
纯JavaScript手写图片轮播代码
2016/10/20 Javascript
Vue2.x中的Render函数详解
2017/05/30 Javascript
JavaScript 中的 this 简单规则
2017/09/19 Javascript
VUE + UEditor 单图片跨域上传功能的实现方法
2018/02/08 Javascript
node中modules.exports与exports导出的区别
2018/06/08 Javascript
[03:01]DOTA2英雄基础教程 露娜
2014/01/07 DOTA
[00:30]塑造者的传承礼包-戴泽“暗影之焰”套装展示视频
2014/04/04 DOTA
Python内置函数bin() oct()等实现进制转换
2012/12/30 Python
python3编码问题汇总
2016/09/06 Python
Python xlwt设置excel单元格字体及格式
2020/04/18 Python
python实现12306抢票及自动邮件发送提醒付款功能
2018/03/08 Python
python ChainMap 合并字典的实现步骤
2019/06/11 Python
python数据爬下来保存的位置
2020/02/17 Python
在python中使用nohup命令说明
2020/04/16 Python
解决Jupyter Notebook使用parser.parse_args出现错误问题
2020/04/20 Python
python dict乱码如何解决
2020/06/07 Python
python环境搭建和pycharm的安装配置及汉化详细教程(零基础小白版)
2020/08/19 Python
Jowissa官方网站:瑞士制造的手表,优雅简约的设计
2020/07/29 全球购物
某公司的.net工程师面试题笔试题
2013/11/22 面试题
正隆泰信息技术有限公司上机题
2012/06/14 面试题
美食节策划方案
2014/05/26 职场文书
政治学求职信
2014/06/03 职场文书
暑期培训班策划方案
2014/08/26 职场文书
自我查摆剖析材料
2014/10/11 职场文书
倡议书格式及范文
2015/04/29 职场文书
2016元旦主持人经典开场白台词
2015/12/03 职场文书
欧元符号 €
2022/02/17 杂记
alibaba seata服务端具体实现
2022/02/24 Java/Android
python playwrigh框架入门安装使用
2022/07/23 Python