使用Jquery Animate突出显示边框颜色?

时间:2013-09-21 15:21:39

标签: javascript jquery html css animation

点击添加动画链接时,我想使用动画功能突出显示 文本字段的边框

像这样:

enter image description here

Js在这里小提琴: http://jsfiddle.net/N9um8/20/

HTML:

<div>   
     <p>
         <input type="text" name="search" id="search" placeholder="Search for people...." class="field_style">
     </p>            
     <p> <a href="javascript:void(0);" class="add_animation">Add Animation</a> </p>    
</div>

    <p class="styling"> I want it like this when click on Add Animation :</p>    

<div>   
     <p>
         <input type="text" name="test_search" id="test_search" placeholder="Search for people...." class="test_field_style">
     </p>     
</div> 

CSS:

.field_style { width:400px; }

.styling { color:red; }

.test_field_style {
     border-color: #6EA2DE; 
     box-shadow: 0 0 10px #6EA2DE;
     width:400px;  
}   

Jquery:

$(".add_animation").click(function (){

    $("#search").focus().animate({ "background-color": "#B6DADA" }, 800, "linear");     

});

3 个答案:

答案 0 :(得分:1)

默认情况下,

backgroundColor不是jQuery动画的动画属性。通常,它不能为颜色设置动画。使用简单的CSS转换会有更好的时间,尽管它们不受广泛支持(IE9-不会)。

.field_style {
    width:400px;
    transition: box-shadow .8s linear;
    outline: 0;
}
.field_style:focus {
    box-shadow: 0 0 10px #6EA2DE;
    border-color: #6EA2DE;
}

http://jsfiddle.net/N9um8/31/

顺便说一下,

.8s对于动画有点长。

答案 1 :(得分:1)

您有几种不同的选择。最好的表现肯定是CSS:

JS:

$(".add_animation").click(function (){

    $("#search").focus(function() {
        $(this).css({backgroundColor: '#B6DADA'});
    }).blur(function() {
        $(this).css({backgroundColor: '#fff'});
    }).focus();

});

CSS:

#search {
    -webkit-transition: .8s background linear;
    -moz-transition: .8s background linear;
    -ms-transition: .8s background linear;
    -o-transition: .8s background linear;
    transition: .8s background linear;
}

The fiddle

另一种方法是使用jQuery插件或其他东西(我最喜欢的名为jquery color)来为你的颜色设置动画。

$(".add_animation").click(function (){

    $("#search").focus(function() {
        $(this).animate({backgroundColor: '#B6DADA'}, 800, 'linear');
    }).blur(function() {
        $(this).animate({backgroundColor: '#fff'}, 800, 'linear');
    }).focus();

});

The fiddle

答案 2 :(得分:0)

尝试使用此插件:http://www.bitstorm.org/jquery/color-animation/并附加:

<script src="http://www.bitstorm.org/jquery/color-animation/jquery.animate-colors-min.js"></script>

这里有一个例子:

http://jsfiddle.net/N9um8/20/

相关问题