jQuery - 在显示滑动面板时动画显示绝对定位div的"左"位置

Mat*_*att 7 jquery panel sliding jquery-animate

我在屏幕左侧有一个隐藏的面板,当屏幕左侧的"标签"点击时,它会滑入视图.我需要面板滑过现有页面内容的顶部,我需要选项卡随之移动.所以两者都绝对定位于CSS.一切都很好,除了我需要标签(以及标签容器)在面板显示时向左移动,所以它似乎粘在面板的右侧.使用浮动时它相对简单,但当然这会影响现有内容的布局,因此绝对定位.我已经尝试动画了面板容器的左侧位置(请参阅记录的jquery函数),但我无法使其工作.

这是我已更改的原始代码的示例,如何让按钮/标签滑动?

http://www.iamkreative.co.uk/jquery/slideout_div.html

我的HTML

<div><!--sample page content-->
    <p>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et 
    </p>
</div>

<div id="panel" class="height"> <!--the hidden panel -->
    <div class="content">
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore</p>
    </div>  
</div>

<!--if javascript is disabled use this link-->
<div id="tab-container" class="height">
    <a href="#" onclick="return()">
        <div id="tab"><!-- this will activate the panel. --></div> 
    </a>
</div>
Run Code Online (Sandbox Code Playgroud)

我的jQuery

$(document).ready(function(){

$("#panel, .content").hide(); //hides the panel and content from the user

$('#tab').toggle(function(){ //adding a toggle function to the #tab
    $('#panel').stop().animate({width:"400px", opacity:0.8}, 100, //sliding the #panel to 400px

    // THIS NEXT FUNCTION DOES NOT WORK -->
    function() {
        $('#tab-container').animate({left:"400px"} //400px to match the panel width
        });

    function() {
        $('.content').fadeIn('slow'); //slides the content into view.
        });  
},

function(){ //when the #tab is next cliked
    $('.content').fadeOut('slow', function() { //fade out the content 
        $('#panel').stop().animate({width:"0", opacity:0.1}, 500); //slide the #panel back to a width of 0
    });
   });

  });
Run Code Online (Sandbox Code Playgroud)

这就是css

#panel {
position:absolute;
left:0px;
top:50px;
background-color:#999999;
height:500px;
display:none;/*hide the panel if Javascript is not running*/
}

#panel .content {
width:290px;
margin-left:30px;
}

#tab-container{
position:absolute;
top:20px;
width:50px;
height:620px;
background:#161616;
}

#tab {
width:50px;
height:150px;
margin-top:100px;
display:block;
cursor:pointer;
background:#DDD;
}
Run Code Online (Sandbox Code Playgroud)

非常感谢

Mat*_*att 10

我从头开始,实际上读了jQuery docs hehe.我将面板和按钮放在一个绝对定位的div中,将它们向左悬浮.给容器一个负向左侧位置,然后在按钮上放置一个jQuery切换操作.

$('#button').toggle(function() {

    $('#slider').animate({
        left: '+=200'
        }, 458, 'swing', function() {

        // Animation complete. CALLBACK?

    });

}, function() {

    $('#slider').animate({
        left: '-=200'
        }, 458, 'swing', function() {

        // Animation complete. CALLBACK?

    });

});
Run Code Online (Sandbox Code Playgroud)