代码之家  ›  专栏  ›  技术社区  ›  Christian

为什么jquery hover animate在本例中不起作用?

  •  0
  • Christian  · 技术社区  · 14 年前

    当我将鼠标悬停在盒子上时,它不会根据我的意图改变它的主叫方。发生了什么?

    <html>
    <head><title>test</title></head>
    <style type="text/css" >
    .box_type1{
        width:560px;
        height:560px;
    
        background-color:#b0c4de;
    }
    
    </style>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    
    <script type="text/javascript">
    
    $(document).ready(function(){
    $('.box_type1').hover(
        function () {
            $(this).stop().animate({backgroundColor:'#4E1402'}, 300);
            }, function () {
            $(this).stop().animate({backgroundColor:'#943D20'}, 100);
        });
    
        });
    </script>
    <body>
    
    </div>
    
        <div class="box_type1">
        </div>
    </body>
    </html>
    
    2 回复  |  直到 14 年前
        1
  •  2
  •   Nick Craver    14 年前

    你的代码有效, you can see here for a demo .

    你需要 jQuery color plugin 对于 jQuery UI 为此(要动画大多数非数字属性),例如使用来自cdn的ui(如果您对ui还有其他用途…如果您只需要这个动画,请使用颜色插件,要小得多):

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script>
    

    还有一个额外的 </div> 在你 <body> ,请确保删除它,无效的HTML会导致各种不可预测的行为,尤其是跨浏览器的行为:)

        2
  •  1
  •   Amy B    14 年前

    jquery不使用插件进行背景色动画: http://plugins.jquery.com/project/color

    另外,您的HTML是非常无效的,因此可能仍然会导致失败。

    更有效:

    DOCTYPE html>
    <html lang="en">
        <head>
            <title>test</title>
            <style type="text/css" >
            .box_type1{
                width:560px;
                height:560px;
                background-color:#b0c4de;
            }
            </style>
            <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
            <script type="text/javascript">
            $(document).ready(function(){
            $('.box_type1').hover(
                function () {
                    $(this).animate({backgroundColor: '#4E1402'}, 300);
                    }, function () {
                    $(this).animate({backgroundColor:'#943D20'}, 100);
                });
    
                });
        </script>
        </head>
        <body>
            <div class="box_type1">
            </div>
        </body>
    </html>