Проблема позиционирования CSS - PullRequest
0 голосов
/ 14 июля 2011

У меня есть эта функция javascript, которая вызывается после ответа ajax.Он содержит этот бит кода:

// If there is a notification already, clear it and restart with the new notification.
try {clearTimeout(timeout_handle);}
catch(err){}

$('#animated_status_message').html(
    'this is a status message'
);
$("#animated_status_message").animate({top:"0"},{duration:400})
timeout_handle = setTimeout("$('#animated_status_message').animate({top:'-60px'},{duration:400})", 4000);

Это CSS для div:

#animated_status_message{
    background-color: orange;
    color: black;
    font-weight: bold;
    border: 1px solid black;
    margin-top: 2px;
    padding: 20px;
    z-index: 1000;
    max-width: 80%;
    position:fixed;
    opacity:0.92;
}

И, наконец, html:

<div id="animated_status_message" style="top: -60px;"></div>

JavaScriptвероятно, здесь нет необходимости, но на всякий случай.

Итак, проблема в том, что div не центрирован.Я попробовал несколько методов с помощью margin auto и создал родительский div и добавил к нему свойство left и фактический div, содержащий содержимое.

Есть идеи, как связать все это вместе?

Спасибо, Эрик

Ответы [ 3 ]

1 голос
/ 14 июля 2011

С position:fixed; margin: 0 auto; не будет работать. Я бы установил фиксированную (или процентную) ширину, как предложил Тобиас.

1 голос
/ 14 июля 2011

Если вы знаете размер div, вы можете попробовать это:

#animated_status_message{
   left: 50%;
   width: 400px (just an example);
   margin-left: -200px (the width of the div divided by two);
}
0 голосов
/ 14 июля 2011

Попробуйте это

// If there is a notification already, clear it and restart with the new notification.
try {clearTimeout(timeout_handle);}
catch(err){}

$('#animated_status_message').html(
    'this is a status message'
);
$("#animated_status_message").animate({top:"0px"}, 400);
timeout_handle = setTimeout(function(){
 $('#animated_status_message').animate({top:'-60px'}, 400)
}, 4000);
...