如何“#” (哈希标记,数字符号)关闭模式框? [英] How does "#" (hash mark, number sign) close a modal box?

查看:162
本文介绍了如何“#” (哈希标记,数字符号)关闭模式框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近阅读了这篇关于创建一个纯CSS模式框的文章:



我测试过代码,效果很好。我将它用于我网站的联系人和注册/登录页面。



有一部分代码我没有完全掌握。



这是怎么回事...

 < a href =#close title =Closeclass =close> X< / a> 

... 关闭方块?

CSS中没有任何内容隐藏或移除 close 类的元素,也没有任何类似的函数针对 href #close title 。这只是一行代码本身,当使用点击它时,模式窗口会关闭(整齐,优雅,我可能会添加)。



很明显, 关闭文本并不重要。文本可以是任何东西,并且该功能可以工作。但是,如果删除了#,则样式失败,但窗口仍然关闭。

那么#有什么特别之处?



.modalDialog {position:fixed; font-family:Arial,Helvetica,sans-serif; top:0;正确:0;底部:0;左:0;背景:rgba(0,0,0,0.8); z-index:99999;不透明度:0;过渡:不透明400ms缓和; pointer-events:none;}。modalDialog:target {opacity:1; pointer-events:auto;}。modalDialog> div {width:400px;位置:相对;保证金:10%汽车; padding:5px 20px 13px 20px; border-radius:10px;背景:#fff;}。close {background:#606061;颜色:#FFFFFF; line-height:25px;位置:绝对;正确:-12px; text-align:center;顶部:-10px;宽度:24px;文字修饰:无; font-weight:bold; border-radius:12px; box-shadow:1px 1px 3px#000;}。close:hover {background:#00d9ff;}

 < a href =#openModal>打开模式< / a>< div id =openModalclass =modalDialog> < DIV> < a href =#title =Closeclass =close> X< / a> < h2>模式框< / h2> < p>这是一个可以使用CSS3的权力创建的示例模式框。< / p> < p>您可以在这里做很多事情,例如弹出广告,显示您的网站加载时间,或为用户创建登录/注册表单。< / p> < / div>< / div>  

解决方案

当你点击href =#openModal时,它会找到ID =openModal的片段,并将焦点放在它上面。改变它的状态。 css .modalDialog:target会使模态显示为不透明:1。



当您点击href =#close时,它会尝试查找片段ID =close,它不存在。它也消除了其他片段的焦点,使得modalDialog变得焦点集中。所以没有什么重点。和模式关闭。


I recently read this article about creating a pure CSS modal box:

I've tested the code and it works great. I'm using it for my site's contact and register/log-in pages.

There is one part of the code I haven't fully grasped.

How does this...

<a href="#close" title="Close" class="close">X</a>

...close the box?

There is nothing in the CSS that hides or removes an element with a close class, nor any similar function targeting href, #close or title. It's simply that one line of code by itself, and when the use clicks on it, the modal window closes (neatly and elegantly, I might add).

It's clear that the "close" text doesn't matter. The text could be anything and the function works. But if "#" is removed, the styling fails, but the window still closes.

So what's so special about "#"?

.modalDialog {
  position: fixed;
  font-family: Arial, Helvetica, sans-serif;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background: rgba(0, 0, 0, 0.8);
  z-index: 99999;
  opacity: 0;
  transition: opacity 400ms ease-in;
  pointer-events: none;
}
.modalDialog:target {
  opacity: 1;
  pointer-events: auto;
}
.modalDialog > div {
  width: 400px;
  position: relative;
  margin: 10% auto;
  padding: 5px 20px 13px 20px;
  border-radius: 10px;
  background: #fff;
}
.close {
  background: #606061;
  color: #FFFFFF;
  line-height: 25px;
  position: absolute;
  right: -12px;
  text-align: center;
  top: -10px;
  width: 24px;
  text-decoration: none;
  font-weight: bold;
  border-radius: 12px;
  box-shadow: 1px 1px 3px #000;
}
.close:hover {
  background: #00d9ff;
}

<a href="#openModal">Open Modal</a>

<div id="openModal" class="modalDialog">
  <div>
    <a href="#" title="Close" class="close">X</a>
    <h2>Modal Box</h2>
    <p>This is a sample modal box that can be created using the powers of CSS3.</p>
    <p>You could do a lot of things here like have a pop-up ad that shows when your website loads, or create a login/register form for users.</p>
  </div>
</div>

解决方案

When you click on href="#openModal", it finds the fragment with id="openModal" and puts the focus on it. Changing its state. The css .modalDialog:target comes into action making the modal visible with opacity:1.

When you click on href="#close", it tries to find fragment id="close", which is not present. It also removes focus from any other fragment, making modalDialog loose focus. So nothing remains in focus. and modal closes.

这篇关于如何“#” (哈希标记,数字符号)关闭模式框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆