Javascript 当给定的值以百分比为单位时,动画功能以像素为单位设置宽度,而不是百分比

Javascript 当给定的值以百分比为单位时,动画功能以像素为单位设置宽度,而不是百分比,javascript,jquery,html,Javascript,Jquery,Html,我尝试使用jQuery的animate函数来增加元素的宽度,使用以下代码: $('.center-left').animate({width:'50%'}); 但它正在将div的值设置为50px 添加HTML代码 <!Doctype HTML> <html> <head> <link rel='stylesheet' type='text/css' href='style.css'> <scr

我尝试使用jQuery的animate函数来增加元素的宽度,使用以下代码:

$('.center-left').animate({width:'50%'});
但它正在将div的值设置为50px

添加HTML代码

    <!Doctype HTML>
<html>
    <head>
        <link rel='stylesheet' type='text/css' href='style.css'>
        <script src='jquery-1.7.js'></script>
        <script type='text/javascript' src='script.js'></script>
    </head>
    <body>
        <div class='header'>        
            <ul id='nav'>
                <li class='nav-2'><a href='#'>HOME</a></li>
                <li class='nav-1'><a href='#'>INFO</a> </li>
                <li class='nav-2'><a href='#'>COMPANY</a></li>
                <li class='nav-1'><a href='#'>CONTACTS</a></li>
                <li class='nav-2'><a href='#'>CLIENTS</a></li>
                <div style="clear:both"></div>
            </ul>

        </div>
        <div class='center'>
            <div id='center-left'>
                <div id='left-top'>
                    <span id='c'>
                        C
                    </span>
                    <span id='o'>
                        O
                    </span>
                    <span id='n'>
                        N
                    </span>
                </div>
                <div id='left-bottom'>
                    <span id='t'>
                        T
                    </span>
                    <span id='a'>
                        A
                    </span>
                    <span id='C'>
                        C
                    </span>
                    <span id='t'>
                        T
                    </span>
                </div>
            </div>
            <div id='center-right'>
            </div>
        </div>
        <div class='footer'>
            <span id='copyright'> Copyright &copy; 2010. All Rights Reserved</span>   
        </div>

    </body>

</html>
最初我将宽度设置为0

添加css

/* http://meyerweb.com/eric/tools/css/reset/ 
   v2.0 | 20110126
   License: none (public domain)
*/

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
    display: block;
}
body {
    line-height: 1;
}
ol, ul {
    list-style: none;
}
blockquote, q {
    quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
    content: '';
    content: none;
}
table {
    border-collapse: collapse;
    border-spacing: 0;
}
@font-face{
font-family:chaletlondon;
src:url('fonts/chalet-londonnineteenseventy.woff');
}
ul{

font-family:chaletlondon;
}
li{
display:inline-block;
float:left;
padding-left:150px;
}
a{
text-decoration:none;
color:black;
}
.header{
width:100%;
border:1px solid black;
height:100px;
}
#nav{
border:1px solid black;
height:100px;
width:100%;
display:inline-block;
margin:0px;
position:relative;
top:0%
}
.footer{
position:relative;
margin-left:auto;
margin-right:auto;
width:50%;
text-align:center;
bottom:0;
border:1px solid;
height:90px;
}
.center{
height:470px;
border:1px solid ;
margin:0;
bottom:3%;
width:100%;
}
#copyright{
display:inline-block;
position:relative;
top:80%;
border:1px solid ;
}
.nav-1,.nav-2{
position:relative;
bottom:10%;
opacity:0;
}
#center-left{
background-color:black;
height:100%;
width:0;
}
#left-top,#left-bottom{
height:50%;
border:1px solid white;
}
#c,#o,#n{
color:white;
width:32%;
height:100%;
font-size:343px;
display:inline-block;
border:1px solid white;
line-height:72%;
}
#C,#t,#a{
color:white;
width:24%;
height:100%;
font-size:332px;
display:inline-block;
border:1px solid white;
line-height:71%;
}
我从中找到了解决办法


这是jquery-1.7中的一个错误,它不支持以百分比表示的动画宽度!尽管版本1.6支持它,但在包含1.6 jquery之后,代码运行良好

你能在上复制这个问题吗?似乎对我很好。它在JSFIDLE中很好。@oGeez是的,我在JSFIDLE中也得到了同样的结果。很抱歉上面的评论,这意味着错误不在这段代码中。也许您的父元素恰好是100px宽度?
/* http://meyerweb.com/eric/tools/css/reset/ 
   v2.0 | 20110126
   License: none (public domain)
*/

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
    display: block;
}
body {
    line-height: 1;
}
ol, ul {
    list-style: none;
}
blockquote, q {
    quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
    content: '';
    content: none;
}
table {
    border-collapse: collapse;
    border-spacing: 0;
}
@font-face{
font-family:chaletlondon;
src:url('fonts/chalet-londonnineteenseventy.woff');
}
ul{

font-family:chaletlondon;
}
li{
display:inline-block;
float:left;
padding-left:150px;
}
a{
text-decoration:none;
color:black;
}
.header{
width:100%;
border:1px solid black;
height:100px;
}
#nav{
border:1px solid black;
height:100px;
width:100%;
display:inline-block;
margin:0px;
position:relative;
top:0%
}
.footer{
position:relative;
margin-left:auto;
margin-right:auto;
width:50%;
text-align:center;
bottom:0;
border:1px solid;
height:90px;
}
.center{
height:470px;
border:1px solid ;
margin:0;
bottom:3%;
width:100%;
}
#copyright{
display:inline-block;
position:relative;
top:80%;
border:1px solid ;
}
.nav-1,.nav-2{
position:relative;
bottom:10%;
opacity:0;
}
#center-left{
background-color:black;
height:100%;
width:0;
}
#left-top,#left-bottom{
height:50%;
border:1px solid white;
}
#c,#o,#n{
color:white;
width:32%;
height:100%;
font-size:343px;
display:inline-block;
border:1px solid white;
line-height:72%;
}
#C,#t,#a{
color:white;
width:24%;
height:100%;
font-size:332px;
display:inline-block;
border:1px solid white;
line-height:71%;
}