javascript实现div的拖动并调整大小类似qq空间个性编辑模块_其他特效
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript实现div的拖动并调整大小类似qq空间个性编辑模块_其他特效,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3710字,纯文字阅读大概需要6分钟。
内容图文
![javascript实现div的拖动并调整大小类似qq空间个性编辑模块_其他特效](/upload/InfoBanner/zyjiaocheng/316/b255c672d25a4b60ba521c502163d51b.jpg)
实现步骤:
1.首先是动态创建一个类似这样的html结构:
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代码.
一下给出一个调用例子:
代码有bug的地方还请大家多多包涵.
内容总结
以上是互联网集市为您收集整理的javascript实现div的拖动并调整大小类似qq空间个性编辑模块_其他特效全部内容,希望文章能够帮你解决javascript实现div的拖动并调整大小类似qq空间个性编辑模块_其他特效所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。