简体   繁体   English

单击按钮时如何使自定义弹出?

[英]How to make a custom pop up when a button is clicked?

I have a webpage with a lot of buttons on it and I've made a custom popup that should popup in the center of the screen when a button is clicked.我有一个网页,上面有很多按钮,我制作了一个自定义弹出窗口,单击按钮时应该在屏幕中央弹出。 Ideally the screen darkens when the window comes up and you can click off anywhere on the screen to make the window go away.理想情况下,当 window 出现时屏幕会变暗,您可以单击屏幕上的任意位置以使 window go 消失。

How is this done?这是怎么做到的? Below is a snippet of my buttons + the popup I'd like to display when a button is pressed.下面是我的按钮片段+按下按钮时我想显示的弹出窗口。 The CSS window has display:hidden at the moment however.然而,CSS window 有display:hidden

 var containerDiv = document.querySelector("#main"); let tempCounterBtn = 0; for (let i = 0; i < 2; i++) { let newDiv = document.createElement("div"); newDiv.id = "div-" + (i + 1); containerDiv.appendChild(newDiv); for (let x = 0; x < 6; x++) { let btn = document.createElement("button"); btn.id = "button-" + (tempCounterBtn + 1); tempCounterBtn++; newDiv.appendChild(btn); } }
 body { display: flex; justify-content: center; margin: 1.2em; } button { background: rgba(0, 0, 0, 0); color: #a5afaa; border: 1px solid white; border-radius: .6em; padding: 3em; } button:hover { border-color: #fff8cc; box-shadow: 0em 0em 1em 0em yellow; } #main { border: solid 2px #939388; border-radius: 10px; background-color: #0f0f3de8; box-shadow: 0 0.1em 0.3em 0.1em rgba(0, 0, 0, 0.6); }.inner { display: flex; align-items: center; flex-direction: column; justify-content: center; padding: 20px; }.smallFrame { display: none; border: solid 2px #939388; border-radius: 10px; background-color: #0f0f3de8; box-shadow: 0 0.1em 0.3em 0.1em rgba(0, 0, 0, 0.6); width: 400px; height: 250px; }
 <div id="main"> <div id="inner-15" class="inner"></div> </div> <div class="smallFrame"></div>

try to read the documentation that I provided, hopefully it can help https://www.w3schools.com/howto/howto_css_modals.asp尝试阅读我提供的文档,希望它可以帮助https://www.w3schools.com/howto/howto_css_modals.asp

Changes变化

div#main has been changed to dialog#main . div#main已更改为dialog#main <dialog> is a built-in HTML tag and is normally hidden unless it has the open attribute or has been opened by one of it's methods either .showModal() or .show() . <dialog>是一个内置的 HTML 标记,通常是隐藏的,除非它具有open属性或已通过其方法之一打开.showModal().show() .showModal() is what you'd be interested in because there's a special CSS property that only applies to fullscreen backgrounds or a dialog's backdrop -- it's appropriately called ::backdrop (see CSS of example). .showModal()是您感兴趣的,因为有一个特殊的 CSS 属性仅适用于全屏背景或对话框的背景——它被恰当地称为::backdrop (参见示例 CSS)。

Added 2 buttons one to close the <dialog> and another to open it.添加了 2 个按钮,一个用于关闭<dialog> ,另一个用于打开它。

Added a flag which is true to start out at then once the <dialog> opens that button generating function starts and then that flag is set to false .添加了一个flag ,当<dialog>打开生成 function 的按钮开始时,该标志为true ,然后该标志设置为false I had to stop it because I'm not sure if you wanted it to keep making buttons every time the <dialog> opens or not.我不得不停止它,因为我不确定您是否希望它在每次<dialog>打开时继续制作按钮。 Anyways, the flag is easily removed or you could adjust the conditions in which the flag is set to.无论如何,该flag很容易删除,或者您可以调整flag设置的条件。

 const modal = document.querySelector("#main"); const show = document.querySelector('.show'); const hide = document.querySelector('.hide'); let flag = true; show.onclick = popUp; hide.onclick = e => modal.close(); function popUp(e) { modal.showModal(); if (;flag) { return; } let tempCounterBtn = 0; for (let i = 0; i < 2. i++) { let newDiv = document;createElement("div"). newDiv;id = "div-" + (i + 1). modal;appendChild(newDiv); for (let x = 0; x < 6. x++) { let btn = document;createElement("button"). btn;id = "button-" + (tempCounterBtn + 1); tempCounterBtn++. newDiv;appendChild(btn); } } flag = false; }
 html { font: 300 2ch/1.25 'Segoe UI' } body { display: flex; justify-content: center; margin: 1.2em; } button { background: rgba(0, 0, 0, 0); color: #a5afaa; border: 1px solid white; border-radius: .6em; padding: 3em; } button:hover { border-color: #fff8cc; box-shadow: 0em 0em 1em 0em yellow; } #main { border: solid 2px #939388; border-radius: 10px; background-color: #0f0f3de8; box-shadow: 0 0.1em 0.3em 0.1em rgba(0, 0, 0, 0.6); } #main::backdrop { background: rgba(0, 0, 0, 0.5); }.inner { display: flex; align-items: center; flex-direction: column; justify-content: center; padding: 20px; }.smallFrame { display: none; border: solid 2px #939388; border-radius: 10px; background-color: #0f0f3de8; box-shadow: 0 0.1em 0.3em 0.1em rgba(0, 0, 0, 0.6); width: 400px; height: 250px; }.hide { padding: 0.5em; float: right; }.show:hover, .hide:hover { border-color: #fff8cc; box-shadow: 0em 0em 1em 0em lime; cursor: pointer; }
 <dialog id="main"> <button class='hide'>Close</button> <div id="inner-15" class="inner"></div> </dialog> <button class='show'>Show</button> <div class="smallFrame"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM