【问题标题】:Spinning Effect Anchor tag CSS3旋转效果锚标签 CSS3
【发布时间】:2014-10-22 10:00:51
【问题描述】:

伙计们!

我试图用 CSS3 创建一个旋转悬停效果。

刚刚做了一个圆圈旋转的效果。在此处查看 jsFiddle:http://jsfiddle.net/63yyeezn/26/

但是我现在想做的是创建一个旋转的东西,但这次是盒子类型

就像这张图片:

所以基本上我想要与上面显示的 jsFiddle 类似的效果,但是这次它必须是 box。

真的很难弄清楚这一点。这是我的 CSS:

    body {
    background: #292929;
    padding-left: 30px;
    font-size: 12px;
}


.twist {
    display: inline-block;
    font-size: 45px;
    line-height: 90px;
    cursor: pointer;
    margin: 20px;
    width: 90px;
    height: 90px;
    border-radius: 50%;
    text-align: center;
    position: relative;
    text-decoration: none;
    z-index: 1;
    color: #fff;
}
.twist:after {
    pointer-events: none;
    position: absolute;
    width: 100%;
    height: 100%;
    border-radius: 50%;
    content:'';
    -webkit-box-sizing: content-box;
    -moz-box-sizing: content-box;
    box-sizing: content-box;
}
.twist:before {
    speak: none;
    font-size: 48px;
    line-height: 90px;
    font-style: normal;
    font-weight: normal;
    font-variant: normal;
    text-transform: none;
    display: block;
    -webkit-font-smoothing: antialiased;
}



 .twist.demo-4 {
    width: 92px;
    height: 92px;
    box-shadow: 0 0 0 4px rgba(255, 255, 255, 1);
}

.twist.demo-4:before {
    line-height: 92px;
}
.twist.demo-4:after {
    top: -4px;
    left: -4px;
    padding: 0;
    z-index: 10;
    border: 4px dashed #fff;
}
.twist.demo-4:hover {
    box-shadow: 0 0 0 0 rgba(255, 255, 255, 0);
    color: #fff;
}
.twist.demo-4:hover i {
    color: #fff;
}

 .twist.demo-4.spin:hover {
    -webkit-transition: box-shadow 0.2s;
    -moz-transition: box-shadow 0.2s;
    transition: box-shadow 0.2s;
}
.twist.demo-4.spin:hover:after {
    -webkit-animation: spinAround 9s linear infinite;
    -moz-animation: spinAround 9s linear infinite;
    animation: spinAround 9s linear infinite;
}
@-webkit-keyframes spinAround {
    from {
        -webkit-transform: rotate(0deg)
    }
    to {
        -webkit-transform: rotate(360deg);
    }
}
@-moz-keyframes spinAround {
    from {
        -moz-transform: rotate(0deg)
    }
    to {
        -moz-transform: rotate(360deg);
    }
}
@keyframes spinAround {
    from {
        transform: rotate(0deg)
    }
    to {
        transform: rotate(360deg);
    }
}

希望你能帮助我处理一个 jsFiddle 文件。

谢谢!

【问题讨论】:

标签: html css


【解决方案1】:

我的回答并不完全适合您的示例,但您可能会感兴趣,因为它是一个完整的 CSS3 解决方案,没有 HTML 标记更改。动画不会是旋转,而是平移。

Webkit 版本

.bordered {
  overflow: hidden;
}
.bordered:before {
  content: '';
  position: absolute;
  top: 5px; /* 5px: border width */
  left: 5px;
  right: 5px;
  bottom: 5px;
  background: white;
  z-index: -1;
}
.bordered:after {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  right: -50%;
  bottom: -50%;
  background: black;
  z-index: -2;
}
.bordered:hover:after {
  background: -webkit-linear-gradient(left, white 50%, black 50%); /* black: border color*/
  background-size: 20px 100%; /* 20px: dash width */
  -webkit-animation: borderAnimated 1s linear infinite;
}

@-webkit-keyframes borderAnimated {
  from {
    transform: rotate(45deg) translate(0, 0);
  }
  to {
    transform: rotate(45deg) translate(20px, 0);
  }
}

/* --- Style only--- */
.bordered {
  width: 150px;
  height: 150px;
  line-height: 150px;
  text-align: center;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
<div class="bordered">Lorem ipsum</div>

诀窍是在:after 伪元素中使用剥离的背景,在:before 元素中使用假的空背景,这将用作掩码。悬停元素时,您只需为 :after 伪元素设置动画即可获得不错的效果。

【讨论】:

    【解决方案2】:

    致谢:@vsynz

    我认为仅使用静态边框是不可能的。这是一个替代解决方案:

    .rotating-dashed {
        position: relative;
        margin: 40px auto;
        width: 90px;
        height: 90px;
        overflow: hidden;
        color: #268;
    }
    .rotating-dashed .dashing {
        display: block;
        width: 100%;
        height: 100%;
        position: absolute;
    }
    .rotating-dashed .dashing:nth-of-type(2) {
        -webkit-transform: rotate(90deg);
    }
    .rotating-dashed .dashing:nth-of-type(3) {
        -webkit-transform: rotate(180deg);
    }
    .rotating-dashed .dashing:nth-of-type(4) {
        -webkit-transform: rotate(270deg);
    }
    .rotating-dashed .dashing i {
        display: block;
        position: absolute;
        left: 0;
        top: 0;
        width: 200%;
        border-bottom: 5px solid
    }
    .rotating-dashed strong {
        display: block;
        width: 105%;
        line-height: 90px;
        text-align: center;
        position: absolute;
    }
    .rotating-dashed:hover {
        cursor: pointer;
    }
    .rotating-dashed:hover .dashing i {
        -webkit-animation: slideDash 2.5s infinite linear;
        border-bottom: 5px dashed
    }
    @-webkit-keyframes slideDash {
        from {
            -webkit-transform: translateX(-50%);
        }
        to {
            -webkit-transform: translateX(0%);
        }
    }
    <div class="rotating-dashed"> <span class="dashing"><i></i></span>
     <span class="dashing"><i></i></span>
     <span class="dashing"><i></i></span>
     <span class="dashing"><i></i></span>
     <strong>Demo</strong>
    </div>

    【讨论】:

    • 感谢您的回答,但是当我将其悬停时,您向我展示的演示完全没有移动。此外,正常状态下必须有实线边框,悬停状态下必须有移动虚线边框。
    • 此演示仅适用于 IE。如果您尝试在 Chrome 中浏览它,则必须将动画属性更改为 webkit。我会为你准备一个例子。
    • 您应该提到您正在使用他在此评论中发布的@vsync 的解决方案:stackoverflow.com/questions/5261122/…
    • @Danield 实际上是它的修改版本:jsfiddle.net/desandro/zm7Et
    • @Danield 不能再同意了
    猜你喜欢
    • 2015-07-09
    • 1970-01-01
    • 2013-11-15
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 2012-05-27
    相关资源
    最近更新 更多