Home >Web Front-end >CSS Tutorial >How to achieve dotted border scrolling effect in css
Let’s take a look at the effect first:
The implementation code is as follows:
HTML
<div class="box"> <p>测试测试</p> </div>
Easy-way
Achieved through background images.
.box { width: 100px; height: 100px; position: relative; background: url(https://www.zhangxinxu.com/study/image/selection.gif); p { position: absolute; left: 0; top: 0; right: 0; bottom: 0; margin: auto; height: calc(100% - 2px); width: calc(100% - 2px); background-color: #fff; } }
(Video tutorial recommendation: css video tutorial)
repeating-linear-gradient
135 degree repeating linear gradient, p spread height, The white background covers the outer div gradient.
.box { width: 100px; height: 100px; background: repeating-linear-gradient( 135deg, transparent, transparent 4px, #000 4px, #000 8px ); overflow: hidden; // 新建一个BFC,解决margin在垂直方向上折叠的问题 animation: move 1s infinite linear; p { height: calc(100% - 2px); margin: 1px; background-color: #fff; } } @keyframes move { from { background-position: -1px; } to { background-position: -12px; } }
linear-gradient&&background
Draw a dotted line through linear gradient and background-size, and then move it to the four sides through background-position. The good thing about this method is that you can set the styles of the four sides and the direction of the animation respectively. Careful students will find that the animation in the previous method is not clockwise or counterclockwise.
.box { width: 100px; height: 100px; background: linear-gradient(0deg, transparent 6px, #e60a0a 6px) repeat-y, linear-gradient(0deg, transparent 50%, #0f0ae8 0) repeat-y, linear-gradient(90deg, transparent 50%, #09f32f 0) repeat-x, linear-gradient(90deg, transparent 50%, #fad648 0) repeat-x; background-size: 1px 12px, 1px 12px, 12px 1px, 12px 1px; background-position: 0 0, 100% 0, 0 0, 0 100%; animation: move2 1s infinite linear; p { margin: 1px; } } @keyframes move2 { from { } to { background-position: 0 -12px, 100% 12px, 12px 0, -12px 100%; } }
linear-gradient&&mask
The mask attribute specification has been included in the list of candidate recommended specifications. It will be said that it is a certainty that it will enter the established specification standards in the future. You can rest assured to learn it and it will be useful in the future.
Here you can also use mask to achieve the same animation, and you can achieve the effect of dotted border gradient color. The difference from background is that mask needs to add an opaque mask in the middle, otherwise the content of the p element will be covered.
.box { width: 100px; height: 100px; background: linear-gradient(0deg, #f0e, #fe0); -webkit-mask: linear-gradient(0deg, transparent 6px, #e60a0a 6px) repeat-y, linear-gradient(0deg, transparent 50%, #0f0ae8 0) repeat-y, linear-gradient(90deg, transparent 50%, #09f32f 0) repeat-x, linear-gradient(90deg, transparent 50%, #fad648 0) repeat-x, linear-gradient(0deg, #fff, #fff) no-repeat; // 这里不透明颜色随便写哦 -webkit-mask-size: 1px 12px, 1px 12px, 12px 1px, 12px 1px, 98px 98px; -webkit-mask-position: 0 0, 100% 0, 0 0, 0 100%, 1px 1px; overflow: hidden; animation: move3 1s infinite linear; p { height: calc(100% - 2px); margin: 1px; background-color: #fff; } } @keyframes move3 { from { } to { -webkit-mask-position: 0 -12px, 100% 12px, 12px 0, -12px 100%, 1px 1px; } }
Recommended tutorial: css quick start
The above is the detailed content of How to achieve dotted border scrolling effect in css. For more information, please follow other related articles on the PHP Chinese website!