js+html+css實(shí)現(xiàn)鼠標(biāo)移動(dòng)div實(shí)例
更新時(shí)間:2013年01月30日 18:05:15 作者:
移動(dòng)div對(duì)于很多的網(wǎng)有們來說是一件很熟悉的事了,本文老生長談,用js實(shí)現(xiàn)鼠標(biāo)移動(dòng)div,希望大伙們可以舉一反三,感興趣的朋友可以參考下,或許本文對(duì)你有所幫助
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)文章
JS實(shí)現(xiàn)網(wǎng)頁煙花動(dòng)畫效果
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)網(wǎng)頁煙花動(dòng)畫效果,點(diǎn)擊鼠標(biāo)煙花向四周擴(kuò)散,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03JS自動(dòng)生成動(dòng)態(tài)HTML驗(yàn)證碼頁面
這篇文章主要介紹了JS自動(dòng)生成動(dòng)態(tài)HTML驗(yàn)證碼頁面,輸入錯(cuò)誤自動(dòng)清空輸入框的功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06json實(shí)現(xiàn)添加、遍歷與刪除屬性的方法
這篇文章主要介紹了json實(shí)現(xiàn)添加、遍歷與刪除屬性的方法,結(jié)合簡單實(shí)例形式分析了json常見的添加、遍歷與刪除操作相關(guān)技巧,需要的朋友可以參考下2016-06-06JavaScript?error瀏覽器端錯(cuò)誤捕獲處理方法筆記解決示例
這篇文章主要為大家介紹了JavaScript?error瀏覽器端錯(cuò)誤捕獲處理方法筆記解決示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06微信小程序 scroll-view 實(shí)現(xiàn)錨點(diǎn)跳轉(zhuǎn)功能
這篇文章主要介紹了微信小程序 scroll-view 實(shí)現(xiàn)錨點(diǎn)跳轉(zhuǎn)功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12layui 數(shù)據(jù)表格 根據(jù)值(1=業(yè)務(wù),2=機(jī)構(gòu))顯示中文名稱示例
今天小編就為大家分享一篇layui 數(shù)據(jù)表格 根據(jù)值(1=業(yè)務(wù),2=機(jī)構(gòu))顯示中文名稱示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10利用types增強(qiáng)vscode中js代碼提示功能詳解
這篇文章主要給大家介紹了如何增強(qiáng)vscode中js代碼提示功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。2017-07-07