- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试模仿 Twitter 的通知 动画
这是我到目前为止想出的:
$('button').click(function() {
$('#left').css('width', '400px');
$('#right').css('width', '400px');
});
.wrapper {
position: relative;
min-height: 50px;
}
.left {
position: absolute;
left: 0;
height: 50px;
background: #00AEEF;
}
.right {
position: absolute;
right: 0;
height: 50px;
background: #00AEEF;
}
.banner {
width: 0%;
overflow: hidden;
-webkit-transition: width 1s ease-in-out;
-moz-transition: width 1s ease-in-out;
-o-transition: width 1s ease-in-out;
transition: width 1s ease-in-out;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<div class="wrapper">
<div id="left" class="banner left"></div>
<div id="right" class="banner right"></div>
</div>
<div style="margin: 10px;">
<button>start animation</button>
</div>
</div>
但是为左右动画使用 2 个不同的 div 感觉就像是 hack。
是否有更好的那种类型的内置 css 动画(用于单个 div)?
最佳答案
(完整效果演示见下文)
您可以为 background-position
设置动画两个linear-gradients
放置在单个元素中(因此您甚至不需要再使用两个空元素来进行样式设置)例如
div {
background:
linear-gradient(to left, #00AEEF 50%, transparent 0),
linear-gradient(to right, #00AEEF 50%, transparent 0);
background-position: 50vw 0, -50vw 0;
background-repeat: no-repeat;
height: 50px;
transition: background-position 1s;
}
:checked + div {
background-position: 0 0, 0 0;
}
只需通过 js
设置一个类触发转换(为了简单起见,我用 :checked
伪类激活了效果)
您也可以通过相反的动画获得相同的效果:如果您在蓝色 background-color
上放置白色渐变你可以只为 background-size
设置动画像这样的渐变
div {
background: #00AEEF linear-gradient(to right, #fff, #fff);
background-position: 50% 0;
background-repeat: no-repeat;
background-size: 100% 100%;
height: 50px;
transition: background-size 1s;
}
:checked ~ div { background-size: 0 100%; }
Comparing the two approaches我个人更喜欢最后一个(更少的代码输入,一个单一的渐变动画,看起来更平滑。此外,第二个演示防止了第一个演示中有时会发生的烦人的舍入问题,当重新定位两个渐变时,当你从下面的截图可以看出)
要重新创建此通知的完整效果,标记和样式当然应该略有更改:从上一个演示开始,我将主要效果移到了 <a>
上。包装器内的元素,我插入了其他效果,如 @
带有动画的脉动,并在 5 秒后最后滑下。
右箭头由unicode符号U+3009
构成它被放置为 content
的 a::after
伪元素
注意:所有属性都是无前缀。必要时添加前缀
标记
<div class="notification">
<a href="#"><span>@</span>Miro mentioned you</a>
</div>
CSS (从谷歌字体嵌入 Lato 字体)
* {
font : 1rem "Lato", Arial;
box-sizing : border-box;
}
.notification {
position : relative;
overflow : hidden;
font-weight : 100;
font-size : 1.5rem;
}
.notification a {
display : block;
padding : 1em 3em 1em 2.25em;
width : 100%;
font-size : inherit;
font-weight : inherit;
color : transparent;
background : #00AEEF linear-gradient(to right, #fff, #fff);
text-decoration : none;
background-position : 50% 0;
background-repeat : no-repeat;
background-size : 100% 100%;
}
/* The at-sign: I've also tried to use :first-letter but it
* is unreliable when the first char is not a letter or a digit
*/
.notification a span {
position : absolute;
line-height : 1;
top : 50%;
left : 50%;
color : #fff;
font-weight : bold;
transform : translate(-50%, -50%) scale(0);
transform-origin : 50% 50%;
}
/* The arrow */
.notification a:after {
position : absolute;
content : "\3009";
right : 1em;
top : 50%;
transform : translateY(-50%);
}
/* sliding doors effect, color change and final slide down
* all with proper delays
*/
:checked ~ .notification a {
transition: background-size .2s, color .33s 1s, transform 1s 5s;
transform: translateY(100%);
background-size: 0 100%;
color: #fff;
}
/* pulsing and moving the @-sign */
:checked ~ .notification a span {
animation: pulse-at .66s ease-in-out .33s forwards;
}
@keyframes pulse-at {
0% { transform: scale(0) translate(-50%, -50%); }
20% { transform: scale(1.1) translate(-50%, -50%); }
25% { transform: scale(1) translate(-50%, -50%); }
40% { transform: scale(1) translate(-50%, -50%); left: 50%; }
100% { transform: scale(1) translate(0, -50%); left: 1em; }
}
最终结果
关于javascript - CSS 中的滑动门动画,如 Twitter 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35728553/
我想实现一个 bool NAND/NOR 门。问题是我在代码本身中即时学习的门没有输入,即我事先不知道它可能有多少输入。以下是 OR 的代码。但我想不出一种方法来为 NOR/NAND 做这件事。结果的
我正试图找到用 C 语言编写 XNOR 门的最有效方法。 if(VAL1 XNOR VAL2) { BLOCK; } 有什么建议吗? 谢谢。 最佳答案 有两个操作数,这很简单: if (val
我正在尝试进行 laravel 基本授权。我正在使用 gate 进行 laravel 授权。 表结构 User Table, Permission Table, Role, role_permissi
据我所知,我们有一个Youtube API,可通过使用以下API详细信息来获取基于某个地区的趋势YouTube视频: https://developers.google.com/youtube/v3/
我有一个看起来像这样的出租车列表: 1204725 2162 1300163 420247 我希望从上面的taxids中按顺序获得一个带有分类ID的文件: kingdom_id phylum
我一直在尝试弄清楚如何制作“ HitTest 门帖子”,其中帖子是按在最短的时间内获得最多支持排序的。 我有 2 个数据库表: 有趣的帖子: fun_post_upvotes: 我的最新代码仍然不起作
如何通过 Windows API 调用打开 CD/DVD 门? 最佳答案 如果您使用 .NET,这将起作用: http://www.dotnetspider.com/resources/15834-e
我们的核心目标是: 使用图像处理来读取/扫描建筑平面图图像(从 CAD 软件导出) 使用图像处理来读取/扫描建筑平面图图像(从 CAD 软件导出)提取各种直线和曲线,将它们分组为结构实体,如墙、柱、梁
给定 n 个元素 1,2,.........,n 上的二叉搜索树的后序遍历 P。您必须确定以 P 作为其后序遍历的唯一二叉搜索树。执行此操作的最有效算法的时间复杂度是多少? (a) theeta(lo
根据定义,门 1/sqrt(5) (I + 2iZ) 应作用于量子位 a|0> + b|1> 以将其转换为 1/sqrt (5) ((1+2i)a|0> + (1-2i)b|1>) 但每个 RUS 步
我有物种的分类 ID,我可以从 NCBI ( https://www.ncbi.nlm.nih.gov/Taxonomy/TaxIdentifier/tax_identifier.cgi ) 获得物种
我是一名优秀的程序员,十分优秀!