5

如果不支持 CSS3,有没有办法结合使用 Modernizr 和 jQuery 来启用类似于转换的功能?我目前正在做的事情是这样的......

<div class="hoverable">
 <p>This div changes both width and height on hover</p>
</div>

CSS是

.hoverable {
 height: 100px;
 width: 2000px;
 transition: height .5s, width .5s;
}

.hoverable:hover {
 height: 200px;
 width: 100px;
}

如果不支持 CSS3 过渡,我目前只是使用 Modernizr 使 div 默认处于悬停状态。如果不支持 CSS3,有没有办法使用 Modernizr 来触发 jQuery 动画?如果不是 jQuery,那么我也可以使用自定义动画,但我也不知道该怎么做。

4

2 回答 2

12

自己解决了这个问题。我的做法是这样的

<!doctype html>
<html>
    <head>
        <title>Modernizr + jQuery Testing</title>
        <script type="text/javascript" src="modernizr.js"></script>
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript">
        if(!Modernizr.csstransitions) { // Test if CSS transitions are supported
            $(function() {
                $('#js').hover(function(){
                    $(this).animate({width:'50px',height:'50px'},{queue:false,duration:500});
                }, function(){
                    $(this).animate({width:'100px',height:'100px'},{queue:false,duration:500});
                });
            });
        }
        </script>
        <style type="text/css">
            body {
                margin: 0;
                padding: 0;
            }
            div {
                border: 1px solid red;
                height: 100px;
                margin: 25px auto;
                width: 100px;
            }
            #css {
                transition: height .5s, width .5s;
                -khtml-transition: height .5s, width .5s;
                -moz-transition: height .5s, width .5s;
                -o-transition: height .5s, width .5s;
                -webkit-transition: height .5s, width .5s;
            }
            #css:hover {
                height: 50px;
                width: 50px;
            }
        </style>
    </head>

    <body>
        <div id="js">
            JS
        </div>
        <div id="css">
            CSS
        </div>
    </body>
</html>

那工作得很好。CSS 仅在新浏览器中动画(因为这是它唯一可以的地方),而 JS 仅在旧浏览器中动画。如果你使用这个脚本,你可以从 http://www.modernizr.com/ 获得modernizr和从http://www.jquery.com/获得jQuery (当然)。

于 2011-05-24T17:03:20.737 回答
4

您可以使用:

if(!Modernizr.csstransitions) { // 测试是否支持 CSS 过渡
    $('#myDiv').bind({
        鼠标输入:函数(){
            $(this).animate({
                宽度:1000,
                身高:1000
            }, 1000);
        },
        鼠标离开:函数(){
            $(this).animate({
                宽度:100,
                身高:100
            }, 1000);
        }
    });
}
于 2011-05-23T15:03:35.077 回答