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

css样式

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

jquery左右滚动焦点图banner图片,鼠标经过显示上下页
适合宽和高都比较大的页面使用
澳门新浦京app下载 1 
演示

复制代码 代码如下:.remind span
{display:block;margin-top:-22px;}.remind a:hover
{padding-top:22px;}.remind a
{float:left;overflow:hidden;}.remind{height:22px;line-height:22px;overflow:hidden;padding-left:10px;}

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

复制代码 代码如下:

html代码

复制代码 代码如下:

<div class=”bannerbox”>
<div id=”focus”>
<ul>
<li><a href=”” target=”_blank”>
<img src=”../dandong.png” alt=”” /></a></li>
<li><a href=”” target=”_blank”>
<img src=”../mohe.png” alt=”” /></a></li>
<li><a href=”” target=”_blank”>
<img src=”../changbaishan.png” alt=”” /></a></li>
<li><a href=”” target=”_blank”>
<img src=”../erlianhaote.png” alt=”” /></a></li>
</ul>
</div>
</div>

复制代码 代码如下:div a
href=”#”span如需修改帐号,请发送修改信息及相关证明至邮箱/span提示/a/div

<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.然后将事件绑定到这些对象上.具体看一下代码.

@charset “utf-8”;
img { border: 0px; }
.bannerbox { width: 320px; height: 150px; overflow: hidden; margin: 0px
auto; }
#focus { width: 320px; height: 150px; clear: both; overflow: hidden;
position: relative; float: left; }
#focus ul { width: 320px; height: 150px; float: left; position:
absolute; clear: both; padding: 0px; margin: 0px; }
#focus ul li { float: left; width: 320px; height: 150px; overflow:
hidden; position: relative; padding: 0px; margin: 0px; }
#focus .preNext { width: 250px; height: 150px; position: absolute; top:
0px; cursor: pointer; }
#focus .pre { left: 0; background: url(../images/sprite.png) no-repeat
left center; }
#focus .next { rightright: 0; background: url(../images/sprite1.png)
no-repeat rightright center; }

鼠标经过变换文字

复制代码 代码如下:

js文件

HOME首页 NEWS新闻 ABOUT关于 CONTACT联系 照片PHOTO

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){

复制代码 代码如下:

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

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;
}

$(function () {
var sWidth = $(“#focus”).width();
var len = $(“#focus ul li”).length;
var index = 0;
var picTimer;
var btn = “<div class=’btnBg’></div><div
class=’btn’>”;
for (var i = 0; i < len; i++) {
btn += “<span></span>”;
}
btn += “</div><div class=’preNext pre’></div><div
class=’preNext next’></div>”;
$(“#focus”).append(btn);
$(“#focus .btnBg”).css(“opacity”, 0);
$(“#focus .btn span”).css(“opacity”, 0.4).mouseenter(function () {
index = $(“#focus .btn span”).index(this);
showPics(index);
}).eq(0).trigger(“mouseenter”);
$(“#focus .preNext”).css(“opacity”, 0.0).hover(function () {
$(this).stop(true, false).animate({ “opacity”: “0.5” }, 300);
}, function () {
$(this).stop(true, false).animate({ “opacity”: “0” }, 300);
});
$(“#focus .pre”).click(function () {
index -= 1;
if (index == -1) { index = len – 1; }
showPics(index);
});
$(“#focus .next”).click(function () {
index += 1;
if (index == len) { index = 0; }
showPics(index);
});
$(“#focus ul”).css(“width”, sWidth * (len));
$(“#focus”).hover(function () {
clearInterval(picTimer);
}, function () {
picTimer = setInterval(function () {
showPics(index);
index++;
if (index == len) { index = 0; }
}, 2800);
}).trigger(“mouseleave”);
function showPics(index) {
var nowLeft = -index * sWidth;
$(“#focus ul”).stop(true, false).animate({ “left”: nowLeft }, 300);
$(“#focus .btn span”).stop(true, false).animate({ “opacity”: “0.4” },
300).eq(index).stop(true, false).animate({ “opacity”: “1” }, 300);
}
});

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

适合宽和高都比较大的页面使用 演示 复制代码 代码如下: div
class=”bannerbox” div id=”focus”…

函数的参数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的地方还请大家多多包涵.

You can leave a response, or trackback from your own site.

Leave a Reply

网站地图xml地图