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

JS實(shí)現(xiàn)簡(jiǎn)單拖動(dòng)模態(tài)框案例

 更新時(shí)間:2022年07月04日 10:18:38   作者:setTimeout()  
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)簡(jiǎn)單拖動(dòng)模態(tài)框案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

需要實(shí)現(xiàn)的效果:

①點(diǎn)擊“點(diǎn)擊,彈出登錄框”后模態(tài)框和遮擋層就會(huì)顯示出來

②點(diǎn)擊關(guān)閉按鈕,模態(tài)框和遮蓋層就會(huì)隱藏起來

③頁面拖拽

功能分析:

首先給上面的"點(diǎn)擊,彈出登錄框"設(shè)置點(diǎn)擊事件,點(diǎn)擊之后就顯示遮罩層和模態(tài)框,然后給模態(tài)框上面的關(guān)閉按鈕設(shè)置點(diǎn)擊事件,點(diǎn)擊之后就隱藏遮罩層和模態(tài)框。然后是拖拽過程,這個(gè)過程的實(shí)現(xiàn)較為復(fù)雜,主要分為下面幾步:

1.明確模態(tài)框的真正位置是鼠標(biāo)的坐標(biāo)減去鼠標(biāo)在模態(tài)框內(nèi)的坐標(biāo)。

2.鼠標(biāo)的坐標(biāo)通過鼠標(biāo)按下事件獲取,通過e.pageY和e.pageX來獲取。

3.按下之后想要獲得鼠標(biāo)在模態(tài)框中的坐標(biāo)(一直都不會(huì)變),需要獲得盒子的坐標(biāo),盒子坐標(biāo)通過element.offsetTop和element.offsetLeft來獲取,通過鼠標(biāo)的坐標(biāo)減去模態(tài)框的坐標(biāo)獲得鼠標(biāo)在模態(tài)框中的坐標(biāo)。

4.按下之后鼠標(biāo)移動(dòng),就讓模態(tài)框的坐標(biāo)設(shè)置稱為鼠標(biāo)的坐標(biāo)減去鼠標(biāo)在模態(tài)框中的坐標(biāo)。

5.鼠標(biāo)松開之后需要停止拖拽,就是移除鼠標(biāo)移動(dòng)事件。

完整代碼:

<!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>拖動(dòng)模態(tài)框</title>
? ? <script>
? ? ? ? window.onload = function() {
? ? ? ? ? ? var loginbox = document.querySelector('.loginbox');
? ? ? ? ? ? var gray = document.querySelector('.gray');
? ? ? ? ? ? var loginheader = document.querySelector('.login-header');
? ? ? ? ? ? var close = document.querySelector('.close');
? ? ? ? ? ? var move = document.querySelector('.move');
? ? ? ? ? ? loginheader.addEventListener('click', function() {
? ? ? ? ? ? ? ? loginbox.style.display = 'block';
? ? ? ? ? ? ? ? gray.style.display = 'block';
? ? ? ? ? ? });
? ? ? ? ? ? close.addEventListener('click', function() {
? ? ? ? ? ? ? ? loginbox.style.display = 'none';
? ? ? ? ? ? ? ? gray.style.display = 'none';
? ? ? ? ? ? });
? ? ? ? ? ? move.addEventListener('mousedown', function(e) {
? ? ? ? ? ? ? ? // 鼠標(biāo)在盒子內(nèi)得距離
? ? ? ? ? ? ? ? var x = e.pageX - loginbox.offsetLeft;
? ? ? ? ? ? ? ? var y = e.pageY - loginbox.offsetTop;
? ? ? ? ? ? ? ? // alert('hahah')
? ? ? ? ? ? ? ? document.addEventListener('mousemove', move)
?
? ? ? ? ? ? ? ? function move(e) {
? ? ? ? ? ? ? ? ? ? loginbox.style.left = e.pageX - x + 'px';
? ? ? ? ? ? ? ? ? ? loginbox.style.top = e.pageY - y + 'px';
?
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? // 鼠標(biāo)談起就移除鼠標(biāo)移動(dòng)事件
? ? ? ? ? ? ? ? document.addEventListener('mouseup', function() {
? ? ? ? ? ? ? ? ? ? document.removeEventListener('mousemove', move)
? ? ? ? ? ? ? ? })
?
? ? ? ? ? ? })
? ? ? ? }
? ? </script>
? ? <style>
? ? ? ? .login-header {
? ? ? ? ? ? width: 100%;
? ? ? ? ? ? text-align: center;
? ? ? ? ? ? height: 30px;
? ? ? ? ? ? font-size: 24px;
? ? ? ? ? ? line-height: 30px;
? ? ? ? }
? ? ? ??
? ? ? ? .move {
? ? ? ? ? ? cursor: move;
? ? ? ? }
? ? ? ??
? ? ? ? ul,
? ? ? ? li,
? ? ? ? ol,
? ? ? ? dl,
? ? ? ? dt,
? ? ? ? dd,
? ? ? ? div,
? ? ? ? p,
? ? ? ? span,
? ? ? ? h1,
? ? ? ? h2,
? ? ? ? h3,
? ? ? ? h4,
? ? ? ? h5,
? ? ? ? h6,
? ? ? ? a {
? ? ? ? ? ? padding: 0px;
? ? ? ? ? ? margin: 0px;
? ? ? ? }
? ? ? ??
? ? ? ? a {
? ? ? ? ? ? text-decoration: none;
? ? ? ? ? ? color: #000000;
? ? ? ? }
? ? ? ??
? ? ? ? .loginbox {
? ? ? ? ? ? display: none;
? ? ? ? ? ? position: absolute;
? ? ? ? ? ? top: 200px;
? ? ? ? ? ? left: 50%;
? ? ? ? ? ? transform: translateX(-50%);
? ? ? ? ? ? z-index: 2;
? ? ? ? ? ? width: 520px;
? ? ? ? ? ? height: 300px;
? ? ? ? ? ? background-color: #fff;
? ? ? ? }
? ? ? ??
? ? ? ? .loginbox .close {
? ? ? ? ? ? cursor: pointer;
? ? ? ? }
? ? ? ??
? ? ? ? .loginbox p {
? ? ? ? ? ? float: left;
? ? ? ? ? ? font-size: 22px;
? ? ? ? ? ? text-align: center;
? ? ? ? ? ? margin-top: 30px;
? ? ? ? ? ? margin-left: 240px;
? ? ? ? }
? ? ? ??
? ? ? ? .loginbox .content {
? ? ? ? ? ? float: right;
? ? ? ? ? ? margin-top: 30px;
? ? ? ? ? ? margin-right: 70px;
? ? ? ? }
? ? ? ??
? ? ? ? .loginbox .content input {
? ? ? ? ? ? width: 300px;
? ? ? ? ? ? height: 25px;
? ? ? ? ? ? outline: none;
? ? ? ? ? ? border: 1px solid #ccc;
? ? ? ? }
? ? ? ??
? ? ? ? .loginbox .btn {
? ? ? ? ? ? background-color: #fff;
? ? ? ? ? ? width: 180px;
? ? ? ? ? ? height: 30px;
? ? ? ? ? ? border: 1px solid #ccc;
? ? ? ? ? ? margin-top: 40px;
? ? ? ? ? ? margin-left: 170px;
? ? ? ? }
? ? ? ??
? ? ? ? .loginbox span {
? ? ? ? ? ? position: absolute;
? ? ? ? ? ? font-size: 12px;
? ? ? ? ? ? text-align: center;
? ? ? ? ? ? line-height: 50px;
? ? ? ? ? ? z-index: 9999;
? ? ? ? ? ? top: -25px;
? ? ? ? ? ? right: -25px;
? ? ? ? ? ? width: 50px;
? ? ? ? ? ? height: 50px;
? ? ? ? ? ? background-color: #fff;
? ? ? ? ? ? color: #000000;
? ? ? ? ? ? border-radius: 50%;
? ? ? ? ? ? box-shadow: 1px 1px 5px rgba(0, 0, 0, .3);
? ? ? ? }
? ? ? ??
? ? ? ? .gray {
? ? ? ? ? ? display: none;
? ? ? ? ? ? position: fixed;
? ? ? ? ? ? top: 0;
? ? ? ? ? ? left: 0;
? ? ? ? ? ? width: 100%;
? ? ? ? ? ? height: 100%;
? ? ? ? ? ? background-color: rgba(0, 0, 0, .3);
? ? ? ? }
? ? </style>
</head>
?
<body>
? ? <div class="login-header"><a id="link" href="javascript:;" >點(diǎn)擊,彈出登錄框</a></div>
? ? <div class="loginbox">
? ? ? ? <p class="move">登錄會(huì)員</p>
? ? ? ? <div class="content">
? ? ? ? ? ? <label for="">用戶名:</label>
? ? ? ? ? ? <input type="text" placeholder="請(qǐng)輸入用戶名">
? ? ? ? </div>
? ? ? ? <div class="content">
? ? ? ? ? ? <label for="">登錄密碼:</label>
? ? ? ? ? ? <input type="password" placeholder="請(qǐng)輸入用戶名">
? ? ? ? </div>
? ? ? ? <input type="button" value="登錄會(huì)員" class="btn">
? ? ? ? <span class="close">關(guān)閉</span>
? ? </div>
? ? <div class="gray">
?
? ? </div>
</body>
?
</html>

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

相關(guān)文章

最新評(píng)論

咸丰县| 晋宁县| 盐亭县| 泉州市| 勐海县| 阳高县| 九台市| 大竹县| 新巴尔虎右旗| 桃江县| 日照市| 正定县| 论坛| 宕昌县| 怀化市| 浑源县| 沙坪坝区| 遂平县| 南开区| 诸暨市| 新竹市| 南阳市| 哈巴河县| 姜堰市| 霍山县| 迭部县| 洱源县| 曲靖市| 大足县| 平江县| 江北区| 林口县| 壶关县| 克什克腾旗| 东乌珠穆沁旗| 名山县| 铁力市| 定日县| 星子县| 盘山县| 扶绥县|