不要在鼠标向上事件中关闭模式
Posted
技术标签:
【中文标题】不要在鼠标向上事件中关闭模式【英文标题】:Do not close the mode in the mouse up event 【发布时间】:2021-08-23 14:48:40 【问题描述】:我在我的项目中使用了 micro modal js 包,但是我遇到了问题。当发生鼠标按下或鼠标向上事件时,此模态关闭奖牌范围之外。但我想要 mouse-up 事件不要做任何事情,只需 mouse-down 事件就可以关闭奖牌。 这是我的 html 代码。
let openButton = document.getElementById('open-modal');
openButton.addEventListener('click', function()
MicroModal.show('modal-1');
);
/**************************\
Basic Modal Styles
\**************************/
.modal
font-family: -apple-system,BlinkMacSystemFont,avenir next,avenir,helvetica neue,helvetica,ubuntu,roboto,noto,segoe ui,arial,sans-serif;
.modal__overlay
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0,0,0,0.6);
display: flex;
justify-content: center;
align-items: center;
.modal__container
background-color: #fff;
padding: 30px;
max-width: 500px;
max-height: 100vh;
border-radius: 4px;
overflow-y: auto;
box-sizing: border-box;
.modal__header
display: flex;
justify-content: space-between;
align-items: center;
.modal__title
margin-top: 0;
margin-bottom: 0;
font-weight: 600;
font-size: 1.25rem;
line-height: 1.25;
color: #00449e;
box-sizing: border-box;
.modal__close
background: transparent;
border: 0;
.modal__header .modal__close:before content: "\2715";
.modal__content
margin-top: 2rem;
margin-bottom: 2rem;
line-height: 1.5;
color: rgba(0,0,0,.8);
.modal__btn
font-size: .875rem;
padding-left: 1rem;
padding-right: 1rem;
padding-top: .5rem;
padding-bottom: .5rem;
background-color: #e6e6e6;
color: rgba(0,0,0,.8);
border-radius: .25rem;
border-style: none;
border-width: 0;
cursor: pointer;
-webkit-appearance: button;
text-transform: none;
overflow: visible;
line-height: 1.15;
margin: 0;
will-change: transform;
-moz-osx-font-smoothing: grayscale;
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
-webkit-transform: translateZ(0);
transform: translateZ(0);
transition: -webkit-transform .25s ease-out;
transition: transform .25s ease-out;
transition: transform .25s ease-out,-webkit-transform .25s ease-out;
.modal__btn:focus, .modal__btn:hover
-webkit-transform: scale(1.05);
transform: scale(1.05);
.modal__btn-primary
background-color: #00449e;
color: #fff;
/**************************\
Demo Animation Style
\**************************/
@keyframes mmfadeIn
from opacity: 0;
to opacity: 1;
@keyframes mmfadeOut
from opacity: 1;
to opacity: 0;
@keyframes mmslideIn
from transform: translateY(15%);
to transform: translateY(0);
@keyframes mmslideOut
from transform: translateY(0);
to transform: translateY(-10%);
.micromodal-slide
display: none;
.micromodal-slide.is-open
display: block;
.micromodal-slide[aria-hidden="false"] .modal__overlay
animation: mmfadeIn .3s cubic-bezier(0.0, 0.0, 0.2, 1);
.micromodal-slide[aria-hidden="false"] .modal__container
animation: mmslideIn .3s cubic-bezier(0, 0, .2, 1);
.micromodal-slide[aria-hidden="true"] .modal__overlay
animation: mmfadeOut .3s cubic-bezier(0.0, 0.0, 0.2, 1);
.micromodal-slide[aria-hidden="true"] .modal__container
animation: mmslideOut .3s cubic-bezier(0, 0, .2, 1);
.micromodal-slide .modal__container,
.micromodal-slide .modal__overlay
will-change: transform;
<html>
<head>
<Title>MicroModal Toggle Pen</Title>
<script defer src="https://unpkg.com/micromodal/dist/micromodal.min.js"></script>
<script defer src="script.js"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>
<button type="button" id="open-modal">Open new modal</button>
<!-- New event modal -->
<div class="modal micromodal-slide" id="modal-1" aria-hidden="true">
<div class="modal__overlay" tabindex="-1" data-micromodal-close>
<div class="modal__container" role="dialog" aria-modal="true" aria-labelledby="modal-1-title">
<header class="modal__header">
<h2 class="modal__title" id="modal-1-title">
New event
</h2>
<button class="modal__close" aria-label="Close modal" data-micromodal-close></button>
</header>
<main class="modal__content" id="modal-1-content">
<input type="text" placeholder="name">
</main>
<footer class="modal__footer">
<button class="modal__btn modal__btn-primary">Continue</button>
<button class="modal__btn" data-micromodal-close aria-label="Close this dialog window">Close</button>
</footer>
</div>
</div>
</div>
<!-- Event 1 -->
<div class="modal micromodal-slide" id="event-modal-1" aria-hidden="true">
<div class="modal__overlay" tabindex="-1" data-micromodal-close>
<div class="modal__container" role="dialog" aria-modal="true" aria-labelledby="modal-1-title">
<header class="modal__header">
<h2 class="modal__title" id="modal-1-title">
Event 1
</h2>
<button class="modal__close" aria-label="Close modal" data-micromodal-close></button>
</header>
<main class="modal__content" id="modal-1-content">
Blah
</main>
<footer class="modal__footer">
<button class="modal__btn modal__btn-primary">Continue</button>
<button class="modal__btn" data-micromodal-close aria-label="Close this dialog window">Close</button>
</footer>
</div>
</div>
</div>
<!-- Event Modal 2 -->
<div class="modal micromodal-slide" id="event-modal-2" aria-hidden="true">
<div class="modal__overlay" tabindex="-1" data-micromodal-close>
<div class="modal__container" role="dialog" aria-modal="true" aria-labelledby="modal-1-title">
<header class="modal__header">
<h2 class="modal__title" id="modal-1-title">
Event 2
</h2>
<button class="modal__close" aria-label="Close modal" data-micromodal-close></button>
</header>
<main class="modal__content" id="modal-1-content">
blah
</main>
<footer class="modal__footer">
<button class="modal__btn modal__btn-primary">Continue</button>
<button class="modal__btn" data-micromodal-close aria-label="Close this dialog window">Close</button>
</footer>
</div>
</div>
</div>
</body>
</html>
如果有人可以帮助我,我将非常感激。
【问题讨论】:
听起来像是要在github.com/ghosh/Micromodal/issues报告的问题 【参考方案1】:解决方案是从您的modal__overlay
中删除data-micromodal-close
。这样做会阻止模式在您单击背景时关闭。
<div class="modal__overlay" tabindex="-1">
仅将data-micromodal-close
放在单击时应关闭模式的元素上,例如关闭按钮。
https://github.com/ghosh/micromodal/issues/132#issuecomment-478688942
【讨论】:
【参考方案2】:如果您希望在mouseup
987654321 mousedown
987654322 @(aka时选择文本和光标在模态或相反的情况下移动到模态)时,则仍然希望能够关闭模态点击覆盖本身,而不是你可以使用这个:
let openButton = document.getElementById('open-modal');
openButton.addEventListener('click', function()
MicroModal.show('modal-1');
);
const overlays = document.querySelectorAll(".modal__overlay");
for(let i = 0; i < overlays.length; i++)
const overlay = overlays[i];
let overlayMouseDown;
/* detect if clicked on overlay itself */
overlay.addEventListener("mousedown", e => overlayMouseDown = e.target === overlay);
/* prevent from closing when clicked on overlay and dragged into modal */
overlay.addEventListener("mouseout", e => overlayMouseDown = false);
overlay.addEventListener("click", e =>
if (e.target === overlay && !overlayMouseDown)
e.preventDefault();
e.stopPropagation();
);
/**************************\
Basic Modal Styles
\**************************/
.modal
font-family: -apple-system,BlinkMacSystemFont,avenir next,avenir,helvetica neue,helvetica,ubuntu,roboto,noto,segoe ui,arial,sans-serif;
.modal__overlay
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0,0,0,0.6);
display: flex;
justify-content: center;
align-items: center;
.modal__container
background-color: #fff;
padding: 30px;
max-width: 500px;
max-height: 100vh;
border-radius: 4px;
overflow-y: auto;
box-sizing: border-box;
.modal__header
display: flex;
justify-content: space-between;
align-items: center;
.modal__title
margin-top: 0;
margin-bottom: 0;
font-weight: 600;
font-size: 1.25rem;
line-height: 1.25;
color: #00449e;
box-sizing: border-box;
.modal__close
background: transparent;
border: 0;
.modal__header .modal__close:before content: "\2715";
.modal__content
margin-top: 2rem;
margin-bottom: 2rem;
line-height: 1.5;
color: rgba(0,0,0,.8);
.modal__btn
font-size: .875rem;
padding-left: 1rem;
padding-right: 1rem;
padding-top: .5rem;
padding-bottom: .5rem;
background-color: #e6e6e6;
color: rgba(0,0,0,.8);
border-radius: .25rem;
border-style: none;
border-width: 0;
cursor: pointer;
-webkit-appearance: button;
text-transform: none;
overflow: visible;
line-height: 1.15;
margin: 0;
will-change: transform;
-moz-osx-font-smoothing: grayscale;
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
-webkit-transform: translateZ(0);
transform: translateZ(0);
transition: -webkit-transform .25s ease-out;
transition: transform .25s ease-out;
transition: transform .25s ease-out,-webkit-transform .25s ease-out;
.modal__btn:focus, .modal__btn:hover
-webkit-transform: scale(1.05);
transform: scale(1.05);
.modal__btn-primary
background-color: #00449e;
color: #fff;
/**************************\
Demo Animation Style
\**************************/
@keyframes mmfadeIn
from opacity: 0;
to opacity: 1;
@keyframes mmfadeOut
from opacity: 1;
to opacity: 0;
@keyframes mmslideIn
from transform: translateY(15%);
to transform: translateY(0);
@keyframes mmslideOut
from transform: translateY(0);
to transform: translateY(-10%);
.micromodal-slide
display: none;
.micromodal-slide.is-open
display: block;
.micromodal-slide[aria-hidden="false"] .modal__overlay
animation: mmfadeIn .3s cubic-bezier(0.0, 0.0, 0.2, 1);
.micromodal-slide[aria-hidden="false"] .modal__container
animation: mmslideIn .3s cubic-bezier(0, 0, .2, 1);
.micromodal-slide[aria-hidden="true"] .modal__overlay
animation: mmfadeOut .3s cubic-bezier(0.0, 0.0, 0.2, 1);
.micromodal-slide[aria-hidden="true"] .modal__container
animation: mmslideOut .3s cubic-bezier(0, 0, .2, 1);
.micromodal-slide .modal__container,
.micromodal-slide .modal__overlay
will-change: transform;
<html>
<head>
<Title>MicroModal Toggle Pen</Title>
<script defer src="https://unpkg.com/micromodal/dist/micromodal.min.js"></script>
<script defer src="script.js"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>
<button type="button" id="open-modal">Open new modal</button>
<!-- New event modal -->
<div class="modal micromodal-slide" id="modal-1" aria-hidden="true">
<div class="modal__overlay" tabindex="-1" data-micromodal-close>
<div class="modal__container" role="dialog" aria-modal="true" aria-labelledby="modal-1-title">
<header class="modal__header">
<h2 class="modal__title" id="modal-1-title">
New event
</h2>
<button class="modal__close" aria-label="Close modal" data-micromodal-close></button>
</header>
<main class="modal__content" id="modal-1-content">
<input type="text" placeholder="name">
</main>
<footer class="modal__footer">
<button class="modal__btn modal__btn-primary">Continue</button>
<button class="modal__btn" data-micromodal-close aria-label="Close this dialog window">Close</button>
</footer>
</div>
</div>
</div>
<!-- Event 1 -->
<div class="modal micromodal-slide" id="event-modal-1" aria-hidden="true">
<div class="modal__overlay" tabindex="-1" data-micromodal-close>
<div class="modal__container" role="dialog" aria-modal="true" aria-labelledby="modal-1-title">
<header class="modal__header">
<h2 class="modal__title" id="modal-1-title">
Event 1
</h2>
<button class="modal__close" aria-label="Close modal" data-micromodal-close></button>
</header>
<main class="modal__content" id="modal-1-content">
Blah
</main>
<footer class="modal__footer">
<button class="modal__btn modal__btn-primary">Continue</button>
<button class="modal__btn" data-micromodal-close aria-label="Close this dialog window">Close</button>
</footer>
</div>
</div>
</div>
<!-- Event Modal 2 -->
<div class="modal micromodal-slide" id="event-modal-2" aria-hidden="true">
<div class="modal__overlay" tabindex="-1" data-micromodal-close>
<div class="modal__container" role="dialog" aria-modal="true" aria-labelledby="modal-1-title">
<header class="modal__header">
<h2 class="modal__title" id="modal-1-title">
Event 2
</h2>
<button class="modal__close" aria-label="Close modal" data-micromodal-close></button>
</header>
<main class="modal__content" id="modal-1-content">
blah
</main>
<footer class="modal__footer">
<button class="modal__btn modal__btn-primary">Continue</button>
<button class="modal__btn" data-micromodal-close aria-label="Close this dialog window">Close</button>
</footer>
</div>
</div>
</div>
</body>
</html>
【讨论】:
【参考方案3】:我首先从 modal__overlay 中删除了 data-micromodal-close 然后我写了这个 js 代码
let openButton = document.getElementById('open-modal');
const modalOverlay = document.querySelector('.modal__overlay');
openButton.addEventListener('click', function ()
MicroModal.show('modal-1');
);
modalOverlay.addEventListener('mousedown', (e) =>
if (e.target.closest('.modal__container'))
return;
MicroModal.close('modal-1');
);
【讨论】:
以上是关于不要在鼠标向上事件中关闭模式的主要内容,如果未能解决你的问题,请参考以下文章
在 Macbook PRO 中关闭 NSWindow 时 CEF 崩溃(仅在触摸板上不用于鼠标单击)