JQuery Animation

This is a simple slide animation. The problem is that when I change the "top" parameter of the myDiv class, the animation does not work correctly, instead of sliding up, it slides from above. It only works incorrectly when I click the button for the first time. When I change the myDiv top parameter to a larger number, there are no more problems. Could you please help me find what is wrong with the code.

   <style>
    .box {
        position:relative;
        width: 200px;
        height: 500px;
    }
    .myDiv {
        position: absolute;
        width:100%;
        overflow: hidden;
        bottom:0px;
        top:10px;
        left:500;
    }
</style>
<script>
    var swt = 0;
    $(document).ready(function () {
        $(".b1").click(function () {

            var div = $(".myDiv");
            if (swt == 0) {
                div.animate({
                    top: '300px',
                    opacity: '1'
                }, "slow");
                //  div.animate({height:'300px', opacity:'1'},"slow");
                swt++;
            } else {
                div.animate({
                    top: '500px',
                    opacity: '1'
                }, "slow");
                //  div.animate({height:'0px', opacity:'1'},"slow");
                swt--;
            }
        });
    });
</script>
</head>
<body>
    <button class="b1">Start Animation</button>
    <p>posds</p>
    <div class="box">
        <div class="myDiv" style="background:#7549B1; width:200px;"></div>
    </div>
</body>

</html>

      

+3


source to share


3 answers


I'm not sure what you want, but you can change the value bottom

instead top

:

if(swt==0){
    div.animate({bottom:'500px', opacity:'1'}, "slow");
    swt++;  
} else {
    div.animate({bottom:'300px', opacity:'1'}, "slow");
    swt--;
}

      



http://jsbin.com/ixigaf/1/edit

+3


source


Do not use bottom

, and top

for the same item, it will create a conflict. Determine the height and top of the animation with a negative value:

Here is a jsFiddle.



if(swt==0){
       div.animate({top:'-300px', opacity:'1'}, "slow");
        swt++;  
}else{
      div.animate({top:'-500px', opacity:'1'}, "slow");
        swt--;
}

      

+1


source


http://jsfiddle.net/Cyberek/yLBeK/

This is buggy as I understand it, but if you change the css value at the top from 10px to 100px (the same as the top ones in js) everything should be fine.

.myDiv {
        position: absolute;
        width:100px;
        height: 100px;
        overflow: hidden;
        top:100px;
        left:100px;
        background: red;
}

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

            var div = $(".myDiv");
            if (swt == 0) {
                div.animate({
                    top: '100px',
                    opacity: '1'
                }, "slow");
                //  div.animate({height:'300px', opacity:'1'},"slow");
                swt++;
            } else {
                div.animate({
                    top: '200px',
                    opacity: '1'
                }, "slow");
                //  div.animate({height:'0px', opacity:'1'},"slow");
                swt--;
            }
        });

      

0


source







All Articles