如果CSS3过渡不存在,使用Modernizr和jQuery来动画

Jac*_*Dev 5 javascript jquery css3 transitions

如果不支持CSS3,有没有办法使用Modernizr和jQuery的组合来启用类似于转换的东西?我现在正在做的就是这样......

<div class="hoverable">
 <p>This div changes both width and height on hover</p>
</div>
Run Code Online (Sandbox Code Playgroud)

CSS是

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

.hoverable:hover {
 height: 200px;
 width: 100px;
}
Run Code Online (Sandbox Code Playgroud)

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

Jac*_*Dev 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>
Run Code Online (Sandbox Code Playgroud)

这非常有效.CSS只在新浏览器中动画(因为这是它唯一的地方),而JS只在旧浏览器中动画.如果使用这个脚本,你可以从Modernizr的http://www.modernizr.com/从和jQuery http://www.jquery.com/(当然).