SVG был бы более подходящим здесь.Просто настройте значение ширины / высоты и свойства хода:
svg {
width: 200px;
height: 100px;
margin: 10px;
overflow: visible;
}
svg>rect {
stroke:purple;
fill:transparent;
stroke-dasharray: 130, 150;
animation: rotate 2s linear infinite;
}
@keyframes rotate {
to {
stroke-dashoffset: 280;
}
}
<svg xmlns="http://www.w3.org/2000/svg">
<rect x="5" y="5" height="100%" width="100%" rx="20" ry="20" stroke-width="10"/>
</svg>
Но это также можно сделать с помощью CSS.Вот идея без прозрачности, где хитрость заключается в том, чтобы вращать элемент, который будет покрывать только границы, и где мы будем применять clip-path
:
.box {
width:200px;
height:100px;
overflow:hidden;
border-radius:20px;
position:relative;
z-index:0;
}
.box:after {
content:"";
position:absolute;
top:10px;
left:10px;
right:10px;
bottom:10px;
background:#fff;
border-radius:10px;
}
.box:before{
content:"";
position:absolute;
top:-50%;
left:-50%;
right:-50%;
bottom:-50%;
background:blue;
clip-path:polygon(0 0,0 100%,100% 0, 100% 100%);
animation:rotate 1s linear infinite;
}
@keyframes rotate {
50% {
transform:rotate(90deg) scaleX(0.5) ;
}
100% {
transform:rotate(180deg);
}
}
<div class="box">
</div>
Чтобы понять хитрость, удалите перелив и измените цвет:
.box {
width:200px;
height:100px;
border-radius:20px;
position:relative;
z-index:0;
border:1px solid;
margin:50px;
}
.box:after {
content:"";
position:absolute;
top:10px;
left:10px;
right:10px;
bottom:10px;
background:rgba(255,255,255,0.4);
border-radius:10px;
}
.box:before{
content:"";
position:absolute;
top:-50%;
left:-50%;
right:-50%;
bottom:-50%;
background:blue;
clip-path:polygon(0 0,0 100%,100% 0, 100% 100%);
animation:rotate 1s linear infinite;
}
@keyframes rotate {
50% {
transform:rotate(90deg) scaleX(0.5) ;
}
100% {
transform:rotate(180deg);
}
}
<div class="box">
</div>
Это также можно сделать без clip-path
и простого прямоугольника:
.box {
width:200px;
height:100px;
overflow:hidden;
border-radius:20px;
position:relative;
z-index:0;
}
.box:after {
content:"";
position:absolute;
top:10px;
left:10px;
right:10px;
bottom:10px;
background:#fff;
border-radius:10px;
}
.box:before{
content:"";
position:absolute;
top:-100%;
left:30%;
right:30%;
bottom:-100%;
background:blue;
animation:rotate 1s linear infinite;
}
@keyframes rotate {
50% {
transform:rotate(90deg) scaleX(0.5) ;
}
100% {
transform:rotate(180deg);
}
}
<div class="box">
</div>