Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I stop a jquery animation based on CSS values?

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?

like image 584
kevn Avatar asked May 31 '10 18:05

kevn


2 Answers

rather than using this use method .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/

like image 187
Pranay Rana Avatar answered Sep 20 '22 02:09

Pranay Rana


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.

like image 36
M A Hossain Tonu Avatar answered Sep 21 '22 02:09

M A Hossain Tonu