jQuery marginTop(左)不起作用

时间:2015-10-21 14:43:01

标签: jquery

当您单击块时,我需要在父级的中心设置一个块#note。代码设置了边距,但是块没有到达中心。

$('#note').click( function(){
    $('#note').animate({
        position: 'absolute',
        height: '565px',
        width: '700px',
        left: '50%',
        top: '50%',
        marginTop: '-282.5px',
        marginLeft: '-350px',
    }, 1500);
});

1 个答案:

答案 0 :(得分:4)

您遇到语法错误:

$('#note').click( function(){
    $('#note').animate({
        position: 'absolute',
        height: '565px',
        width: '700px',
        left: '50%',
        top: '50%',
        marginTop: '-282.5px',
        marginLeft: '-350px', // Remove the last ,
        //------------------^ Remove this
    }, 1500);
});

正确的语法:

$('#note').click( function(){
    $('#note').animate({
        position: 'absolute',
        height: '565px',
        width: '700px',
        left: '50%',
        top: '50%',
        marginTop: '-282.5px',
        marginLeft: '-350px'
    }, 1500);
});