views:

71

answers:

3

So, I have two divs: #div1 and #div2. I want '#div2' to disappear when '#div1' has the CSS value: top = 0px.

Here is the CSS:

#div1 {
    top: 0px;
}
#div2 {
    display: block;
} 
if ( $('#div1').css('top') == '0px' ) {
    $("#div2").hide();
} else {
    $("div2").fadeIn();
}
$("div2").click(function(){
        $("#div1").animate({top:"+=315px"}, "slow");
});

The problem I am running into is that I'm changing that CSS value (for #div1) via Javascript and for this reason, my js doesn't acknowledge the change and doesn't make the div disappear (I think). Is there any way to make #div2 disappear when #div1's CSS property top = 0 and reappear whenever it is changed? Or is there a better way to implement this?

+2  A: 

rather than using this use mehtod .position()

<script>
var p = $("#div1");
var position = p.position();
alert( "left: " + position.left + ", top: " + position.top );
</script>

more detail about this : http://api.jquery.com/position/

Pranay Rana
Pranay,Thanks for your answer. The alert created using .position results in the values for left and top for #div1. However, when these values are changed programmatically using Javascript, the new values are not recorded. I need to change behavior based on these new values. The problem still is finding a way for my script to catch these changes and act upon them.
kevn
check out this link may help you to achieve your objective http://www.quirksmode.org/js/findpos.html
Pranay Rana
+1  A: 

Try this for click function:

$("div2").click(function(){

        $("#div1").parent().css({postion,"relative"});

        $("#div1").css({postion,"absolute"});

        $("#div1").animate({top:"+=315px"}, "slow");
});

in order to reflect proper positioning #div1 should have absolute position and parent of #div1 should have relative position.

M A Hossain Tonu
+1  A: 

Just use callbacks

#div1 {
    top: 0px;
}
#div2 {
    display: block;
}

$("div2").click(function(){
        $("#div1").animate({top:"+=315px"}, "slow", function(){
             if($('#div1').position().top < 1){
                  $('#div1').hide();
             }else{
                  $('#div1').fadeIn('slow');
             }
        });
});
José Manuel Lucas