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

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

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

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

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

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

②點擊關閉按鈕,模態(tài)框和遮蓋層就會隱藏起來

③頁面拖拽

功能分析:

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

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

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

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

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

5.鼠標松開之后需要停止拖拽,就是移除鼠標移動事件。

完整代碼:

<!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>
? ? <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) {
? ? ? ? ? ? ? ? // 鼠標在盒子內(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';
?
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? // 鼠標談起就移除鼠標移動事件
? ? ? ? ? ? ? ? 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:;" >點擊,彈出登錄框</a></div>
? ? <div class="loginbox">
? ? ? ? <p class="move">登錄會員</p>
? ? ? ? <div class="content">
? ? ? ? ? ? <label for="">用戶名:</label>
? ? ? ? ? ? <input type="text" placeholder="請輸入用戶名">
? ? ? ? </div>
? ? ? ? <div class="content">
? ? ? ? ? ? <label for="">登錄密碼:</label>
? ? ? ? ? ? <input type="password" placeholder="請輸入用戶名">
? ? ? ? </div>
? ? ? ? <input type="button" value="登錄會員" class="btn">
? ? ? ? <span class="close">關閉</span>
? ? </div>
? ? <div class="gray">
?
? ? </div>
</body>
?
</html>

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

相關文章

最新評論

鄂伦春自治旗| 通化县| 当阳市| 凤台县| 肃南| 灌云县| 青海省| 嫩江县| 濉溪县| 瓦房店市| 杭锦后旗| 商都县| 汽车| 陇西县| 垣曲县| 旬邑县| 苗栗县| 河池市| 册亨县| 洛阳市| 宁化县| 平阳县| 天津市| 长寿区| 四子王旗| 延川县| 嘉黎县| 项城市| 林甸县| 兴安盟| 临武县| 仙游县| 习水县| 通辽市| 金堂县| 大同县| 江油市| 准格尔旗| 社旗县| 甘南县| 山东省|