css中怎样用outline-offset实现加号动画效果?
Admin 2021-10-20 群英技术资讯 1096 次浏览
本文给大家分享在css中用outline-offset实现加号动画效果,也就是黑色边框缩小变成加号的动画,具体的实现效果及代码如下,感兴趣的朋友可以了解看看。
假设有这么一个初始代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div {
margin-left: 100px;
margin-top: 100px;
padding: 0;
width: 200px;
height: 200px;
background-color: green;
outline: 20px solid #000;
outline-offset: 10px;
}
</style>
</head>
<body>
<div></div>
</body>
</html>
其效果如下:

然后再把这个outline-offset属性的值改为-118px,那么就会把边框变成一个加号 当然我这里为了效果显著一些,我加了一个动画效果来显示,如下代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div {
margin-left: 100px;
margin-top: 100px;
padding: 0;
width: 200px;
height: 200px;
background-color: green;
outline: 20px solid #000;
animation: move 3s infinite;
}
@keyframes move {
0% {
outline-offset: 10px;
}
100% {
outline-offset: -118px;
}
}
</style>
</head>
<body>
<div></div>
</body>
</html>
其效果如下:

使用outline-offset做加号的总结
我觉得这个很有意思,在这里我试了很多次,在这里我总结了一下使用outline-offset属性负值的条件:
在这个例子后,我又在想,CSS 属性可以取负值的属性和地方有很多,也有许多意想不到的效果。大家最为熟知的就是负margin,使用负的 marign,可以用来实现类似多列等高布局、垂直居中等等。那还有没有其他一些有意思的负值使用技巧呢?
下文就再介绍一些 CSS 负值有意思的使用场景。
使用 scale(-1) 实现翻转
通常,我们要实现一个元素的 180° 翻转,我们会使用 transform: rotate(180deg) ,这里有个小技巧,使用 transform: scale(-1) 可以达到同样的效果。看个 Demo:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.g_container {
position: absolute;
margin: 100px 0 0 500px;
}
.item {
width: 100px;
height: 100px;
background-color: green;
position: relative;
border-radius: 50%;
}
.item {
transform: rotate(0) translate(-80px, 0) ;
}
.item:nth-child(1) {
animation: rotate 3s infinite linear;
}
.item:nth-child(2) {
animation: rotate 3s infinite 1s linear;
}
.item:nth-child(3) {
animation: rotate 3s infinite 2s linear;
}
@keyframes rotate {
100% {
transform: rotate(360deg) translate(-80px, 0) ;
}
}
</style>
</head>
<body>
<div class="g_container">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
</body>
</html>
看看效果:

当然,如果想要让三个球同时运动,去掉这个延迟,那么可以改成这样的代码:
.item:nth-child(1) {
animation: rotate 3s infinite linear;
}
.item:nth-child(2) {
animation: rotate 3s infinite -1s linear;
}
.item:nth-child(3) {
animation: rotate 3s infinite -2s linear;
}
其效果我就不说了,就是同时运动,可参照上面的那个效果
负值 margin
负值 margin 在 CSS 中算是运用的比较多的,元素的外边距可以设置为负值。
在 flexbox 布局规范还没流行之前,实现多行等高布局还是需要下一番功夫的。其中一种方法便是使用正 padding 负 margin 相消的方法。
有如下一个布局:

左右两栏的内容都是不确定的,也就是高度未知。但是希望无论左侧内容较多还是右侧内容较多,两栏的高度始终保持一致。
OK,其中一种 Hack 办法便是使用一个很大的正 padding 和相同的负 margin 相消的方法填充左右两栏:
.left {
...
padding-bottom: 9999px;
margin-bottom: -9999px;
}
.right {
...
padding-bottom: 9999px;
margin-bottom: -9999px;
}
可以做到无论左右两栏高度如何变化,高度较低的那一栏都会随着另外一栏变化。
总结一下
除了这些,还有很多的属性,例子没有列出来(因作者的水平和时间有限),例如:
总结
以上就是在css中用outline-offset实现加号动画效果的代码,需要的朋友可以参考学习,希望对大家学习css 有帮助,想要了解更多可以继续浏览群英网络其他相关的文章。
文本转载自脚本之家
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
在用flex布局时,发现有两个属性功能好像有点类似:align-items和align-content,乍看之下,它们都是用于定义flex容器中元素在交叉轴(主轴为flex-deriction定义的方向,默认为row,那么交叉轴跟主轴垂直即为column,反之它们互调,flex基本的概念如下图所示)上的对齐方式,那么它们之间有什么区别呢?
这篇文章主要介绍了css 评分效果的星星示例的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
这篇文章主要介绍了浅谈CSS 伪元素&伪类的妙用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
在css3中,我们可以实现很多丰富有趣的图形,今天给大家分享的是用css3怎样画一个心形的方法,实现效果、实现思路和代码如下,感兴趣的朋友可以参考,下面跟随小编一起来看看吧。
效果预览思路将当前列表滚动至最后一项的末尾,然后瞬间切换回第一项问题点1.用什么方式实现无限轮播。这个问题就是列表滚动到最后时底部会留白(有多余空间)如何处理?
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008