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

时间:2014-07-16 12:46:47

标签: jquery css css3 css-transitions

我想在此网站中创建一个悬停效果:

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

将鼠标悬停在“更多”链接上。

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?

4 个答案:

答案 0 :(得分:8)

et voila!

通过使用伪元素和动画关键帧,您可以使用纯CSS实际上足够接近。好处是减少DOM混乱,没有JS和严格的关注点分离(坚持使用CSS进行样式化)。

请注意,以下示例适用于Chrome,根据其他浏览器(example for Chrome, FF)添加/删除-webkit-供应商前缀。

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 :(得分:1)

您可以使用此代码,我是从您的示例网站复制的。

Here is working jsFiddle.

<强> HTML:

<a href="#">
    <span class="text">Click the link</span>
    <span class="btnBefore"></span>
    <span class="btnAfter"></span>
</a>

<强>的CSS:     一个 {         向左飘浮;         border-radius:5px;         margin-top:10px;         位置:相对;         z-index:1;         溢出:隐藏;         最小宽度:47px;         显示:表;         填充:6px 9px;         border:none;         -webkit-transition:全部0.5秒缓出;         -moz-transition:全部0.5秒缓出;         -ms-transition:全部0.5s缓出;         -o-transition:全部0.5s缓出;         过渡:全部0.5秒缓解;         text-align:center;         颜色:#fff;         background-color:#E32831;         text-decoration:none;     }     a:悬停{         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;
}

答案 2 :(得分: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; }

答案 3 :(得分:0)

您可以将CSS伪元素与过渡属性一起使用。如果您想要更多关于边界动画,请使用SVG动画。我建议你看一下SVG动画。下面是关于纯CSS动画的边框和演示的SVG动画的好文章。

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

Border Animation Effect with SVG and CSS