css如何利用实现虚线边框滚动效果

小灰灰 2020-04-26 07:02 1108阅读 0赞

效果图如下:

实现代码如下:

HTML

  1. <div class="box">
  2. <p>测试测试</p>
  3. </div>

Easy-way

通过背景图片实现。

  1. .box {
  2. width: 100px;
  3. height: 100px;
  4. position: relative;
  5. background: url(http://dandelioncloud.cn/images/1587884493731.gif);
  6. p {
  7. position: absolute;
  8. left: 0;
  9. top: 0;
  10. right: 0;
  11. bottom: 0;
  12. margin: auto;
  13. height: calc(100% - 2px);
  14. width: calc(100% - 2px);
  15. background-color: #fff;
  16. }
  17. }

repeating-linear-gradient

135度repeating线性渐变,p撑开高度,白色背景覆盖外层div渐变。

  1. .box {
  2. width: 100px;
  3. height: 100px;
  4. background: repeating-linear-gradient(
  5. 135deg,
  6. transparent,
  7. transparent 4px,
  8. #000 4px,
  9. #000 8px
  10. );
  11. overflow: hidden; // 新建一个BFC,解决margin在垂直方向上折叠的问题
  12. animation: move 1s infinite linear;
  13. p {
  14. height: calc(100% - 2px);
  15. margin: 1px;
  16. background-color: #fff;
  17. }
  18. }
  19. @keyframes move {
  20. from {
  21. background-position: -1px;
  22. }
  23. to {
  24. background-position: -12px;
  25. }
  26. }

linear-gradient&&background

通过线性渐变以及background-size画出虚线,然后再通过background-position将其移动到四边。这种方式比较好的地方在于可以分别设置四条边的样式以及动画的方向,细心的同学应该会发现上一种方式的动画并不是顺时针或者逆时针方向的。

  1. .box {
  2. width: 100px;
  3. height: 100px;
  4. background: linear-gradient(0deg, transparent 6px, #e60a0a 6px) repeat-y,
  5. linear-gradient(0deg, transparent 50%, #0f0ae8 0) repeat-y,
  6. linear-gradient(90deg, transparent 50%, #09f32f 0) repeat-x,
  7. linear-gradient(90deg, transparent 50%, #fad648 0) repeat-x;
  8. background-size: 1px 12px, 1px 12px, 12px 1px, 12px 1px;
  9. background-position: 0 0, 100% 0, 0 0, 0 100%;
  10. animation: move2 1s infinite linear;
  11. p {
  12. margin: 1px;
  13. }
  14. }
  15. @keyframes move2 {
  16. from {
  17. }
  18. to {
  19. background-position: 0 -12px, 100% 12px, 12px 0, -12px 100%;
  20. }
  21. }

repeating-linear-gradient

mask属性规范已经进入候选推荐规范之列,会说以后进入既定规范标准已经是板上钉钉的事情,大家可以放心学习,将来必有用处。

这里同样可以使用mask来实现相同的动画,并且可以实现虚线边框渐变色这种效果,与background不同的是mask需要在中间加上一块不透明的遮罩,不然p元素的内容会被遮盖住。

  1. .box {
  2. width: 100px;
  3. height: 100px;
  4. background: linear-gradient(0deg, #f0e, #fe0);
  5. -webkit-mask: linear-gradient(0deg, transparent 6px, #e60a0a 6px) repeat-y,
  6. linear-gradient(0deg, transparent 50%, #0f0ae8 0) repeat-y,
  7. linear-gradient(90deg, transparent 50%, #09f32f 0) repeat-x,
  8. linear-gradient(90deg, transparent 50%, #fad648 0) repeat-x,
  9. linear-gradient(0deg, #fff, #fff) no-repeat; // 这里不透明颜色随便写哦
  10. -webkit-mask-size: 1px 12px, 1px 12px, 12px 1px, 12px 1px, 98px 98px;
  11. -webkit-mask-position: 0 0, 100% 0, 0 0, 0 100%, 1px 1px;
  12. overflow: hidden;
  13. animation: move3 1s infinite linear;
  14. p {
  15. height: calc(100% - 2px);
  16. margin: 1px;
  17. background-color: #fff;
  18. }
  19. }
  20. @keyframes move3 {
  21. from {
  22. }
  23. to {
  24. -webkit-mask-position: 0 -12px, 100% 12px, 12px 0, -12px 100%, 1px 1px;
  25. }
  26. }

发表评论

表情:
评论列表 (有 0 条评论,1108人围观)

还没有评论,来说两句吧...

相关阅读