Javascript 如果CSS3转换不';不存在

Javascript 如果CSS3转换不';不存在,javascript,jquery,css,transitions,Javascript,Jquery,Css,Transitions,如果不支持CSS3,有没有一种方法可以结合使用Modernizer和jQuery来实现类似于转换的功能?我现在做的是这样的 <div class="hoverable"> <p>This div changes both width and height on hover</p> </div> 如果CSS3转换不受支持,我目前正在使用Modernizer使div默认处于悬停状态。如果不支持CSS3,有没有办法使用Modernizer触发jQue

如果不支持CSS3,有没有一种方法可以结合使用Modernizer和jQuery来实现类似于转换的功能?我现在做的是这样的

<div class="hoverable">
 <p>This div changes both width and height on hover</p>
</div>
如果CSS3转换不受支持,我目前正在使用Modernizer使div默认处于悬停状态。如果不支持CSS3,有没有办法使用Modernizer触发jQuery动画?如果不是jQuery,那么我也可以使用自定义动画,但我真的不知道怎么做。

您可以使用:

if(!Modernizr.csstransitions) { // Test if CSS transitions are supported $('#myDiv').bind({ mouseenter: function() { $(this).animate({ width: 1000, height: 1000 }, 1000); }, mouseleave: function() { $(this).animate({ width: 100, height: 100 }, 1000); } }); } 如果(!modernizer.cstranitions){//测试是否支持CSS转换 $('#myDiv').bind({ mouseenter:function(){ $(此)。设置动画({ 宽度:1000, 身高:1000 }, 1000); }, mouseleave:function(){ $(此)。设置动画({ 宽度:100, 身高:100 }, 1000); } }); }
我自己解决了这个问题。我是这样做的

<!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>

Modernizer+jQuery测试
如果(!modernizer.cstranitions){//测试是否支持CSS转换
$(函数(){
$('#js')。悬停(函数(){
$(this.animate({width:'50px',height:'50px'},{queue:false,duration:500});
},函数(){
$(this.animate({width:'100px',height:'100px'},{queue:false,duration:500});
});
});
}
身体{
保证金:0;
填充:0;
}
div{
边框:1px纯红;
高度:100px;
保证金:25像素自动;
宽度:100px;
}
#css{
过渡:高度.5s,宽度.5s;
-khtml过渡:高度.5s,宽度.5s;
-moz过渡:高度.5s,宽度.5s;
-o型过渡:高度.5s,宽度.5s;
-webkit过渡:高度.5s,宽度.5s;
}
#css:悬停{
高度:50px;
宽度:50px;
}
JS
CSS

这非常有效。CSS one只在新浏览器中设置动画(因为这是它唯一可以设置动画的地方),而JS one只在旧浏览器中设置动画。如果您使用这个脚本,当然可以从中获得Modernizer和jQuery

您可以使用$.animate()复制效果,尽管有些效果可能非常慢(滞后)。。或者你可以在旧的浏览器中禁用一些效果,因为通常视觉效果并不那么重要,这正是我目前正在做的。(我的意思是禁用效果)看起来很有效。我会在一个小时左右试一下。试过了,但没能成功。我应该把它放在
以外的地方吗?你能把你做的不同的地方贴出来让我们知道吗?
<!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>