本文總結(jié)一下,拖拽所延伸出來的一些效果,供大家參考,具體內(nèi)容如下
1.實(shí)現(xiàn)拖拉圖片時,帶框的效果。即當(dāng)鼠標(biāo)拖動某一個圖片或物體時,其原有位置扔保留其型。
這種效果,其實(shí)很簡單,主要是另外創(chuàng)建一個物體,使其與被拖拽的物體,寬和高一樣,然后,將其變?yōu)橥献У膶ο蟆?/P>
直接上代碼:
<html
<head>
<style>
#div1 {width:100px; height:100px; background:yellow; position:absolute;}
.box{border: 1px solid black;position: absolute;}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標(biāo)題文檔</title>
<script>
window.onload=function ()//凡是被拖拽的物體,其必須定位為absolute
{
var oDiv=document.getElementById('div1');
oDiv.onmousedown=function (ev)
{
var oEvent=ev||event;
var disX=oEvent.clientX-oDiv.offsetLeft;
var disY=oEvent.clientY-oDiv.offsetTop;
var oNewDiv=document.createElement('div');
oNewDiv.className='box';
oNewDiv.style.width=oDiv.offsetWidth-2+'px';//將2px的邊框去掉
oNewDiv.style.height=oDiv.offsetHeight-2+'px';
oNewDiv.style.left=oDiv.offsetLeft+'px';
oNewDiv.style.top=oDiv.offsetTop+'px';
document.body.appendChild(oNewDiv);
document.onmousemove=function (ev)
{
var oEvent=ev||event;
oNewDiv.style.left=oEvent.clientX-disX+'px';
oNewDiv.style.top=oEvent.clientY-disY+'px';
};
document.onmouseup=function ()
{
document.onmousemove=null;
document.onmouseup=null;
document.body.removeChild(oNewDiv);
oDiv.style.left=oNewDiv.style.left;
oDiv.style.top=oNewDiv.style.top;
};
};
};
</script>
</head>
<body>
<div id="div1">
</div>
</body>
</html>
2.關(guān)于窗口拖拉放大縮小的效果,就是在上面的div之中再包一個div。
<html
<head>
<style>
#div1 {width:10px; height:10px; background:url(images/1.gif); position:absolute;bottom: 0;right: 0}//拖拉的物體,改為圖片
#div2{width: 200px;height: 200px;position: relative;background: #ccc;}
.box{border: 1px solid black;position: absolute;}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標(biāo)題文檔</title>
<script>
window.onload=function ()//凡是被拖拽的物體,其必須定位為absolute
{
var oDiv=document.getElementById('div1');
var oDiv2=document.getElementById('div2');
oDiv.onmousedown=function (ev)
{
var oEvent=ev||event;
var disX=oEvent.clientX-oDiv.offsetLeft;
var disY=oEvent.clientY-oDiv.offsetTop;
var oNewDiv=document.createElement('div');
//oNewDiv.className='box';
oNewDiv.style.width=oDiv.offsetWidth-2+'px';
oNewDiv.style.height=oDiv.offsetHeight-2+'px';
oNewDiv.style.left=oDiv.offsetLeft+'px';
oNewDiv.style.top=oDiv.offsetTop+'px';
document.body.appendChild(oNewDiv);
document.onmousemove=function (ev)
{
var oEvent=ev||event;
oDiv2.style.width=oEvent.clientX-disX+'px';//這里是它的父級
oDiv2.style.height=oEvent.clientY-disY+'px';
};
document.onmouseup=function ()
{
document.onmousemove=null;
document.onmouseup=null;
document.body.removeChild(oNewDiv);
oDiv.style.left=oDiv2.style.left;
oDiv.style.top=oDiv2.style.top;
};
};
};
</script>
</head>
<body>
<div id='div2'>
<div id="div1">
</div>
</div>
</body>
</html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。