惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
大猫的无限游戏
大猫的无限游戏
J
Java Code Geeks
MongoDB | Blog
MongoDB | Blog
Martin Fowler
Martin Fowler
GbyAI
GbyAI
Microsoft Azure Blog
Microsoft Azure Blog
Recent Announcements
Recent Announcements
F
Fortinet All Blogs
B
Blog
U
Unit 42
B
Blog RSS Feed
D
DataBreaches.Net
Google DeepMind News
Google DeepMind News
人人都是产品经理
人人都是产品经理
腾讯CDC
量子位
酷 壳 – CoolShell
酷 壳 – CoolShell
V
Visual Studio Blog
博客园 - 聂微东
MyScale Blog
MyScale Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 三生石上(FineUI控件)
Engineering at Meta
Engineering at Meta

vivaxy's Blog

SameSite Cookies 解读 - vivaxy's Blog VSCode Conventional Commits 插件 - vivaxy's Blog VSCode Conventional Commits Extension - vivaxy's Blog How Babel Is Built - vivaxy's Blog 一步一步解码 PNG 图片 - vivaxy's Blog Babel 的工程化实现 - vivaxy's Blog 浏览器图像转换手册 - vivaxy's Blog Decoding A PNG Image with JavaScript Step by Step Comprehensive Image Processing on Browsers Use npm Registry to Restric Installing Client Alfred 4 Workflow Open in VSCode Git Tag And Push Git Tag 基于 Custom Elements 的组件化开发 - vivaxy's Blog JavaScript PNG 图片编码和解码 - vivaxy's Blog 如何在多个模块中共享异步数据 - vivaxy's Blog JavaScript 函数式编程初窥 - vivaxy's Blog 在浏览器中使用 JavaScript 模块 - vivaxy's Blog Karabiner Elements Hyper 改键设置 - vivaxy's Blog Levenshtein 距离 - vivaxy's Blog Levenshtein 距离 - vivaxy's Blog
用纯 CSS 实现弹窗 - vivaxy's Blog
vivaxy · 2017-10-26 · via vivaxy's Blog

知识点

  • a 标签点击改变页面链接中的 hash 部分。
  • :target 选择器可以选中和页面 hash 相同的标签(标签的 id 和页面 hash 相同)。

代码实现

先在页面中定义弹窗 id="modal" 以关联 hash。

添加打开弹窗的按钮,a 标签,链接地址是弹窗的 id。

<a href="#modal">open modal</a>
<div id="modal">
    <h1>modal</h1>
</div>

在弹窗上添加样式 class="modal",在弹窗里面添加关闭按钮 <a href="#">close</a>

<div class="modal" id="modal">
    <h1>modal</h1>
    <a href="#">close</a>
</div>

在 css 中默认隐藏弹窗,同时设置弹窗的基础样式。

.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: white;
}

在 css 中添加 :target 选择器,让 hash 值和弹窗的 id 相同时,弹窗可以展示出来。

.modal:target {
    display: block;
}

参考链接