简体   繁体   English

Jquery在mouseenter上进行动画制作

[英]Jquery animate on mouseenter

i'm not sure what is wrong with this script but it just doesn't seem to want to work. 我不确定这个脚本有什么问题,但它似乎并不想工作。 What is meant to happen is that the box should fade the color in and out according to mouseenter/leave. 意味着盒子应该根据mouseenter / leave来淡入和淡出颜色。

$(document).ready(function() {
    $('.square-box').mouseenter(function() {
        $(this).animate({
            background-color: '#AAAAAA'
        }, 1000);
    });
    $('.square-box').mouseleave(function() {
        $(this).animate({
            background-color: '#CCCCCC'
        }, 1000);
    });
});

https://jsfiddle.net/k9met5oz/ https://jsfiddle.net/k9met5oz/

What am i doing wrong? 我究竟做错了什么? Thanks. 谢谢。

You can animate only attributes with numeric values. 您只能使用数值为属性设置动画。 But you can use this plugin https://github.com/jquery/jquery-color/ for color animation. 但您可以使用此插件https://github.com/jquery/jquery-color/进行颜色动画。 Try this: 尝试这个:

 $(document).ready(function() { $('.square-box').mouseenter(function() { $(this).animate({ "backgroundColor": '#AAAAAA' }, 1000); }); $('.square-box').mouseleave(function() { $(this).animate({ "backgroundColor": '#CCCCCC' }, 1000); }); }); 
 .square-box { width: 200px; height: 200px; line-height: 200px; cursor: pointer; background-color: #CCC; text-align: center; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-color/2.1.2/jquery.color.js"></script> <div class="square-box">TEST BOX</div> 

Seems like you cannot animate colors by default in jquery 看起来你不能在jquery中默认为颜色设置动画

You can do it with changing css properties. 你可以通过改变css属性来做到这一点。

 $(document).ready(function() { $('.square-box').mouseenter(function() { $(this).css({ "background-color": "#aaa" }); }).mouseleave(function() { $(this).css({ 'background-color': '#CCCCCC' }) }); }); 
 .square-box { width: 200px; height: 200px; line-height: 200px; cursor: pointer; background-color: #CCC; text-align: center; transition: 1s all; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class='square-box'> TEST BOX </div> 

Jquery cannot animate colors by default. 默认情况下,Jquery无法为颜色设置动画。 But you don't need a plugin. 但是你不需要插件。 You can do this with CSS transitions much more easily: 您可以更轻松地使用CSS转换执行此操作:

 .square-box { width: 200px; height: 200px; line-height: 200px; cursor: pointer; background-color: #CCC; text-align: center; -webkit-transition:background 1s; -moz-transition:background 1s; -o-transition:background 1s; transition:background 1s } .square-box:hover {background:#AAAAAA} 
 <div class='square-box'> TEST BOX </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM