douchun1961 2013-09-28 22:28
浏览 96
已采纳

在打开(活动)移动下拉菜单中将标题的位置从固定更改为相对?

I have a responsive design that when viewed on a device less than 600px wide, uses drop down multi-level navigation. It is nested inside a header that is set to position: fixed. The problem is, that when you drop down the menu the menu overflows downward and does not scroll (because the header is fixed), so the tabs are not visible. Is there a way (using Javascript, jQuery or PHP) to dynamically set it so that when the drop down menu (by way of clicking on the hamburger menu icon in the top right or left), it changes the header's position from fixed to relative?

A good example would be www.dribbble.com. When it's less than 800px wide, the header is sticky (fixed), then when you click on the menu in the top left, it's obvious that the header's position automatically changes to relative.

HTML

    <header role="banner" class="secondary">
    <a href="#menu" class="menu-toggle"><em>Menu</em> <span aria-hidden="true"></span></a>
    <nav id="nav" role="navigation">
    <ul class="menu set">
    <li class="subnav">
    <a href="#">Link</a>
        <ul class="sub-menu">
            <li><a href="#">Link</a></li>
            <li><a href="#">Link</a></li>
            <li><a href="#">Link</a></li>
        </ul>
    </li>
    </ul>
    </nav>
    </header>

CSS

header[role="banner"] {
    width: 100%;
    background: #fff;
    display: block;
    margin: 0 auto;
    padding: 0;
    box-shadow: 0 2px 4px rgba(0,0,0,0.25);
    z-index: 10000; 
}

@media all and (min-width: 500px) {
    header[role="banner"] {
        position: fixed;
        top: 0;
        left: 0;
        clear: both;
        height: auto;
        display: block;
    }
}

@media all and (max-width: 499px) {
    header[role="banner"] {
        position: fixed;
    }
}

JavaScript

    $(document).ready(function() {

$('body').addClass('js');
      var $menu = $('#nav'),
          $menulink = $('.menu-toggle'),
          $menuTrigger = $('.subnav > a');

$menulink.click(function(e) {
        e.preventDefault();
        $menulink.toggleClass('active');
        $menu.toggleClass('active');
});

var add_toggle_links = function() {         
    if ($('.menu-toggle').is(":visible")){
        if ($(".toggle-link").length > 0){
        }
        else{
            $('.subnav > a').before('<span class="toggle-link">Open</span>');
            $('.toggle-link').click(function(e) {       
                var $this = $(this);
                $this.toggleClass('active').siblings('ul').toggleClass('active');
            }); 
        }
    }
    else{
        $('.toggle-link').empty();
    }
 }
add_toggle_links();
$(window).bind("resize", add_toggle_links); 

    });
  • 写回答

1条回答 默认 最新

  • dongling2038 2013-09-28 22:34
    关注

    Well, if you have a custom id or some other kind of attribute for a selector than you can change the position CSS property in multiple ways.

    CSS3

    @media all and (max-width: 800px) {
       header[role="banner"] {
          position: relative;
       }
    }
    

    jQuery

    $(document).ready(function() {
        $('.menu-toggle').click(function() {
           if ($( document ).width() < 499)
              $('header[role="banner"]').css('position', 'relative');
           else
              $('header[role="banner"]').css('position', 'fixed');
        });
    });
    

    Note: You edited your post. Did you realized that you used position: fixed; in both of your CSS @media rules? Change the second to position: relative;.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配