js+html+css實現(xiàn)鼠標(biāo)移動div實例
更新時間:2013年01月30日 18:05:15 作者:
移動div對于很多的網(wǎng)有們來說是一件很熟悉的事了,本文老生長談,用js實現(xiàn)鼠標(biāo)移動div,希望大伙們可以舉一反三,感興趣的朋友可以參考下,或許本文對你有所幫助
js:
var posX;
var posY;
fdiv = document.getElementById("divBody");
document.getElementById("divHead").onmousedown=function(e)
{
if(!e) e = window.event; //IE
posX = e.clientX - parseInt(fdiv.style.left);
posY = e.clientY - parseInt(fdiv.style.top);
document.onmousemove = mousemove;
}
document.onmouseup = function()
{
document.onmousemove = null;
}
function mousemove(ev)
{
if(ev==null) ev = window.event;//IE
fdiv.style.left = (ev.clientX - posX) + "px";
fdiv.style.top = (ev.clientY - posY) + "px";
}
html:
<div class="divBody" id="divBody" style="left: 29px; top: 14px;"> <!--這里要加style="left: 29px; top: 14px;" 否則有問題-->
<div class="divHead" id="divHead" style="cursor: move;">
</div>
<div class="content">
</div>
<div class="tail">
</div>
</div>
css:
.divBody{
//margin-top:20px;
border: solid #CCC 1px;
width:500px;
height:400px;
position:relative;
z-index:0;
margin-left:auto;
margin-right:auto;
}
.divHead{
width:500px;
height:50px;
background-color:#CCC;
}
.content
{
width:500px;
height:300px;
}
.tail{
background:#CCC;
height:50px;
width:500px;
display:table-cell;
vertical-align:middle;
}
復(fù)制代碼 代碼如下:
var posX;
var posY;
fdiv = document.getElementById("divBody");
document.getElementById("divHead").onmousedown=function(e)
{
if(!e) e = window.event; //IE
posX = e.clientX - parseInt(fdiv.style.left);
posY = e.clientY - parseInt(fdiv.style.top);
document.onmousemove = mousemove;
}
document.onmouseup = function()
{
document.onmousemove = null;
}
function mousemove(ev)
{
if(ev==null) ev = window.event;//IE
fdiv.style.left = (ev.clientX - posX) + "px";
fdiv.style.top = (ev.clientY - posY) + "px";
}
html:
復(fù)制代碼 代碼如下:
<div class="divBody" id="divBody" style="left: 29px; top: 14px;"> <!--這里要加style="left: 29px; top: 14px;" 否則有問題-->
<div class="divHead" id="divHead" style="cursor: move;">
</div>
<div class="content">
</div>
<div class="tail">
</div>
</div>
css:
復(fù)制代碼 代碼如下:
.divBody{
//margin-top:20px;
border: solid #CCC 1px;
width:500px;
height:400px;
position:relative;
z-index:0;
margin-left:auto;
margin-right:auto;
}
.divHead{
width:500px;
height:50px;
background-color:#CCC;
}
.content
{
width:500px;
height:300px;
}
.tail{
background:#CCC;
height:50px;
width:500px;
display:table-cell;
vertical-align:middle;
}
相關(guān)文章
JavaScript?error瀏覽器端錯誤捕獲處理方法筆記解決示例
這篇文章主要為大家介紹了JavaScript?error瀏覽器端錯誤捕獲處理方法筆記解決示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06微信小程序 scroll-view 實現(xiàn)錨點跳轉(zhuǎn)功能
這篇文章主要介紹了微信小程序 scroll-view 實現(xiàn)錨點跳轉(zhuǎn)功能,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-12-12layui 數(shù)據(jù)表格 根據(jù)值(1=業(yè)務(wù),2=機構(gòu))顯示中文名稱示例
今天小編就為大家分享一篇layui 數(shù)據(jù)表格 根據(jù)值(1=業(yè)務(wù),2=機構(gòu))顯示中文名稱示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10