JavaScript+CSS實(shí)現(xiàn)模態(tài)框效果
本文實(shí)例為大家分享了JavaScript+CSS實(shí)現(xiàn)模態(tài)框效果的具體代碼,供大家參考,具體內(nèi)容如下
發(fā)現(xiàn)的問題
1)鼠標(biāo)按下后拖動(dòng)的瞬間,光標(biāo)會(huì)脫離模態(tài)盒子跑到外面
2)鼠標(biāo)彈起事件不起作用
解決的思路
首先是因?yàn)榇a里有用到offsetLeft和offsetTop這兩個(gè)屬性,所以考慮是不是模態(tài)框的定位出現(xiàn)了問題 。
又:設(shè)置關(guān)閉標(biāo)簽設(shè)置了絕對(duì)定位,那么loginBox作為其父級(jí)元素我將其設(shè)置為相對(duì)定位。
各個(gè)類型定位的介紹:
1.靜態(tài)定位: position: static 默認(rèn),也就是文檔流定位,元素的顯示位置與源碼順序一致(不是定位元素)
2.相對(duì)定位: position: relative;相對(duì)于該元素在文檔流中的原始位置進(jìn)行偏移
3.絕對(duì)定位: position: absolue;相對(duì)于它的祖先中離它最近的”定位元素”的位置發(fā)生偏移(如果祖先元素中不存在定位元素,它就參考根元素(html)進(jìn)行定位)
4.固定定位: position: fixed; 是絕對(duì)定位的一個(gè)特例,它始終相對(duì)于html定位
定位元素:只要這個(gè)元素中有position: relative;或者 position:absolute;就稱為定位元素
由上可看出,相對(duì)定位,指的是相對(duì)于該元素在文檔流中的原始位置進(jìn)行偏移而不是相對(duì)于html,所以loginBox不能設(shè)置為相對(duì)定位??梢杂媒^對(duì)定位或固定定位,兩者均可。
另外,需要注意使用top、left定位后,則margin-top、margin-left產(chǎn)生同方向相加的位移效果,margin-bottom、margin-righ產(chǎn)生無任何效果的margin空間。所以保留margin: 100px auto;時(shí)效果還是會(huì)有些奇怪,注釋起來了就好了。
代碼
<!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> ? ? ? ? * { ? ? ? ? ? ? padding: 0; ? ? ? ? ? ? margin: 0; ? ? ? ? } ? ? ? ?? ? ? ? ? body { ? ? ? ? ? ? background-color: white; ? ? ? ? } ? ? ? ?? ? ? ? ? .dianji { ? ? ? ? ? ? height: 50px; ? ? ? ? ? ? width: 300px; ? ? ? ? ? ? font-size: 20px; ? ? ? ? ? ? margin: 10px auto; ? ? ? ? ? ? cursor: pointer; ? ? ? ? } ? ? ? ?? ? ? ? ? .loginBox { ? ? ? ? ? ? position: absolute; ? ? ? ? ? ? top: 50%; ? ? ? ? ? ? left: 50%; ? ? ? ? ? ? transform: translate(-50%, -50%); ? ? ? ? ? ? /* margin: 100px auto; */ ? ? ? ? ? ? box-sizing: border-box; ? ? ? ? ? ? height: 200px; ? ? ? ? ? ? width: 370px; ? ? ? ? ? ? background-color: white; ? ? ? ? ? ? text-align: center; ? ? ? ? ? ? display: none; ? ? ? ? } ? ? ? ?? ? ? ? ? .loginBox>span { ? ? ? ? ? ? position: absolute; ? ? ? ? ? ? top: -15px; ? ? ? ? ? ? right: -15px; ? ? ? ? ? ? height: 30px; ? ? ? ? ? ? width: 30px; ? ? ? ? ? ? font-size: 8px; ? ? ? ? ? ? line-height: 30px; ? ? ? ? ? ? border-radius: 50%; ? ? ? ? ? ? background-color: white; ? ? ? ? ? ? border: 1px solid #cccccc; ? ? ? ? } ? ? ? ?? ? ? ? ? .loginBox h5 { ? ? ? ? ? ? /* position: relative; */ ? ? ? ? ? ? margin-bottom: 18px; ? ? ? ? ? ? font-weight: normal; ? ? ? ? ? ? font-size: 16px; ? ? ? ? ? ? cursor: move; ? ? ? ? } ? ? ? ?? ? ? ? ? .uname { ? ? ? ? ? ? margin: 18px 0 15px 15px; ? ? ? ? ? ? height: 25px; ? ? ? ? ? ? line-height: 25px; ? ? ? ? } ? ? ? ?? ? ? ? ? .uname span, ? ? ? ? .psw span { ? ? ? ? ? ? font-size: 12px; ? ? ? ? } ? ? ? ?? ? ? ? ? .uname input, ? ? ? ? .psw input { ? ? ? ? ? ? height: 25px; ? ? ? ? ? ? width: 230px; ? ? ? ? ? ? font-size: 12px; ? ? ? ? ? ? outline: none; ? ? ? ? } ? ? ? ?? ? ? ? ? .loginBox button { ? ? ? ? ? ? margin: 20px; ? ? ? ? ? ? width: 190px; ? ? ? ? ? ? height: 30px; ? ? ? ? ? ? border: 1px solid #cccccc; ? ? ? ? } ? ? </style> </head> <body> ? ? <div class="dianji">點(diǎn)擊,彈出登錄框</div> ? ? <div class="loginBox"> ? ? ? ? <span class="close">關(guān)閉</span> ? ? ? ? <h5>登錄會(huì)員</h5> ? ? ? ? <div class="uname"><span>用戶名:</span> <input type="text" placeholder="請(qǐng)輸入用戶名"></div> ? ? ? ? <div class="psw"><span>登錄密碼:</span> <input type="password" placeholder="請(qǐng)輸入密碼"></div> ? ? ? ? <button>登錄</button> ? ? </div> ? ? <script> ? ? ? ? var dianji = document.querySelector('.dianji'); ? ? ? ? var loginBox = document.querySelector('.loginBox'); ? ? ? ? var close = document.querySelector('.close'); ? ? ? ? var bodyy = document.body; ? ? ? ? var move_area = document.querySelector('h5'); ? ? ? ? dianji.addEventListener('click', function() { ? ? ? ? ? ? bodyy.style.backgroundColor = '#ccc'; ? ? ? ? ? ? loginBox.style.display = 'block'; ? ? ? ? }) ? ? ? ? close.addEventListener('click', function() { ? ? ? ? ? ? bodyy.style.backgroundColor = 'white'; ? ? ? ? ? ? loginBox.style.display = 'none'; ? ? ? ? }) ? ? ? ? move_area.addEventListener('mousedown', function(e) { ? ? ? ? ? ? var x = e.pageX - loginBox.offsetLeft; ? ? ? ? ? ? var y = e.pageY - loginBox.offsetTop; ? ? ? ? ? ? console.log('x=' + x + ' ?y=' + y); ? ? ? ? ? ? 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)時(shí)間解除 ? ? ? ? ? ? document.addEventListener('mouseup', function() { ? ? ? ? ? ? ? ? document.removeEventListener('mousemove', move); ? ? ? ? ? ? }) ? ? ? ? }) ? ? </script> </body> </html>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
js計(jì)算兩個(gè)時(shí)間差 天 時(shí) 分 秒 毫秒的代碼
這篇文章主要介紹了js計(jì)算兩個(gè)時(shí)間差 天 時(shí) 分 秒 毫秒的實(shí)例代碼,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-05-05純 JS 實(shí)現(xiàn)放大縮小拖拽功能(完整代碼)
這篇文章主要介紹了純js實(shí)現(xiàn)放大縮小拖拽功能,文中給大家提到了在開發(fā)過程中遇到的一些問題及解決方法,需要的朋友可以參考下2019-11-11List all the Databases on a SQL Server
List all the Databases on a SQL Server...2007-06-06JavaScript實(shí)現(xiàn)繼承的4種方法總結(jié)
這篇文章主要介紹了JavaScript實(shí)現(xiàn)繼承的4種方法總結(jié),本文給出了原型鏈繼承、構(gòu)造繼承、實(shí)例繼承、拷貝繼承等實(shí)現(xiàn)JS繼承的方法,需要的朋友可以參考下2014-10-10js實(shí)現(xiàn)文章文字大小字號(hào)功能完整實(shí)例
這篇文章主要介紹了js實(shí)現(xiàn)文章文字大小字號(hào)功能的實(shí)現(xiàn)方法,可根據(jù)用戶習(xí)慣調(diào)整顯示文字的大小.詳細(xì)講述了實(shí)現(xiàn)這一功能的完整步驟.是非常實(shí)用的技巧,需要的朋友可以參考下2014-11-11兩種不同的方法實(shí)現(xiàn)js對(duì)checkbox進(jìn)行全選和反選
這篇文章主要介紹了通過兩種不同的方法實(shí)現(xiàn)js對(duì)checkbox進(jìn)行全選和反選,需要的朋友可以參考下2014-05-05javascript html5搖一搖功能的實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了javascript html5搖一搖功能的實(shí)現(xiàn)過程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-04-04JS+CSS實(shí)現(xiàn)帶小三角指引的滑動(dòng)門效果
這篇文章主要介紹了JS+CSS實(shí)現(xiàn)帶小三角指引的滑動(dòng)門效果,可實(shí)現(xiàn)帶有箭頭提示效果的滑動(dòng)門功能,涉及JavaScript動(dòng)態(tài)操作頁面元素樣式的相關(guān)技巧,需要的朋友可以參考下2015-09-09JS圖片定時(shí)翻滾效果實(shí)現(xiàn)方法
這篇文章主要介紹了JS圖片定時(shí)翻滾效果實(shí)現(xiàn)方法,涉及javascript結(jié)合時(shí)間函數(shù)實(shí)現(xiàn)頁面元素動(dòng)態(tài)切換的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06