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

javascript實(shí)現(xiàn)移動(dòng)的模態(tài)框效果

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

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

頁(yè)面效果:

點(diǎn)擊鏈接后,彈出登錄模態(tài)框,點(diǎn)擊關(guān)閉鏈接可以關(guān)閉模態(tài)框,鼠標(biāo)在模態(tài)框標(biāo)題區(qū)域按下后可以拖拽模態(tài)框,松開鼠標(biāo)后,模態(tài)框停止移動(dòng)

實(shí)現(xiàn)思路:

1、html、css搭建好頁(yè)面,設(shè)置好模態(tài)框內(nèi)容和樣式后,將模態(tài)框隱藏:display: none;如果點(diǎn)擊彈出模態(tài)框后,頁(yè)面背景色發(fā)生改變,可以添加一個(gè)遮罩層,將遮罩層也先隱藏

2、給點(diǎn)擊后彈出模態(tài)框的元素添加點(diǎn)擊事件- - -onclick

事件處理程序中設(shè)置- - -模態(tài)框 和 遮罩層 顯示- - -eg:

login.style.display = ‘block';
loginBg.style.display = ‘block';

3、給關(guān)閉模態(tài)框元素添加點(diǎn)擊事件- - -onclick

事件處理程序中設(shè)置- - -模態(tài)框 和 遮罩層 隱藏- - -eg:

login.style.display = ‘none';
loginBg.style.display = ‘none';

4、給模態(tài)框標(biāo)題部分添加鼠標(biāo)按下事件- - -mousedown
獲取鼠標(biāo)在模態(tài)框中的位置

5、鼠標(biāo)按下事件中再添加鼠標(biāo)移動(dòng)事件- - -mousemove

document.addEventListener(‘mousemove', move);

獲取鼠標(biāo)在頁(yè)面中的位置
鼠標(biāo)的位置值 - 鼠標(biāo)在模態(tài)框中的位置值 = 模態(tài)框在頁(yè)面中的位置值
將計(jì)算得出的位置值賦值給模態(tài)框的top、left,就可以達(dá)到拖拽鼠標(biāo)的效果,跟隨鼠標(biāo)移動(dòng)

6、當(dāng)鼠標(biāo)松開后,模態(tài)框要停止移動(dòng)。鼠標(biāo)按下事件中再添加鼠標(biāo)松開事件- - -mouseup
鼠標(biāo)松開事件處理程序:頁(yè)面移除鼠標(biāo)移動(dòng)事件- - -document.removeEventListener(‘mousemove’, move);

注意:要添加移除事件,所以給鼠標(biāo)移動(dòng)函數(shù)單獨(dú)拿出來(lái),寫一個(gè)函數(shù)名,添加和移除事件時(shí)引用函數(shù)名就可以了

代碼示例:

<!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>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        a {
            text-decoration: none;
            color: #000;
        }
        
        .login-header {
            margin: 100px auto;
            height: 30px;
            line-height: 30px;
            font-size: 24px;
            text-align: center;
        }
        
        .login {
            display: none;
            width: 515px;
            height: 282px;
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            border: 1px solid #ebebeb;
            background-color: #fff;
            box-shadow: 0px 0px 20px #ddd;
            text-align: center;
            z-index: 99999;
        }
        
        .login-title {
            position: relative;
            height: 50px;
            line-height: 50px;
            font-size: 18px;
            cursor: move;
        }
        
        .close {
            position: absolute;
            top: 0;
            right: 0;
            transform: translate(50%, -50%);
            width: 36px;
            height: 36px;
            line-height: 36px;
            font-size: 12px;
            border-radius: 18px;
            border: 1px solid #ddd;
            color: #666;
            background-color: #fff;
        }
        
        .login-input-content {
            margin: 10px 0;
        }
        
        .login-input label {
            display: inline-block;
            width: 80px;
            text-align: right;
        }
        
        .login-input input {
            width: 300px;
            height: 40px;
            margin: 10px 0;
            padding-left: 10px;
            border: 1px solid #ddd;
            outline-color: royalblue;
        }
        
        .loginBtn a {
            display: block;
            width: 180px;
            height: 35px;
            line-height: 35px;
            margin: 10px auto;
            border: 1px solid #ddd;
        }
        
        .login-bg {
            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="login">
        <div class="login-title">
            登錄會(huì)員
            <span><a  class="close" href="javascript:void(0);" >關(guān)閉</a></span>
        </div>
        <div class="login-input-content">
            <div class="login-input">
                <label for="username">用戶名:</label>
                <input type="text" name="info[username]" id="username" placeholder="請(qǐng)輸入用戶名"><br>
            </div>
            <div class="login-input">
                <label for="password">登錄密碼:</label>
                <input type="password" name="info[password]" id="password" placeholder="請(qǐng)輸入登錄密碼"><br>
            </div>
        </div>
        <div type="submit" value="登錄會(huì)員" class="loginBtn"><a href="javascript:void(0);" >登錄會(huì)員</a></div>
    </div>

    <!-- 遮蓋層 -->
    <div class="login-bg"></div>
    <script>
        var link = document.querySelector('#link');
        var login = document.querySelector('.login');
        var loginBg = document.querySelector('.login-bg');
        var close = document.querySelector('.close');
        var loginTitle = document.querySelector('.login-title');
        link.addEventListener('click', function() {
            login.style.display = 'block';
            loginBg.style.display = 'block';
        })

        close.addEventListener('click', function() {
            login.style.display = 'none';
            loginBg.style.display = 'none';
        })

        loginTitle.addEventListener('mousedown', function(e) {
            // 計(jì)算出鼠標(biāo)按下時(shí),鼠標(biāo)在模態(tài)框中的位置
            var x = e.pageX - login.offsetLeft;
            var y = e.pageY - login.offsetTop;

            // 給移動(dòng)的模態(tài)框賦值位置
            function move(e) {
                login.style.left = e.pageX - x + 'px';
                login.style.top = e.pageY - y + 'px';
            }

            // 添加鼠標(biāo)事件,按下鼠標(biāo)時(shí)模態(tài)框跟著鼠標(biāo)移動(dòng)
            document.addEventListener('mousemove', move);

            // 鼠標(biāo)松開時(shí),模態(tài)框停止移動(dòng)
            document.addEventListener('mouseup', function() {
                document.removeEventListener('mousemove', move);
            })

        })
    </script>
</body>

</html>

頁(yè)面效果:

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

相關(guān)文章

  • H5移動(dòng)端適配 Flexible方案

    H5移動(dòng)端適配 Flexible方案

    這篇文章主要為大家詳細(xì)介紹了H5移動(dòng)端適配,F(xiàn)lexible方案,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • JS動(dòng)畫定時(shí)器知識(shí)總結(jié)

    JS動(dòng)畫定時(shí)器知識(shí)總結(jié)

    這篇文章給大家總結(jié)了關(guān)于JS動(dòng)畫中定時(shí)器的相關(guān)用法以及相關(guān)知識(shí)點(diǎn)總結(jié),有需要的朋友可以參考學(xué)習(xí)下。
    2018-03-03
  • 微信小程序?qū)崿F(xiàn)抖音播放效果的實(shí)例代碼

    微信小程序?qū)崿F(xiàn)抖音播放效果的實(shí)例代碼

    這篇文章主要介紹了微信小程序?qū)崿F(xiàn)抖音播放效果的實(shí)例代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-04-04
  • JavaScript中使用replace結(jié)合正則實(shí)現(xiàn)replaceAll的效果

    JavaScript中使用replace結(jié)合正則實(shí)現(xiàn)replaceAll的效果

    JavaScript?中使用?replace?達(dá)到?replaceAll的效果,其實(shí)就用利用的正則的全局替換。
    2010-06-06
  • jquery div模態(tài)窗口的簡(jiǎn)單實(shí)例

    jquery div模態(tài)窗口的簡(jiǎn)單實(shí)例

    下面小編就為大家?guī)?lái)一篇jquery div模態(tài)窗口的簡(jiǎn)單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-05-05
  • 用js正確判斷用戶名cookie是否存在的方法

    用js正確判斷用戶名cookie是否存在的方法

    用cookie保存用戶名,記錄登錄狀態(tài),如何正確判斷該機(jī)用戶cookie是否存在呢?下面有個(gè)不錯(cuò)的方法,大家可以參考下
    2014-01-01
  • 最新評(píng)論