欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS實(shí)現(xiàn)彈出浮動(dòng)窗口(支持鼠標(biāo)拖動(dòng)和關(guān)閉)實(shí)例詳解

 更新時(shí)間:2015年08月06日 11:53:40   作者:皮蛋  
這篇文章主要介紹了JS實(shí)現(xiàn)彈出浮動(dòng)窗口,可支持鼠標(biāo)拖動(dòng)和關(guān)閉的功能,界面美觀大方,涉及javascript動(dòng)態(tài)創(chuàng)建對(duì)話框的相關(guān)技巧,需要的朋友可以參考下

本文實(shí)例講述了JS實(shí)現(xiàn)彈出浮動(dòng)窗口。分享給大家供大家參考。具體如下:

這里介紹的JS彈出浮動(dòng)窗口,支持鼠標(biāo)拖動(dòng)和關(guān)閉,點(diǎn)擊鏈接文字后彈出層窗口,也稱作是彈出式對(duì)話框吧。

關(guān)于一些參數(shù)說明:

bodycontent:要在窗口中顯示的內(nèi)容
title:窗口的標(biāo)題
removeable:窗口是否能拖動(dòng)

注意:內(nèi)容窗體的高度是height-30px,請(qǐng)計(jì)算好要顯示的內(nèi)容高度和寬度。

注:在火狐或chrome下效果最佳,IE8下可能有些小問題。

點(diǎn)擊此處查看運(yùn)行效果:

http://demo.jb51.net/js/2015/js-float-window-mousemove-codes/

運(yùn)行效果圖如下:

具體代碼:

<html>
<head>
<title>Js彈出浮動(dòng)窗口,支持鼠標(biāo)拖動(dòng)和關(guān)閉</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
/**
關(guān)于一些參數(shù)說明:
*bodycontent:要在窗口顯示的內(nèi)容,dom對(duì)象
*title:窗口標(biāo)題,字符串類型
*removeable:窗口能否拖動(dòng),布爾類型
*注意:內(nèi)容窗體的高度是height-30px,請(qǐng)計(jì)算好你要顯示的內(nèi)容的高度和寬度。彈出窗的id為"223238909",所以你的頁(yè)面不要再取值為"223238909"的id了,以防js執(zhí)行出錯(cuò)*/
function createdialog(width,height,bodycontent,title,removeable){
 if(document.getElementById("www_jb51_net")==null){
  /*創(chuàng)建窗口的組成元素*/
  var dialog = document.createElement("div");
  var dialogtitlebar= document.createElement("div");
  var dialogbody = document.createElement("div");
  var dialogtitleimg = document.createElement("span");
  var dialogtitle = document.createElement("span");
  var dialogclose = document.createElement("span");
  var closeaction = document.createElement("button");
  /*為窗口設(shè)置一個(gè)id,id如此怪異是為了盡量避免與其他用戶取的id相同而出錯(cuò)*/
  dialog.id = "223238909";
  /*組裝對(duì)話框標(biāo)題欄,按從里到外的順序組裝*/
  dialogtitle.innerHTML = title;
  dialogtitlebar.appendChild(dialogtitleimg);
  dialogtitlebar.appendChild(dialogtitle);
  dialogtitlebar.appendChild(dialogclose);
  dialogclose.appendChild(closeaction);
  /*組裝對(duì)話框主體內(nèi)容*/
  if(bodycontent!=null){
   bodycontent.style.display = "block";
   dialogbody.appendChild(bodycontent);
  }
  /*組裝成完整的對(duì)話框*/
  dialog.appendChild(dialogtitlebar);
  dialog.appendChild(dialogbody);
  /*設(shè)置窗口組成元素的樣式*/
  var templeft,temptop,tempheight//窗口的位置(將窗口放在頁(yè)面中間的輔助變量)
  var dialogcssText,dialogbodycssText;//拼出dialog和dialogbody的樣式字符串
  templeft = (document.body.clientWidth-width)/2;
  temptop = (document.body.clientHeight-height)/2;
  tempheight= height-30;
 dialogcssText= "position:absolute;background:#65c294;padding:1px;border:4px;top:"+temptop+"px;left:"+templeft+"px;height:"+height+"px;width:"+width+"px;";
  dialogbodycssText = "width:100%;background:#ffffff;"+"height:" + tempheight + "px;";
  dialog.style.cssText = dialogcssText;
  dialogtitlebar.style.cssText = "height:30px;width:100%;background:url(images/titlebar.png) repeat;cursor:move;";
  dialogbody.style.cssText  = dialogbodycssText;
  dialogtitleimg.style.cssText = "float:left;height:20px;width:20px;background:url(images/40.gif);"+"display:block;margin:4px;line-height:20px;";
  dialogtitle.style.cssText = "font-size:16px;float:left;display:block;margin:4px;line-height:20px;";
  dialogclose.style.cssText  = "float:right;display:block;margin:4px;line-height:20px;";
  closeaction.style.cssText = "height:20px;width:24px;border-width:1px;"+"background-image:url(images/close.png);cursor:pointer;";
  /*為窗口元素注冊(cè)事件*/
  var dialogleft = parseInt(dialog.style.left);
  var dialogtop = parseInt(dialog.style.top);
  var ismousedown = false;//標(biāo)志鼠標(biāo)是否按下
  /*關(guān)閉按鈕的事件*/       
  closeaction.onclick = function(){
   dialog.parentNode.removeChild(dialog);
  }
  /*實(shí)現(xiàn)窗口的移動(dòng),這段代碼很典型,網(wǎng)上很多類似的代碼*/
  if(removeable == true){
   var ismousedown = false;
   var dialogleft,dialogtop;
   var downX,downY;
   dialogleft = parseInt(dialog.style.left);
   dialogtop = parseInt(dialog.style.top);
   dialogtitlebar.onmousedown = function(e){
   ismousedown = true;
   downX = e.clientX;
   downY = e.clientY;
   }
   document.onmousemove = function(e){
    if(ismousedown){
    dialog.style.top = e.clientY - downY + dialogtop + "px";
    dialog.style.left = e.clientX - downX + dialogleft + "px";
    }
   }
   /*松開鼠標(biāo)時(shí)要重新計(jì)算當(dāng)前窗口的位置*/
   document.onmouseup = function(){
    dialogleft = parseInt(dialog.style.left);
    dialogtop = parseInt(dialog.style.top);
    ismousedown = false;
   }
  }
  return dialog; 
 }//end if(if的結(jié)束)
}
</script>
<style>
table{background:#b2d235;}
button{font-size:12px;height:23;background:#ece9d8;border-width:1;}
#linkurl,#linkname,#savelink{width:100px;}
</style>
</head>
<body>
<!-- 顯示窗口的地方 -->
<div id="here"></div><a id="clickhere" href="#">點(diǎn)擊生成窗口</a>
<!-- 要嵌入到窗口的內(nèi)容 -->
<div id="login" style="display:none;">
 <form action="#" method="post" onSubmit="return false;">
  <table width="400" height="95">
  <tr>
   <td width="78">鏈接文字</td>
   <td width="168"><input name="link.name" type="text"/></td>
   <td width="138" id="linktext"></td>
  </tr>
  <tr>
   <td>鏈接地址</td>
   <td><input name="link.url" type="text"/></td>
   <td id="linkurl"></td>
  </tr>
  <tr>
   <td></td>
   <td><button type="submit" style="float:right;">添加</button></td>
   <td id="savelink"></td>
  </tr>
  </table>
</form>
</div>
<script type="text/javascript">
var here = document.getElementById("here");
var login = document.getElementById("login");
var clickhere = document.getElementById("clickhere");
clickhere.onclick = function(){
here.appendChild(createdialog(400,95+30,login,"歡迎光臨腳本之家",true));
}
</script>
</body>
</html>

希望本文所述對(duì)大家的javascript程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • 利用js+canvas實(shí)現(xiàn)掃雷游戲

    利用js+canvas實(shí)現(xiàn)掃雷游戲

    這篇文章主要為大家詳細(xì)介紹了利用js+canvas實(shí)現(xiàn)掃雷游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • webpack前端應(yīng)用之基礎(chǔ)打包實(shí)現(xiàn)

    webpack前端應(yīng)用之基礎(chǔ)打包實(shí)現(xiàn)

    本文主要介紹了webpack前端應(yīng)用之基礎(chǔ)打包實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • Grid得到選擇行數(shù)據(jù)的方法總結(jié)

    Grid得到選擇行數(shù)據(jù)的方法總結(jié)

    Grid得到選擇行數(shù)據(jù)的方法總結(jié),需要的朋友可以參考下。
    2011-01-01
  • 一個(gè)js控制的導(dǎo)航菜單實(shí)例代碼

    一個(gè)js控制的導(dǎo)航菜單實(shí)例代碼

    這篇文章主要是對(duì)js控制的導(dǎo)航菜單實(shí)例代碼進(jìn)行了介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助
    2013-12-12
  • 淺析JavaScript中嚴(yán)格模式的使用

    淺析JavaScript中嚴(yán)格模式的使用

    在ECMAScript5標(biāo)準(zhǔn)中,JavaScript提出了嚴(yán)格模式的概念(Strict Mode),本文就來和大家簡(jiǎn)單講講JavaScript中嚴(yán)格模式的具體使用,感興趣的可以了解一下
    2023-05-05
  • 基于微信小程序?qū)崿F(xiàn)透明背景人像分割功能

    基于微信小程序?qū)崿F(xiàn)透明背景人像分割功能

    這篇文章主要介紹了基于小程序?qū)崿F(xiàn)透明背景人像分割,此文主要實(shí)現(xiàn)識(shí)別人體的輪廓范圍,與背景進(jìn)行分離并保存效果圖,適用于拍照背景替換及透明背景的人像圖(png格式)轉(zhuǎn)換,需要的朋友可以參考下
    2022-10-10
  • JavaScript實(shí)現(xiàn)動(dòng)態(tài)時(shí)鐘效果

    JavaScript實(shí)現(xiàn)動(dòng)態(tài)時(shí)鐘效果

    這篇文章主要介紹了JavaScript實(shí)現(xiàn)動(dòng)態(tài)時(shí)鐘效果,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下,希望對(duì)你的學(xué)習(xí)有所幫助
    2022-06-06
  • 微信小程序使用wx.chooseLocation開發(fā)地圖怎么做

    微信小程序使用wx.chooseLocation開發(fā)地圖怎么做

    這篇文章主要介紹了微信小程序使用wx.chooseLocation開發(fā)地圖流程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2022-12-12
  • 細(xì)說JavaScript中的this指向與綁定規(guī)則

    細(xì)說JavaScript中的this指向與綁定規(guī)則

    本文主要詳細(xì)介紹了JavaScript中的this指向與綁定規(guī)則,默認(rèn)綁定,隱式綁定,顯示綁定,new綁定這四個(gè)規(guī)則,文中有相關(guān)的代碼示例供大家參考,感興趣的同學(xué)可以閱讀下
    2023-05-05
  • javascript實(shí)現(xiàn)圖片輪換動(dòng)作方法

    javascript實(shí)現(xiàn)圖片輪換動(dòng)作方法

    這篇文章主要介紹了javascript實(shí)現(xiàn)圖片輪換動(dòng)作方法,文章通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08

最新評(píng)論