jQuery animate function - hide and stretch divs

删除回忆录丶 提交于 2019-12-12 01:47:51

问题


I have a page with divs as;

<div id="container">
    <div id="header"></div>
    <div id="sidebar">
        <div id="switch"></div>
        <div id="list"></div>
    </div>
    <div id="viewer"></div>
    <div id="footer"></div>
</div>

id switch is a trigger, which is meant to toggle animation. On 1st click, it should shrink the width of sidebar to 5px and width of list to 0px, and the margin-left of switch to 5px and the left of viewer to 5px. When user again click the trigger, everything may return to previous state and positions. In short i want to toggle hide/show sidebar. The animations should be some sliding effect towards left and then towards right.

You can have a look at what I am talking about here and an updated fiddle here.


回答1:


Per your updated code, you are setting the "left" of the #switch when you gave it a margin of 200px;

change,

$('#switch').animate({"left": 5});
$('#switch').animate({"left": 200});

to,

$('#switch').animate({"margin-left": 5});
$('#switch').animate({"margin-left": 200});



回答2:


Your code works fine, it is just your css that needs the fine tuning.

#switch {
    height: 35px;
    width: 35px;
    left: 200px;
    background-color: #CC0066;
    cursor: pointer;
    position: relative;
}

See the codes here: http://jsfiddle.net/EzAV2/8/



来源:https://stackoverflow.com/questions/5583909/jquery-animate-function-hide-and-stretch-divs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!