如何在鼠标悬停时创建动画边框

8

我希望创造一个悬停效果,就像这个网站:

http://themes.creiden.com/circleflip/blog-with-sidebar/

只需将鼠标悬停在“More”链接上即可。

http://joxi.ru/uXHGU_3JTJBkDpt35Iw

所以我尝试做了一些类似的东西,但我只有这个变体

http://jsfiddle.net/TY8CQ/

代码:

HTML

<a href="#">Click the link</a>

CSS

body{
    padding: 100px;
    background: #f6f6f6;
}


a{
    display: block;
    width: 200px;
    position: relative;
    background: #fff;
    height: 40px;
    font: 14px/40px Tahoma;
    color: #39adf0;
    text-decoration: none;
    -webkit-transition: 0.2s;
    margin: auto;
    text-align: center;
    position: relative;
}

a:after{
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 0;
    height: 1px;
    background: #39adf0;
    -webkit-transition: 0.2s;
    -moz-transition: 0.2s;
    -ms-transition: 0.2s;
    -o-transition: 0.2s;
    transition: 0.2s;

}

a:before{
    content: '';
    position: absolute;
    top: 0;
    right: 0;
    width: 1px;
    height: 0;
    background: #39adf0;
    -webkit-transition: 0.2s;
    -moz-transition: 0.2s;
    -ms-transition: 0.2s;
    -o-transition: 0.2s;
    transition: 0.2s;
}

a:hover:after{
    width: 100%;
}

a:hover:before{
    content: '';
    position: absolute;
    top: 0;
    right: 0;
    width: 1px;
    height: 100%;
    background: #39adf0;
    -webkit-transition: 0.2s;
    -moz-transition: 0.2s;
    -ms-transition: 0.2s;
    -o-transition: 0.2s;
    transition: 0.2s;
}

但在示例中(网站上),笔画按顺序出现并出现在所有边缘...我该怎么做?仅使用CSS3?也许使用jQuery?


1
根据动画的表现方式,我认为它很可能是 JavaScript。 - RevanProdigalKnight
伙计,这很简单,你可以从你提到的网站获取所有源代码。只是有点棘手,我不确定人们是否会帮助你“借用”别人的代码!无论如何,基本上你的想法是错误的,因为你想要复制的效果由4个元素组成而不是2个。使用你的网页检查工具/火狐调试工具来探究并查看CSS中“悬停”时会发生什么。 - Rob Schmuecker
你尝试过检查元素吗?链接内有3个span,一个带文本,两个带边框,通过转换进行变形。这比你的尝试更复杂,但所有人都可以分析它。 - wellagain
4个回答

8

et voila!

您可以使用伪元素和动画关键帧来近乎完美地使用纯CSS实现。好处是减少了DOM的混乱,没有JS以及严格的关注点分离(仅使用CSS进行样式设置)。

请注意,下面的示例在Chrome中有效,请根据其他浏览器添加/删除适当的-webkit-供应商前缀(Chrome,FF的示例)。

HTML

<a href='#'>hover!</a>

CSS

a {
    background:#E32831;
    padding:10px 15px;
    display:inline-block;
    box-sizing:border-box;
    position:relative;
    overflow:hidden;
    color:white;
    transition:all 200ms ease-in;
    border-radius:5px;
    font-family:arial;
    text-decoration:none;
    font-size:12px;
}
a:before, a:after {
    display:block;
    width:100%;
    content:'';
    box-sizing:border-box;
    position:absolute;
    height:0px;
    border-radius:5px;
}
a:before {
    border-top:1px solid red;
    border-right:1px solid red;
    left:-100%;
    top:0;
    height:0px;
}
a:after {
    border-bottom:1px solid red;
    border-left:1px solid red;
    left:100%;
    bottom:0;
    height:0px;
}
@-webkit-keyframes left-up {
    0% {
        left:100%;
        height:0;
    }
    50% {
        left:0;
        height:0;
    }
    100% {
        height:100%;
        left:0;
    }
}
@-webkit-keyframes right-dn {
    0% {
        left:-100%;
        height:0;
    }
    50% {
        left:0;
        height:0;
    }
    100% {
        height:100%;
        left:0;
    }
}
a:hover {
    background:lightgrey;
    color:#808080;
}
a:hover:after, a:hover:before {
    -webkit-animation-duration:900ms;
    -webkit-animation-iteration-count: 1;
    -webkit-animation-timing-function: ease-in-out;
    -webkit-animation-fill-mode: forwards
}
a:hover:after {
    -webkit-animation-name:left-up;
}
a:hover:before {
    -webkit-animation-name:right-dn;
}

1
不错的方法,只使用了一个HTML标签,我给你点赞! - Barlas Apaydin

2
您可以使用这段代码,我是从您的示例网站上复制的。 这里是可工作的 jsFiddle。 演示:

a {
    float: left;
    border-radius: 5px;
    margin-top: 10px;
    position: relative;
    z-index: 1;
    overflow: hidden;
    min-width: 47px;
    display: table;
    padding: 6px 9px;
    border: none;
    -webkit-transition: all 0.5s ease-out;
    -moz-transition: all 0.5s ease-out;
    -ms-transition: all 0.5s ease-out;
    -o-transition: all 0.5s ease-out;
    transition: all 0.5s ease-out;
    text-align: center;
    color: #fff;
    background-color: #E32831;
    text-decoration: none;
}
a:hover {
    background-color: #f1f1f1 !important;
}

span {
    position: relative;
    z-index: 1;
    line-height: 23px;
    transition: 0.3s ease;
    -webkit-transition: 0.3s ease;
    -moz-transition: 0.3s ease;
    -o-transition: 0.3s ease;
    -ms-transition: 0.3s ease;
}
a:hover span {
    color: #5a5a5a;
}
.btnBefore, .btnAfter {
    content: '';
    position: absolute;
    height: 0;
    width: 0;
    border: solid #e32831;
    border-width: 0;
    border-radius: 0;
    transition: 0;
    -webkit-transition: 0;
    -moz-transition: 0;
    -o-transition: 0;
    -ms-transition: 0;
    box-sizing: border-box;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    border-color: #E32831;
    border-radius: 5px;
}
    a .btnBefore {
    right: 0;
    bottom: 0;
}
a .btnAfter {
    left: 0;
    top: 0;
}
a:hover .btnBefore {
    border-width: 0 0 1px 1px;
}
a:hover .btnAfter {
    border-width: 1px 1px 0 0;
}

a:hover .btnAfter, a:hover .btnBefore {
height: 100%;
width: 100%;
transition: width 0.5s ease, height 0.5s ease 0.5s, border-top-right-radius 0.1s ease 0.4s, border-bottom-left-radius 0.1s ease 0.4s, border-bottom-right-radius 0.1s ease 0.9s, border-top-left-radius 0.1s ease 0.9s;
-webkit-transition: width 0.5s ease, height 0.5s ease 0.5s, border-top-right-radius 0.1s ease 0.4s, border-bottom-left-radius 0.1s ease 0.4s, border-bottom-right-radius 0.1s ease 0.9s, border-top-left-radius 0.1s ease 0.9s;
-moz-transition: width 0.5s ease, height 0.5s ease 0.5s, border-top-right-radius 0.1s ease 0.4s, border-bottom-left-radius 0.1s ease 0.4s, border-bottom-right-radius 0.1s ease 0.9s, border-top-left-radius 0.1s ease 0.9s;
-o-transition: width 0.5s ease, height 0.5s ease 0.5s, border-top-right-radius 0.1s ease 0.4s, border-bottom-left-radius 0.1s ease 0.4s, border-bottom-right-radius 0.1s ease 0.9s, border-top-left-radius 0.1s ease 0.9s;
-ms-transition: width 0.5s ease, height 0.5s ease 0.5s, border-top-right-radius 0.1s ease 0.4s, border-bottom-left-radius 0.1s ease 0.4s, border-bottom-right-radius 0.1s ease 0.9s, border-top-left-radius 0.1s ease 0.9s;
}
<a href="#">
    <span class="text">Click the link</span>
    <span class="btnBefore"></span>
    <span class="btnAfter"></span>
</a>


0

使用jQuery,我几乎可以完全重新创建这种行为:JSFiddle

$('a').append('<div class="borders" id="TL"></div><div class="borders" id="BL"></div><div class="borders" id="TR"></div><div class="borders" id="BR"></div>');

$('a').mouseover(function(){    
     $(this).stop().find('#TL, #BR').css("display","block").animate({width:"100%"},"fast", function(){ 
     $(this).siblings('#TR, #BL').css("display","block").animate({height:"100%"},"fast"); })}
).mouseout(function(){  
     $(this).find('#BL, #TR').animate({height:"0px"},"fast", function(){ 
     $(this).siblings('#BR, #TL').animate({width:"0px"},"fast")}
      ); 
 });

.borders { display: none; width: 1px; height: 1px; background: red; position: absolute; }
#TL { top: 0; left: 0; }
#BL { bottom: 0; left: 0; }
#TR { top: 0; right: 0; }
#BR { bottom: 0; right: 0; }

0

您可以使用带有过渡属性的 CSS 伪元素。如果您想了解更多关于边框动画,请使用 SVG 动画。我建议您查看一下 SVG 动画。以下是关于SVG边框动画的好文章和纯CSS动画演示。

<pre>http://jsfiddle.net/Mostwanted_cJ/F8uZE/<code>

使用SVG和CSS实现边框动画效果


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接