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

JS實現(xiàn)簡單可拖動的模態(tài)框

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

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

這篇博文有 簡單實現(xiàn) 和 帶樣式且稍微復雜一點 的兩個版本

簡單版本

效果圖:

實現(xiàn)思路:

給可拖動部分添加點擊事件,觸發(fā)時計算鼠標在可拖動部分中的坐標( e.pageX - box.offsetLeft ),得到 x y 。

給 document 添加鼠標移動事件,因為當鼠標拖動模態(tài)框的時候,是在整個DOM窗口內(nèi)移動的。保持鼠標與模態(tài)框的相對位置不變,所以需要計算此時的模態(tài)框的位置(e.pageX - x ),進而修改模態(tài)框位置。此時的 e.pageX 已經(jīng)發(fā)生變化,跟上述的 e.pageX 是不相等的。

當鼠標彈起時,清除移動事件即可。

代碼注意點:

1 offsetWidth 這類offset屬性是只讀屬性,不能通過對其進行賦值來修改元素。

2 使用 style.top、style.left 的元素必須有定位,且在給這兩個屬性賦值的時候必須要在末尾加上單位 'px' 。

3 代碼中 mousemove 事件回調(diào)函數(shù)的參數(shù) e 不能省略,如果省略則會使用外部的 e,即 mousedown 事件回調(diào)函數(shù)中的 e,因此得到的鼠標坐標是錯誤的。

代碼:

<!DOCTYPE html>
<html lang="en">
?
<head>
? ? <meta charset="UTF-8">
? ? <meta name="viewport" content="width=device-width, initial-scale=1.0">
? ? <title>Document</title>
? ? <!-- 使用style修改樣式 -->
? ? <style>
? ? ? ? .box {
? ? ? ? ? ? position: absolute;
? ? ? ? ? ? height: 200px;
? ? ? ? ? ? width: 200px;
? ? ? ? ? ? background-color: plum;
? ? ? ? }
?
? ? ? ? .title {
? ? ? ? ? ? width: 100%;
? ? ? ? ? ? height: 50px;
? ? ? ? ? ? background-color: rosybrown;
? ? ? ? ? ? cursor: move;
? ? ? ? ? ? /* 讓文字不可選中 */
? ? ? ? ? ? -webkit-user-select: none;
? ? ? ? ? ? -moz-user-select: none;
? ? ? ? ? ? -ms-user-select: none;
? ? ? ? ? ? user-select: none;
? ? ? ? }
? ? </style>
</head>
?
<body>
? ? <div class="box">
? ? ? ? <div class="title">head部分</div>
? ? </div>
? ? <script>
? ? ? ? var box = document.querySelector('.box');
? ? ? ? var title = document.querySelector('.title');
? ? ? ? title.addEventListener('mousedown', function (e) {
? ? ? ? ? ? //獲取鼠標在box內(nèi)的坐標值
? ? ? ? ? ? var x = e.pageX - box.offsetLeft;
? ? ? ? ? ? var y = e.pageY - box.offsetTop;
? ? ? ? ? ? document.addEventListener('mousemove', move);
?
? ? ? ? ? ? function move(e) {
? ? ? ? ? ? ? ? box.style.left = e.pageX - x + 'px';
? ? ? ? ? ? ? ? box.style.top = e.pageY - y + 'px';
? ? ? ? ? ? }
? ? ? ? ? ? document.addEventListener('mouseup', function () {
? ? ? ? ? ? ? ? document.removeEventListener('mousemove', move);
? ? ? ? ? ? })
? ? ? ? })
? ? </script>
</body>
?
</html>

復雜版本

效果圖:

實現(xiàn)思路:

跟簡單版本大體相同

實現(xiàn)代碼:

<!DOCTYPE html>
<html>
?
<head lang="en">
? ? <meta charset="UTF-8">
? ? <title>拖動模態(tài)框</title>
? ? <style>
? ? ? ? .login-header {
? ? ? ? ? ? width: 100%;
? ? ? ? ? ? text-align: center;
? ? ? ? ? ? height: 30px;
? ? ? ? ? ? font-size: 24px;
? ? ? ? ? ? line-height: 30px;
? ? ? ? }
?
? ? ? ? ul,
? ? ? ? li,
? ? ? ? ol,
? ? ? ? dl,
? ? ? ? dt,
? ? ? ? dd,
? ? ? ? div,
? ? ? ? p,
? ? ? ? span,
? ? ? ? h1,
? ? ? ? h2,
? ? ? ? h3,
? ? ? ? h4,
? ? ? ? h5,
? ? ? ? h6,
? ? ? ? a {
? ? ? ? ? ? padding: 0px;
? ? ? ? ? ? margin: 0px;
? ? ? ? }
?
? ? ? ? .login {
? ? ? ? ? ? display: none;
? ? ? ? ? ? width: 512px;
? ? ? ? ? ? height: 280px;
? ? ? ? ? ? position: fixed;
? ? ? ? ? ? border: #ebebeb solid 1px;
? ? ? ? ? ? left: 50%;
? ? ? ? ? ? top: 50%;
? ? ? ? ? ? background: #ffffff;
? ? ? ? ? ? box-shadow: 0px 0px 20px #ddd;
? ? ? ? ? ? z-index: 9999;
? ? ? ? ? ? transform: translate(-50%, -50%);
? ? ? ? }
?
? ? ? ? .login-title {
? ? ? ? ? ? width: 100%;
? ? ? ? ? ? margin: 10px 0px 0px 0px;
? ? ? ? ? ? text-align: center;
? ? ? ? ? ? line-height: 40px;
? ? ? ? ? ? height: 40px;
? ? ? ? ? ? font-size: 18px;
? ? ? ? ? ? position: relative;
? ? ? ? ? ? cursor: move;
? ? ? ? }
?
? ? ? ? .login-input-content {
? ? ? ? ? ? margin-top: 20px;
? ? ? ? }
?
? ? ? ? .login-button {
? ? ? ? ? ? width: 50%;
? ? ? ? ? ? margin: 30px auto 0px auto;
? ? ? ? ? ? line-height: 40px;
? ? ? ? ? ? font-size: 14px;
? ? ? ? ? ? border: #ebebeb 1px solid;
? ? ? ? ? ? text-align: center;
? ? ? ? }
?
? ? ? ? .login-bg {
? ? ? ? ? ? display: none;
? ? ? ? ? ? width: 100%;
? ? ? ? ? ? height: 100%;
? ? ? ? ? ? position: fixed;
? ? ? ? ? ? top: 0px;
? ? ? ? ? ? left: 0px;
? ? ? ? ? ? background: rgba(0, 0, 0, .3);
? ? ? ? }
?
? ? ? ? a {
? ? ? ? ? ? text-decoration: none;
? ? ? ? ? ? color: #000000;
? ? ? ? }
?
? ? ? ? .login-button a {
? ? ? ? ? ? display: block;
? ? ? ? }
?
? ? ? ? .login-input input.list-input {
? ? ? ? ? ? float: left;
? ? ? ? ? ? line-height: 35px;
? ? ? ? ? ? height: 35px;
? ? ? ? ? ? width: 350px;
? ? ? ? ? ? border: #ebebeb 1px solid;
? ? ? ? ? ? text-indent: 5px;
? ? ? ? }
?
? ? ? ? .login-input {
? ? ? ? ? ? overflow: hidden;
? ? ? ? ? ? margin: 0px 0px 20px 0px;
? ? ? ? }
?
? ? ? ? .login-input label {
? ? ? ? ? ? float: left;
? ? ? ? ? ? width: 90px;
? ? ? ? ? ? padding-right: 10px;
? ? ? ? ? ? text-align: right;
? ? ? ? ? ? line-height: 35px;
? ? ? ? ? ? height: 35px;
? ? ? ? ? ? font-size: 14px;
? ? ? ? }
?
? ? ? ? .login-title span {
? ? ? ? ? ? position: absolute;
? ? ? ? ? ? font-size: 12px;
? ? ? ? ? ? right: -20px;
? ? ? ? ? ? top: -30px;
? ? ? ? ? ? background: #ffffff;
? ? ? ? ? ? border: #ebebeb solid 1px;
? ? ? ? ? ? width: 40px;
? ? ? ? ? ? height: 40px;
? ? ? ? ? ? border-radius: 20px;
? ? ? ? }
? ? </style>
</head>
?
<body>
? ? <div class="login-header"><a id="link" href="javascript:;">點擊,彈出登錄框</a></div>
? ? <div id="login" class="login">
? ? ? ? <div id="title" class="login-title">登錄會員
? ? ? ? ? ? <span><a id="closeBtn" href="javascript:void(0);" class="close-login">關(guān)閉</a></span>
? ? ? ? </div>
? ? ? ? <div class="login-input-content">
? ? ? ? ? ? <div class="login-input">
? ? ? ? ? ? ? ? <label>用戶名:</label>
? ? ? ? ? ? ? ? <input type="text" placeholder="請輸入用戶名" name="info[username]" id="username" class="list-input">
? ? ? ? ? ? </div>
? ? ? ? ? ? <div class="login-input">
? ? ? ? ? ? ? ? <label>登錄密碼:</label>
? ? ? ? ? ? ? ? <input type="password" placeholder="請輸入登錄密碼" name="info[password]" id="password" class="list-input">
? ? ? ? ? ? </div>
? ? ? ? </div>
? ? ? ? <div id="loginBtn" class="login-button"><a href="javascript:void(0);" id="login-button-submit">登錄會員</a></div>
? ? </div>
? ? <!-- 遮蓋層 -->
? ? <div id="bg" class="login-bg"></div>
? ? <script>
? ? ? ? // 1. 獲取元素
? ? ? ? var login = document.querySelector('.login');
? ? ? ? var mask = document.querySelector('.login-bg');
? ? ? ? var link = document.querySelector('#link');
? ? ? ? var closeBtn = document.querySelector('#closeBtn');
? ? ? ? var title = document.querySelector('#title');
? ? ? ? // 2. 點擊彈出層這個鏈接 link ?讓mask 和login 顯示出來
? ? ? ? link.addEventListener('click', function () {
? ? ? ? ? ? mask.style.display = 'block';
? ? ? ? ? ? login.style.display = 'block';
? ? ? ? })
? ? ? ? // 3. 點擊 closeBtn 就隱藏 mask 和 login?
? ? ? ? closeBtn.addEventListener('click', function () {
? ? ? ? ? ? mask.style.display = 'none';
? ? ? ? ? ? login.style.display = 'none';
? ? ? ? })
? ? ? ? // 4. 開始拖拽
? ? ? ? // (1) 當我們鼠標按下, 就獲得鼠標在盒子內(nèi)的坐標
? ? ? ? title.addEventListener('mousedown', function (e) {
? ? ? ? ? ? var x = e.pageX - login.offsetLeft;
? ? ? ? ? ? var y = e.pageY - login.offsetTop;
? ? ? ? ? ? // (2) 鼠標移動的時候,把鼠標在頁面中的坐標,減去 鼠標在盒子內(nèi)的坐標就是模態(tài)框的left和top值
? ? ? ? ? ? document.addEventListener('mousemove', move)
?
? ? ? ? ? ? function move(e) {
? ? ? ? ? ? ? ? login.style.left = e.pageX - x + 'px';
? ? ? ? ? ? ? ? login.style.top = e.pageY - y + 'px';
? ? ? ? ? ? }
? ? ? ? ? ? // (3) 鼠標彈起,就讓鼠標移動事件移除
? ? ? ? ? ? document.addEventListener('mouseup', function () {
? ? ? ? ? ? ? ? document.removeEventListener('mousemove', move);
? ? ? ? ? ? })
? ? ? ? })
? ? </script>
</body>
?
</html>

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

相關(guān)文章

最新評論