jquery - 如何用html,css,jquery制作类似Facebook Mobile侧边菜单的滑动菜单?

标签 jquery html css

我正在制作一个网站,网站左侧有一个垂直的按钮列表。我想将它们隐藏在侧面,所以只显示一个标签。当菜单隐藏时,我希望选项卡显示类似更多的内容,然后当菜单可见时,我希望选项卡显示隐藏。

所以我有几个问题...我怎样才能使菜单(本质上只是一个 div)在单击时从屏幕外滑出,同时更改选项卡上的文本和更改 href 目标这样当滑动功能完成时,tab会说隐藏,点击它时,它会将div送回屏幕之外。

如果你的手机上有 facebook 应用程序,我基本上想在我的桌面网站上复制它。

最佳答案

使用 jQuery 这非常简单。这些是您应该采取的步骤。

  1. 使用固定定位将弹出窗口固定在屏幕的一侧
  2. 添加用户触发滑入/滑出效果的可点击区域
  3. 创建一个 jQuery 动画以通过负边距打开/关闭内容
  4. 在动画回调中更改触发器的显示方式(显示/隐藏)

重要 - toggle event 在 jQuery 1.8 中被弃用并在 1.9 中被移除。我原来的答案将不再有效。这个新版本将适用于新旧版本的 jQuery。此方法使用点击处理程序和名为 hidden 的类来确定弹出窗口是否应在屏幕上/屏幕外动画显示。

http://jsfiddle.net/tzDjA/

jQuery

//when the trigger is clicked we check to see if the popout is currently hidden
//based on the hidden we choose the correct animation
$('#trigger').click( function() {
    if ($('#popout').hasClass('hidden')) {
        $('#popout').removeClass('hidden');
        showPopout();
    }
    else {
        $('#popout').addClass('hidden');
        hidePopout();
    }
});

function showPopout() {
    $('#popout').animate({
        left: 0
    }, 'slow', function () {
        $('#trigger span').html('Close');  //change the trigger text at end of animation
    });
}

function hidePopout() {
    $('#popout').animate({
        left: -40
    }, 'slow', function () {
        $('#trigger span').html('Show');  //change the trigger text at end of animation
    });
}

CSS

/* minimal CSS */
 #popout {
    position: fixed;                /* fix the popout to the left side of the screen */
    top: 50px;
    left: -40px;                    /* use a negative margin to pull the icon area of the popout off the edge of the page */
    width: 75px;
    border: 1px dotted gray;
    color: gray;
}
#trigger {                          /* create a clickable area that triggers the slide in/out effect */
    position: absolute;             /* position clickable area to consume entire right section of popout (add a border if you want to see for yourself) */  
    top: 0;
    bottom: 0;
    right: 0;
    cursor: pointer;   
}

原始答案(不适用于 jQuery 1.9)

http://jsfiddle.net/WMGXr/1/

$('#toggle').toggle( 
    function() {
        $('#popout').animate({ left: 0 }, 'slow', function() {
            $('#toggle').html('Close');
        });
    }, 
    function() {
        $('#popout').animate({ left: -40 }, 'slow', function() {
            $('#toggle').html('Show');
        });
    }
);

<div id="popout">
    <div id="toggle">Show</div>
    <br style="clear: both" />
    <ul>
        <li>a</li>
        <li>b</li>
        <li>c</li>
        <li>d</li>        
    </ul>
</div>

#popout { position: fixed; height: 100px; width: 75px; border: 1px dotted gray; background: darkblue; color: white; top:50px; left: -40px; }
#toggle { float: right; }

关于jquery - 如何用html,css,jquery制作类似Facebook Mobile侧边菜单的滑动菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8753024/

相关文章:

html - 为什么垂直对齐在这种情况下不起作用?

html - 删除导航项下拉列表之间的空格,默认行为无法正常工作

css——我从未见过的css编码

javascript - 使用 jquery 加载的页面的 CSS 属性继承性

javascript - 信用卡号码以 1 和 15 位数字开头

html - CSS - 这可以用 CSS 而不是 <table> 创建吗?

jquery - 如何在 joomla 模块中添加和使用 jquery 和 css 文件..?

jQuery 移动 : Changing button text when the button is clicked

javascript - 在 jQuery 中用 <p> 替换 <img>

javascript - javascript 如何找出 (i)frame 层次结构?