最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

js實(shí)現(xiàn)模態(tài)框的拖拽效果

 更新時(shí)間:2022年07月03日 13:19:42   作者:貓小姐和她的影子  
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)模態(tài)框的拖拽效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了js實(shí)現(xiàn)模態(tài)框拖拽效果的具體代碼,供大家參考,具體內(nèi)容如下

之前學(xué)習(xí)js遇到了這樣的需求:
鼠標(biāo)按下后,移動(dòng)鼠標(biāo),模態(tài)框隨鼠標(biāo)移動(dòng),鼠標(biāo)松開(kāi),模態(tài)框也不會(huì)隨鼠標(biāo)移動(dòng)。<完整的代碼在最后哦>

分析思路:

1.點(diǎn)擊彈出層,模態(tài)框和遮擋層就會(huì)顯示出來(lái)。display:block
2.點(diǎn)擊關(guān)閉按鈕,模態(tài)框和遮擋層就會(huì)隱藏。display:none

3.在頁(yè)面中拖拽的步驟:鼠標(biāo)按下并移動(dòng),之后松開(kāi)鼠標(biāo)
4.觸發(fā)事件是鼠標(biāo)按下mousedown,鼠標(biāo)移動(dòng)是mousemove,鼠標(biāo)松開(kāi):mouseup
5.拖拽過(guò)程:鼠標(biāo)移動(dòng)過(guò)程中,獲得最新的值賦值給模態(tài)框的left和top值,這樣模態(tài)框就可以跟著鼠標(biāo)走了
6.鼠標(biāo)按下觸發(fā)的時(shí)間源是最上面一行,也就是說(shuō),鼠標(biāo)只有放在最上面一行,才能觸發(fā)該事件。放在其他區(qū)域不會(huì)觸發(fā)該事件。
7.鼠標(biāo)的坐標(biāo)減去鼠標(biāo)在盒子內(nèi)的坐標(biāo),才是模態(tài)框真正的位置。(因?yàn)槟B(tài)框是可移動(dòng)的,只有第一次才能拿到模態(tài)框的left和top,其他時(shí)候并不能直接拿到。所以采用‘鼠標(biāo)的坐標(biāo) - 鼠標(biāo)在模態(tài)框內(nèi)的坐標(biāo)’來(lái)計(jì)算模態(tài)框的位置)

8.鼠標(biāo)按下,我們要得到鼠標(biāo)在盒子內(nèi)的坐標(biāo)
9.鼠標(biāo)移動(dòng),就讓模態(tài)框的坐標(biāo)設(shè)置為:鼠標(biāo)坐標(biāo) - 盒子坐標(biāo)即可。注意移送事件要寫(xiě)到按下事件里面
10.鼠標(biāo)松開(kāi),就停止拖拽,可以讓鼠標(biāo)移動(dòng)事件解除

代碼實(shí)現(xiàn):

<!DOCTYPE html>
<html lang="en">

<head>
? ? <meta charset="UTF-8">
? ? <meta http-equiv="X-UA-Compatible" content="IE=edge">
? ? <meta name="viewport" content="width=device-width, initial-scale=1.0">
? ? <title>模態(tài)框拖拽</title>
? ? <style>
? ? ? ? * {
? ? ? ? ? ? margin: 0;
? ? ? ? ? ? padding: 0;
? ? ? ? }

? ? ? ? #link {
? ? ? ? ? ? color: #000;
? ? ? ? ? ? text-decoration: none;
? ? ? ? ? ? border: 1px solid #000;
? ? ? ? }

? ? ? ? .login {
? ? ? ? ? ? width: 300px;
? ? ? ? ? ? height: 200px;
? ? ? ? ? ? background-color: antiquewhite;
? ? ? ? ? ? position: absolute;
? ? ? ? ? ? top: 50%;
? ? ? ? ? ? left: 50%;
? ? ? ? ? ? transform: translate(-50%, -50%);
? ? ? ? ? ? z-index: 2;
? ? ? ? ? ? display: none;
? ? ? ? }

? ? ? ? .login-title {
? ? ? ? ? ? text-align: center;
? ? ? ? ? ? width: 100%;
? ? ? ? ? ? height: 40px;
? ? ? ? ? ? line-height: 40px;
? ? ? ? ? ? background-color: aqua;
? ? ? ? ? ? cursor: move;

? ? ? ? }

? ? ? ? .login-title span {
? ? ? ? ? ? display: block;
? ? ? ? ? ? height: 30px;
? ? ? ? ? ? width: 30px;
? ? ? ? ? ? background-color: antiquewhite;
? ? ? ? ? ? line-height: 30px;
? ? ? ? ? ? border-radius: 50%;
? ? ? ? ? ? position: absolute;
? ? ? ? ? ? top: -10px;
? ? ? ? ? ? right: -10px;
? ? ? ? ? ? font-size: 12px;

? ? ? ? }

? ? ? ? .login-title span a {
? ? ? ? ? ? text-decoration: none;
? ? ? ? ? ? color: #000;
? ? ? ? }

? ? ? ? .login-input-content {
? ? ? ? ? ? margin: 15px 20px 0;
? ? ? ? ? ? line-height: 30px;
? ? ? ? }

? ? ? ? .login-button {
? ? ? ? ? ? width: 200px;
? ? ? ? ? ? height: 20px;
? ? ? ? ? ? margin: 10px auto;
? ? ? ? ? ? border: 1px solid rgb(77, 73, 73);
? ? ? ? ? ? text-align: center;

? ? ? ? }

? ? ? ? .login-button a {
? ? ? ? ? ? text-decoration: none;
? ? ? ? ? ? color: #000;
? ? ? ? ? ? font-size: 14px;
? ? ? ? }

? ? ? ? #bg {
? ? ? ? ? ? display: none;
? ? ? ? ? ? background-color: #000;
? ? ? ? ? ? width: 100%;
? ? ? ? ? ? height: 100%;
? ? ? ? ? ? opacity: 0.3;
? ? ? ? ? ? z-index: -1;
? ? ? ? ? ? position: absolute;
? ? ? ? ? ? top: 0;
? ? ? ? ? ? left: 0;
? ? ? ? }
? ? </style>
</head>

<body>
? ? <div class="login-header"><a href="javascript:;" id="link">點(diǎn)擊,彈出登錄框</a></div>
? ? <div class="login" id="login">
? ? ? ? <div class="login-title">登錄會(huì)員
? ? ? ? ? ? <span><a id="colseBth" href="javascript:void(0);" class="close-login">關(guān)閉</a></span>
? ? ? ? </div>
? ? ? ? <div class="login-input-content">
? ? ? ? ? ? <div class="login-input">
? ? ? ? ? ? ? ? <label for="">用  戶 名:</label>
? ? ? ? ? ? ? ? <input type="text" placeholder="請(qǐng)輸入用戶名" name="info[sername]" id="username" class="username">
? ? ? ? ? ? </div>
? ? ? ? ? ? <div class="login-inpit">
? ? ? ? ? ? ? ? <label for="">登錄密碼:</label>
? ? ? ? ? ? ? ? <input type="password" placeholder="請(qǐng)輸入登錄密碼" name="info[password]" id="password">

? ? ? ? ? ? </div>
? ? ? ? </div>
? ? ? ? <div class="login-button" id="loginBtn"><a href="javascript:void(0);" id="login-button-submit">會(huì)員登錄</a></div>
? ? </div>
? ? <!-- 遮罩層 -->
? ? <div class="login-bg" id="bg"></div>

</body>
<script>
? ? // 1.點(diǎn)擊,彈出模態(tài)框和遮罩層
? ? // 3.點(diǎn)擊關(guān)閉模態(tài)框和遮罩層自動(dòng)隱藏
? ? // 4.頁(yè)面拖拽原理:鼠標(biāo)按下且移動(dòng),之后松開(kāi)鼠標(biāo)
? ? // 5.拖拽過(guò)程:鼠標(biāo)移動(dòng)的時(shí)候獲得新的值賦值給模態(tài)框的left和top值。
?? ?
?? ?//1.獲取DOM元素
? ? var oLink = document.querySelector('#link');
? ? var oLogin = document.querySelector('.login');
? ? var oBg = document.querySelector('#bg');
? ? var oClose = oLogin.querySelector('#colseBth');
? ? var title = oLogin.querySelector('.login-title');

?? ?//2.點(diǎn)擊彈出層這個(gè)鏈接link,讓mask和login顯示出來(lái)
? ? oLink.addEventListener('click', function () {
? ? ? ? oLogin.style.display = 'block';
? ? ? ? oBg.style.display = 'block';
? ? })
?? ?//3.點(diǎn)擊closeBtn就隱藏mask和login
? ? oClose.addEventListener('click', function () {
? ? ? ? oLogin.style.display = 'none';
? ? ? ? oBg.style.display = 'none';
? ? })
?? ?//4.開(kāi)始拖拽
?? ?//(1)當(dāng)我們鼠標(biāo)按下,就獲得鼠標(biāo)在盒子內(nèi)的坐標(biāo)
? ? title.addEventListener('mousedown', function (e) {
? ? ? ? var x = e.pageX - oLogin.offsetLeft;
? ? ? ? var y = e.pageY - oLogin.offsetTop;
? ? ? ? console.log(x, y)
?? ??? ?
?? ??? ?//(2)鼠標(biāo)移動(dòng)的時(shí)候,把鼠標(biāo)在頁(yè)面中的坐標(biāo) 減去 鼠標(biāo)在盒子內(nèi)的坐標(biāo)就是模態(tài)框的left和top值
? ? ? ? document.addEventListener('mousemove', move)?
? ? ? ? ? ? function move(e) {
? ? ? ? ? ? ? ? oLogin.style.left = e.pageX - x + 'px';
? ? ? ? ? ? ? ? oLogin.style.top = e.pageY - y + 'px';
? ? ? ? ? ? }
?? ??? ??? ?
?? ??? ??? ?//(3)鼠標(biāo)彈起,就讓鼠標(biāo)移動(dòng)事件移除
? ? ? ? ? ? document.addEventListener('mouseup', function () {
? ? ? ? ? ? ? ? document.removeEventListener('mousemove', move);
? ? ? ? ? ? ? ??
? ? ? ? ? ? })
? ? ? ? })
? ??
</script>

</html>

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

相關(guān)文章

最新評(píng)論

文成县| 长汀县| 伊春市| 宜兴市| 泰州市| 华池县| 远安县| 特克斯县| 沈丘县| 台州市| 黔东| 皋兰县| 远安县| 肥乡县| 榆中县| 诸暨市| 稷山县| 长武县| 海淀区| 财经| 南澳县| 昌宁县| 叶城县| 盖州市| 赣州市| 清远市| 来宾市| 龙州县| 邹城市| 平江县| 双桥区| 积石山| 西安市| 城步| 乃东县| 拉萨市| 揭西县| 松滋市| 北票市| 阳江市| 周口市|