using jQuery .animate to animate a div from right to left?

后端 未结 5 828
太阳男子
太阳男子 2020-12-04 19:23

I have a div absolutely positioned at top: 0px and right: 0px, and I would like to use jquery\'s .animate() to animate it from it\'s c

5条回答
  •  一个人的身影
    2020-12-04 19:44

    If you know the width of the child element you are animating, you can use and animate a margin offset as well. For example, this will animate from left:0 to right:0

    CSS:

    .parent{
    width:100%;
    position:relative;
    }
    
    #itemToMove{
    position:absolute;
    width:150px;
    right:100%;
    margin-right:-150px;
    }
    

    Javascript:

    $( "#itemToMove" ).animate({
    "margin-right": "0",
    "right": "0"
    }, 1000 );
    

提交回复
热议问题