弹出框也是前端里面经常使用的一个应用场景了,最开始想到的是用js实现这个效果,看到codepen上面有用css实现的。其实就是用到了css里面的一个:target选择器+visibility属性。

URL 带有后面跟有锚名称 #,指向文档内某个具体的元素。这个被链接的元素就是目标元素(target element)。:target 选择器可用于选取当前活动的目标元素。

示例代码:

<div class="container">
<a href="#popup" class="button">删除</a>
<div class="popup" id="popup">
<div class="popup-inner">
<div class="popup__text">
<h3>删除宝贝</h3>
<p>确定要删除该宝贝吗?</p>
<p><span><a href="javascript:void(0)">确定</a></span><span><a href="#">关闭</a></span></p>
</div>
<a href="#" class="popup__close">&times;</a>
</div>
</div>
</div> scss代码:
$main-color: #9191E9; * {
box-sizing: border-box;
padding: 0;
margin: 0;
} html, body {
font-family: 'Raleway', sans-serif;
font-size: 16px;
} .container {
background-color: $main-color;
display: flex;
align-items: center;
justify-content: center;
width: 100vw;
height: 100vh;
} .button {
text-decoration: none;
font-size: .875rem;
} .popup {
display: flex;
align-items: center;
justify-content: center;
position: fixed;
bottom: 0;
right: 0;
width: 100vw;
height: 100vh;
z-index: 2;
visibility: hidden;
&-inner {
background-color: #fff;
width: 400px;
height: 180px;
display: flex;
align-items: center;
position: relative;
bottom: -100vw;
right: -100vh;
transform: rotate(32deg);
transition: .64s ease-in-out;
}
&__text {
padding: 2rem;
h3 {
font-size: 1.2rem;
font-weight: 600;
margin-bottom: 1.2rem;
}
p {
font-size: .75rem;
margin-top: 1rem;
}
span {
display: inline-block;
padding: .42rem 1rem;
margin-right: .625rem;
a {
text-decoration: none;
}
&:first-child {
background-color: #52A0E5;
border: 1px solid #52A0E5;
a {
color: white;
&:hover {
text-decoration: underline;
}
}
}
&:last-child {
border: 1px solid grey;
a {
color: grey;
&:hover {
color: red;
text-decoration:underline;
}
}
}
}
}
&__close {
position: absolute;
right: 1.8rem;
top: 1.8rem;
font-size: 1.5rem;
color: grey;
text-decoration: none;
font-weight: 800;
}
&:target {
visibility: visible;
.popup-inner {
bottom: 0;
right: 0;
transform: rotate(0);
}
}
}

scss代码有点略长,核心就是.popup:targetvisibility的结合,为什么不用opcaity和display呢?在这个场景中,opacity会影响html页面里的锚(这里面的popup遮挡到了body里面的a标签),而display不支持transition,并使transition失效。这里为了让弹出框不那么突兀地出现,加了一个小小的动画效果。

css实现弹出框的更多相关文章

  1. CSS自定义弹出框

    <script type="text/javascript" language="javascript"> function sAlert(str) ...

  2. div非弹出框半透明遮罩实现全屏幕遮盖css实现

    IE浏览器下设置元素css背景为透明: background-color: rgb(0, 0, 0); filter: alpha(opacity=20); 非IE浏览器下设置元素css背景为透明: ...

  3. html+css源码之实现登录弹出框遮罩层效果

    在web开发中,很多网站都做了一些特别炫丽的效果,比如用户登录弹框遮罩层效果,本文章向大家介绍css如何实现登录弹出框遮罩层效果,需要的朋友可以参考一下本文章的源代码. html+css实现登录弹出框 ...

  4. css 弹出框

    最近想弄一个类似登陆框的那种弹出框,其实网上已经有很多例子,而且也有相应的插件,例如:jquery-ui的,可直接使用,而我就简单的弄了个简易版的登陆框,真的很简易. 其实原理就是设置两个div,一个 ...

  5. html、css和js原生写一个模态弹出框,顺便解决父元素半透明子元素不透明效果

    模态框: html部分: <!-- 按钮 --> <button id="box" onclick="pop_box()">弹出框< ...

  6. css超简单实现div页面居中【适合做弹出框】

    1.前言 现在项目中用到弹出框的话大部分都是直接用控件的.不过有控件虽方便,但有时候会有冲突的地方.我上次用layui的弹出框控件,然后也用到了百度的编辑器uEditor,然后一切都好好的,结果编辑赋 ...

  7. angularjs 弹出框 $modal

    angularjs 弹出框 $modal 标签: angularjs 2015-11-04 09:50 8664人阅读 评论(1) 收藏 举报  分类: Angularjs(3)  $modal只有一 ...

  8. bootstrap中popover.js(弹出框)使用总结+案例

    bootstrap中popover.js(弹出框)使用总结+案例 *转载请注明出处: 作者:willingtolove: http://www.cnblogs.com/willingtolove/p/ ...

  9. 使用jsonp跨域调用百度js实现搜索框智能提示,并实现鼠标和键盘对弹出框里候选词的操作【附源码】

    项目中常常用到搜索,特别是导航类的网站.自己做关键字搜索不太现实,直接调用百度的是最好的选择.使用jquery.ajax的jsonp方法可以异域调用到百度的js并拿到返回值,当然$.getScript ...

随机推荐

  1. DVWA 黑客攻防演练(三)命令行注入(Command Injection)

    文章会讨论 DVWA 中低.中.高.不可能级别的命令行注入 这里的需求是在服务器上可以 ping 一下其他的服务器 低级 Hacker 试下输入 192.168.31.130; cat /etc/ap ...

  2. 第三篇 Html-label标签

    label标签 用户获取文字,使得关联的标签获取光标 <!DOCTYPE html> <html lang="en"> <head> <m ...

  3. windows/Linux下的程序员文档浏览工具

    Dash + Alfred https://www.jianshu.com/p/77d2bf8df81f 对于程序员来说,查看api文档是非常频繁,经常窗口之间切换非常麻烦,mac下就有一个查文档的神 ...

  4. Anaconda安装sasl,thrift,thrift-sasl,PyHive连接Hive

    一.安装sasl 安装失败,前往:https://www.lfd.uci.edu/~gohlke/pythonlibs/#sasl下载对应自己python版本的sasl 本地安装: 二.安装thrif ...

  5. 基于TCP 协议的RPC

    前言: 环境: windown 10 Eclipse JDK 1.8 RPC的概念: RPC 是远程过程调用,是分布式网站的基础. 实验 SayHelloService.java 接口类,用于规范 S ...

  6. 【Python 06】汇率兑换1.0-1(IPO与字符串转数字)

    1.案例描述 设计一个汇率换算器程序,功能是将外币换算成人民币. 2.案例分析 将问题划分为输入.处理及输出三部分(IPO) 3.上机实验 rmb_str_value = input('请输入人民币( ...

  7. 【English EMail】Compensation Planning Memo

    Data Foundation  数据基础 [faʊnˈdeʃən] Interesting newsletter for data foundation practice. Annual Code ...

  8. maven 出现错误 -source 1.5 中不支持 diamond 运算符

    mvn clean package -DskipTests 出现如下错误: -source 1.5 中不支持 diamond 运算符 [ERROR] (请使用 -source 7 或更高版本以启用 d ...

  9. MapReduce shuffle过程剖析及调优

    MapReduce简介 在Hadoop MapReduce中,框架会确保reduce收到的输入数据是根据key排序过的.数据从Mapper输出到Reducer接收,是一个很复杂的过程,框架处理了所有问 ...

  10. 26 python 初学(线程、同步锁、死锁和递归锁)

    参考博客: www.cnblogs.com/yuanchenqi/articles/5733873.html 并发:一段时间内做一些事情 并行:同时做多件事情 线程是操作系统能够进行运算调度的基本单位 ...