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

JS實現(xiàn)可移動模態(tài)框

 更新時間:2022年07月05日 17:30:23   作者:yanbabyo  
這篇文章主要為大家詳細介紹了JS實現(xiàn)可移動模態(tài)框,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了JS實現(xiàn)可移動模態(tài)框的具體代碼,供大家參考,具體內(nèi)容如下

點擊增加彈出模態(tài)框。

這個模態(tài)框可以移動的。

由于我寫的項目是egg.js前后端分離,需要獲取數(shù)據(jù)庫內(nèi)容,所以以下代碼中的{{uh.username}}自己根據(jù)實際情況進行修改。

1.首先在前端頁面中添加以下代碼:

<div class="content-top">
? ? ? ? <button type="submit" class="open">增加</button>
? ? </div>
? ? <div class="model-box">
? ? ? ? <div class="content">
? ? ? ? ? ? <div class="title">
? ? ? ? ? ? ? ? <span>增加</span>
? ? ? ? ? ? ? ? <i class="close">×</i>
? ? ? ? ? ? </div>
? ? ? ? ? ? <form method="POST" action="/add" style="height: 250px;">
? ? ? ? ? ? ? ? <div class="form-input">
? ? ? ? ? ? ? ? ? ? <label for="username" >用戶名</label>
? ? ? ? ? ? ? ? ? ? <input type="text" name="username" value={{uh.username}}>{{ uh.username }}
? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? ? ? <div class="form-input">
? ? ? ? ? ? ? ? ? ? <label for="username">密碼</label>
? ? ? ? ? ? ? ? ? ? <input type="password" name="password" value={{uh.password}}>{{ uh.password }}
? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? ? ? <div class="form-input">
? ? ? ? ? ? ? ? ? ? <button type="submit">提交</button>
? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? </form>
? ? ? ? </div>
</div>

2.css樣式

* {
? ?padding: 0;
? ?margin: 0;
}
.content-top button {
? ?/* 取消按鈕自帶的輪廓 */
? ?outline: 0;
? ?width: 100px;
? ?height: 40px;
? ?color: #409eff;
? ?/* 圓角屬性 */
? ?border-radius: 4px;
? ?border: 1px solid #b3d8ff;
? ?background-color: #ecf5ff;
? ?/* 過渡 */
? ?transition: all 0.3s;
? ?cursor: pointer;
}
.content-top button:hover {
? ?color: #fff;
? ?border-color: #409eff;
? ?background-color: #409eff;
}
.model-box button {
? ?/* 取消按鈕自帶的輪廓 */
? ?outline: 0;
? ?width: 100px;
? ?height: 40px;
? ?color: #409eff;
? ?/* 圓角屬性 */
? ?border-radius: 4px;
? ?border: 1px solid #b3d8ff;
? ?background-color: #ecf5ff;
? ?/* 過渡 */
? ?transition: all 0.3s;
? ?/* 鼠標變小手 */
? ?cursor: pointer;
}
.model-box button:hover {
? ?color: #fff;
? ?border-color: #409eff;
? ?background-color: #409eff;
}
/* 模態(tài)框 start */
.model-box {
? ?/* 隱藏模態(tài)框 */
? ?display: none;
? ?position: fixed;
? ?top: 50px;
? ?left: 80px;
? ?width: 100%;
}
.model-box .content {
? ?position: absolute;
? ?top: 5px;
? ?/* calc方法可以自動計算數(shù)值 */
? ?left: calc(50% - 210px);
? ?width: 420px;
? ?border-radius: 5px;
? ?padding: 0 20px;
? ?/* 盒子陰影 */
? ?box-shadow: 0 2px 12px rgba(0, 0, 0, 0.2);
? ?background-color: #fff;
}
.model-box .content .title {
? ?/* 彈性布局 */
? ?display: flex;
? ?/* 讓子元素水平與兩端對齊 */
? ?justify-content: space-between;
? ?height: 60px;
? ?line-height: 60px;
? ?/* 鼠標移入呈現(xiàn)移動光標 */
? ?cursor: move;
}
.model-box .content .title span {
? ?font-size: 18px;
? ?color: #333;
}
.model-box .content .title i {
? ?/* i標簽?zāi)J是斜體 這個屬性可以變正 */
? ?font-style: normal;
? ?font-size: 24px;
? ?color: #909399;
? ?cursor: pointer;
}
/* 鼠標移入變色 */
.model-box .content .title i:hover {
? ?color: #409eff;
}
.model-box .content form .form-input {
? ?margin: 5px 0;
}
/* 因為label元素的for屬性和input元素id屬性設(shè)置了相同的屬性值 所以就可以通過label元素選中 輸入框 布局已完成 ?*/
.model-box .content form .form-input label {
? ?font-size: 14px;
? ?color: #606266;
? ?cursor: pointer;
}
.model-box .content form .form-input input {
? ?/* 取消輸入框默認的輪廓 */
? ?outline: 0;
? ?width: 100%;
? ?height: 30px;
? ?padding: 0 8px;
? ?margin-top: 12px;
? ?border: 1px solid #dcdfe6;
? ?border-radius: 4px;
}
.model-box .content form .form-input input:hover {
? ?border-color: #c0c4cc;
}
/* 輸入框獲取焦點變色 */
.model-box .content form .form-input input:focus {
? ?border-color: #409eff;
}
.model-box .content form .form-input button {
? ?/* 讓按鈕浮動到右側(cè) */
? ?float: right;
? ?margin-top: 10px;
}

3.js部分

// 添加頁面加載事件
window.addEventListener("load", () => {
?? ?// 獲取打開按鈕
?? ?const open = document.querySelector(".open");
?? ?// 獲取關(guān)閉按鈕
?? ?const close = document.querySelector(".close");
?? ?// 獲取整個大的模態(tài)框
?? ?const fillScreen = document.querySelector(".model-box");

?? ?// 獲取模態(tài)框可移動的頭部區(qū)域
?? ?const header = document.querySelector(".title");

?? ?// 獲取模態(tài)框珠主區(qū)域
?? ?const modelBox = document.querySelector(".content");

?? ?//element.addEventListener() 方法為指定元素添加事件句柄
?? ?// 打開功能
?? ?if(open){
?? ?open.addEventListener("click", () => {
?? ??? ?// 點擊打開按鈕 改變display屬性值
?? ??? ?fillScreen.style.display = "block";
?? ?});
? ?}

?? ?// 關(guān)閉功能
?? ?if(close){
?? ?close.addEventListener("click", () => {
?? ??? ?fillScreen.style.display = "none";
?? ?});
? ? }
?? ?// 移動功能 為header添加鼠標按下事件
?? ?if(header){
?? ?header.addEventListener("mousedown", (event) => {
?? ??? ?// 讓模態(tài)框移動 需要知道鼠標在header區(qū)域的光標位置 計算方式 是先算出鼠標光標在整個瀏覽器區(qū)域的位置 再算出模態(tài)框距離瀏覽器邊緣位置的大小 相減
?? ??? ?// event.pageX可以獲取鼠標光標距離瀏覽器邊緣位置的大小
?? ??? ?// modelBox.offsetLeft 可以獲取到模態(tài)框區(qū)里瀏覽器左邊框的距離
?? ??? ?const x = event.pageX - modelBox.offsetLeft;
?? ??? ?const y = event.pageY - modelBox.offsetTop;
?? ??? ?console.log(x, y);
?? ??? ?// 在按下事件內(nèi)添加移動事件
?? ??? ?document.addEventListener("mousemove", move);
?? ??? ?// 做鼠標彈起事件?
?? ??? ?function move(event) {
?? ??? ??? ?// 算出移動時的模態(tài)框的位置距離 并賦值 原理和上面求x,y一樣
?? ??? ??? ?// css屬性值需要單位?
?? ??? ??? ?modelBox.style.left = event.pageX - x + "px";
?? ??? ??? ?modelBox.style.top = event.pageY - y + "px";
?? ??? ?}
?? ??? ?//onmouseup 當松開鼠標按鈕時運行腳本 ? ?removeEventListener移除由 addEventListener() 方法添加的 "mousemove" 事件
?? ??? ?document.addEventListener("mouseup", () => {
?? ??? ??? ?document.removeEventListener("mousemove", move);
?? ??? ?});
?? ?});
}
});

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 淺談JavaScript正則表達式-非捕獲性分組

    淺談JavaScript正則表達式-非捕獲性分組

    下面小編就為大家?guī)硪黄狫avaScript正則表達式-非捕獲性分組。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03
  • div當滾動到頁面頂部的時候固定在頂部實例代碼

    div當滾動到頁面頂部的時候固定在頂部實例代碼

    使用Javascript實現(xiàn)了滾動頁面時,DIV到達頂部時固定在頂部。在IE下效果有點閃,具體代碼如下,感興趣的朋友可以參考下哈
    2013-05-05
  • javascript 系統(tǒng)文件夾文件操作及參數(shù)介紹

    javascript 系統(tǒng)文件夾文件操作及參數(shù)介紹

    javascript文件操作包括寫入文件、當前目錄文件、讀文件、刪除文件、批量刪除,未刪除文件夾,刪除不了當前目錄文件等等,感興趣的朋友可以參考下
    2013-01-01
  • js逆向解密之網(wǎng)絡(luò)爬蟲

    js逆向解密之網(wǎng)絡(luò)爬蟲

    在本篇內(nèi)容里小編給大家整理的是關(guān)于js逆向解密之網(wǎng)絡(luò)爬蟲的相關(guān)知識點內(nèi)容,需要的朋友們參考下。
    2019-05-05
  • 基于HTML5上使用iScroll實現(xiàn)下拉刷新,上拉加載更多

    基于HTML5上使用iScroll實現(xiàn)下拉刷新,上拉加載更多

    本文主要介紹在HTML5中使用iScroll實現(xiàn)下拉刷新,上拉加載更多數(shù)據(jù)的方法,主要就是寫了兩個自定義函數(shù)pullDownAction和pullUpAction,分別在下拉和上拉的事件中調(diào)用他們。
    2016-05-05
  • JavaScript鍵盤事件響應(yīng)順序詳解

    JavaScript鍵盤事件響應(yīng)順序詳解

    這篇文章主要為大家詳細介紹了JavaScript鍵盤事件響應(yīng)順序,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • js禁止頁面復(fù)制功能禁用頁面右鍵菜單示例代碼

    js禁止頁面復(fù)制功能禁用頁面右鍵菜單示例代碼

    禁止頁面復(fù)制功能、禁用頁面右鍵菜單等等在瀏覽網(wǎng)頁時想必大家都有遇到過吧,下面為大家詳細介紹下使用js是如何實現(xiàn)的,感興趣的朋友可以參考下
    2013-08-08
  • JavaScript reduce方法使用方法介紹

    JavaScript reduce方法使用方法介紹

    Reduce是個純函數(shù),即只要傳入相同的參數(shù),每次都應(yīng)返回相同的結(jié)果。不要把和處理數(shù)據(jù)無關(guān)的代碼放在Reduce里,讓Reduce保持純凈,只是單純地執(zhí)行計算,這篇文章主要介紹了Redux拆分reduce函數(shù)流程
    2022-10-10
  • 向左滾動文字 js代碼效果

    向左滾動文字 js代碼效果

    本文章來給大家介紹一個javascript中向左滾動文字效果。有需要使用的朋友可參考,這里面我們需要注意下面的css必須強制white-space:nowrap;overflow:hidden;不換行才行哦
    2013-08-08
  • js添加千分位的實現(xiàn)代碼(超簡單)

    js添加千分位的實現(xiàn)代碼(超簡單)

    下面小編就為大家?guī)硪黄猨s添加千分位的實現(xiàn)代碼(超簡單)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-08-08

最新評論