我有一个类,它可以在运行时将虚线样式边框属性应用于文本块。我正在尝试使用CSS找到一个解决方案,使边框像gif图像一样移动。
有没有办法实现这个效果?
我有一个类,它可以在运行时将虚线样式边框属性应用于文本块。我正在尝试使用CSS找到一个解决方案,使边框像gif图像一样移动。
有没有办法实现这个效果?
虽然不是CSS3,但它可以工作:http://matthewjamestaylor.com/blog/animated-photoshop-selection-on-a-web-page
您可以使用CSS3渐变来制作条纹,并通过动画化背景位置(粗略演示:http://codepen.io/christopheschwyzer/pen/CEwBI)而不需要图像,但我不建议这样做,因为它只能在Webkit上表现良好。
.outer {
position: absolute;
left: 100px;
top: 50px;
width: 100px;
height: 100px;
background-image: url(http://matthewjamestaylor.com/blog/selection.gif);
border: 1px solid;
}
.selected {
border: 0px;
}
.inner {
position:absolute;
bottom: 0;
top: 0;
left: 0;
right: 0;
background-color: #9CF;
}
.selected .inner {
margin: 1px;
}
<div class="outer selected">
<div class="inner" />
</div>
这取决于您希望动画看起来像什么。
通常情况下,通过 border-style
提供给您的样式是静态呈现的;无法对它们进行动画处理。
即使使用 CSS3,您的选择也相当有限。通过 border-image
,您可以做的最好的事情要么是使用精心制作的动画 GIF(再次取决于浏览器如何实现带有动画图像的 border-image
),要么使用渐变动画-您选择哪种取决于浏览器兼容性和您想要的效果。
否则,您可以尝试使用 ::before
和 ::after
伪元素来实现所需的效果。
作为一个警告,除非您能确保您的动画符合相关的 WCAG 指南,特别是 2.2.2 和 2.3,否则我强烈建议不要去追求动画 GIF 的外观。除了对某些人有危险性之外,粗制滥造的动画可能会比大多数人更令人讨厌-这就是当年使动画 GIF 如此臭名昭着的原因。
换句话说,要谨慎使用这种技术,只有当您知道它会增加用户体验而不是减少用户体验时才使用它。
blink
标签,还有什么更好的呢 :) - Myles Gray以下是一种相当灵活的 SCSS 选项:
$antlength: 50px;
$antwidth: 10px;
$antcolor: black;
@keyframes marching-ants {
0% {background-position: 0 0, $antlength 100%, 0 $antlength, 100% 0;}
100% {background-position: $antlength 0, 0 100%, 0 0, 100% $antlength;}
}
.ants {
background-image: linear-gradient(90deg, $antcolor 50%, transparent 50%),
linear-gradient(90deg, $antcolor 50%, transparent 50%),
linear-gradient(0, $antcolor 50%, transparent 50%),
linear-gradient(0, $antcolor 50%, transparent 50%);
background-repeat: repeat-x, repeat-x, repeat-y, repeat-y;
background-size: $antlength $antwidth, $antlength $antwidth, $antwidth $antlength, $antwidth $antlength;
animation: marching-ants 400ms infinite linear;
}
Collapsed to a snippet:
@keyframes marching-ants {
0% {
background-position: 0 0, 50px 100%, 0 50px, 100% 0;
}
100% {
background-position: 50px 0, 0 100%, 0 0, 100% 50px;
}
}
.ants {
background-image: linear-gradient(90deg, black 50%, transparent 50%), linear-gradient(90deg, black 50%, transparent 50%), linear-gradient(0, black 50%, transparent 50%), linear-gradient(0, black 50%, transparent 50%);
background-repeat: repeat-x, repeat-x, repeat-y, repeat-y;
background-size: 50px 10px, 50px 10px, 10px 50px, 10px 50px;
animation: marching-ants 400ms infinite linear;
}
<div class="ants">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
我也在寻找这样的解决方案,因为我正在尝试模拟Excel使用的动画边框,以指示当前选择已被剪切并等待粘贴。
俗气吗?不,在我打算使用的情况下不是。
我找到了这个jQuery插件:http://there4development.com/projects/animatedborder/,原帖发布者可能会想要试一试。
border-image
的例子。
优点:
.selected {
position: absolute;
width: 100px;
height: 100px;
top: 50px;
border: 1px solid transparent;
box-sizing: border-box;
border-image-outset: 0px;
border-image-repeat: repeat;
border-image-source: url("http://matthewjamestaylor.com/blog/selection-big.gif");
}
.v1 {
left: 100px;
border-image-slice: 6;
border-image-width: 1px;
}
.v2 {
left: 300px;
border-image-slice: 3;
border-image-width: 2px;
}
<p style="color: #aaa;">Lorem ipsum dolor sit amet, consectetur adipiscing elit. In aliquam metus hendrerit venenatis placerat. Morbi sem ex, egestas at egestas iaculis, imperdiet in mauris. Nulla facilisi. Aliquam eu porttitor libero, vel porta ipsum. Proin egestas consequat urna, et rhoncus arcu congue vitae. Maecenas at massa mollis, accumsan mauris in, faucibus ligula. Nulla sed lorem pharetra lacus dictum lobortis sit amet id tellus. Vestibulum porta auctor maximus. Fusce congue, orci et feugiat ultricies, turpis mi egestas est, nec vulputate nulla risus quis lorem. Sed vitae risus rhoncus, ultricies nunc non, mollis mauris.</p>
<div class="selected v1">
</div>
<div class="selected v2">
</div>
你可以在背景中使用GIF图像,唯一的解决方案是通过CSS来实现。否则就只能使用JavaScript。