响应式导航调整移动和桌面之间的问题

时间:2014-09-04 14:13:40

标签: javascript jquery css responsive-design nav

所以目前,我有两个菜单可以完全按照我的预期完成,但是,当我尝试将它们合并到一个响应式菜单中时,我想出了一些奇怪的东西,我以为我会引用StackOverflow社区。请参阅下面的代码和下面的两个JSFiddle链接,以查看导航示例:

桌面:http://jsfiddle.net/ckteb5np/5/

HTML BEGINS

<nav id="sub" class="clearfix">
  <div id="lefty">&lt;</div>
  <div class="container_element">
    <div class="inner_container">
      <a href="#"><div class="box">Estimate</div></a>
      <a href="#"><div class="box">About</div></a>
      <a href="#"><div class="box">Customer Information</div></a>
      <a href="#"><div class="box">Financing</div></a>
      <a href="#"><div class="box">Careers</div></a>
      <a href="#"><div class="box">Locate Us</div></a>
      <a href="#"><div class="box">Inspiration</div></a>
      <span class="clearfix"></span>
    </div>
  </div>
  <div id="righty">&gt;</div>
</nav>

HTML ENDS

DESKTOP CSS BEGINS

.container_element {
    max-width:1200px;
    margin:0 auto;
    text-align:center;
}

 .container_element .box {
    color: #fff;
    font-family:helvetica, sans-serif;
    display:block;
    float:left;
    padding:10px 2%;
}
.container_element .box:hover {
    background:#007FEB;
}
nav#sub {
    border-bottom:#00325a solid 3px;
    /* Old browsers */
    background: #004173;
    /* FF3.6+ */
    background: -moz-linear-gradient(top, #004173 0%, #014f8d 100%);
    /* Chrome,Safari4+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #004173), color-stop(100%, #014f8d));
    /* Chrome10+,Safari5.1+ */
    background: -webkit-linear-gradient(top, #004173 0%, #014f8d 100%);
    /* Opera 11.10+ */
    background: -o-linear-gradient(top, #004173 0%, #014f8d 100%);
    /* IE10+ */
    background: -ms-linear-gradient(top, #004173 0%, #014f8d 100%);
    /* W3C */
    background: linear-gradient(to bottom, #004173 0%, #014f8d 100%);
    /* IE6-9 */
    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#004173', endColorstr='#014f8d', GradientType=0);
    -webkit-box-shadow: 0 4px 6px 0 #BFBFBF;
    box-shadow: 0 4px 6px 0 #BFBFBF;
}
#sub a {
    color:#fff;
    font-size:10pt;
    text-decoration:none;
    font-weight:400;
}
#lefty, #righty {
    display:none;
}

.clearfix:before, .clearfix:after {
    content:" ";
    display: table;
}
.clearfix:after {
    clear: both;
}
/* * For IE 6/7 only * Include this rule to trigger hasLayout and contain floats. */
 .clearfix {
    *zoom: 1;
}

DESKTOP CSS ENDS

移动:http://jsfiddle.net/6qmg7m4u/1/ 桌面上的参考HTML

MOBILE CSS BEGINS

.box {
    width:400px;
    height:30px;
    line-height: 29px;
    padding:10px 0 0 0;
    color:#fff;
    float:left;
    text-decoration:none;
    text-align:center;
    font-family:helvetica, sans-serif;
}
.box:hover {
    background:#007FEB;
}
.container_element {
    white-space:nowrap;
    width:400px;
    margin-left: 10px;
    overflow-x:hidden;
    display:inline-block;
    overflow-y:hidden;
}
.inner_container {
    width:10000px;
}
#lefty, #righty {
    width: 35px;
    display: inline-block;
    height: 30px;
    text-align: center;
    color: #fff;
    line-height: 50px;
    cursor: pointer;
    font-size: 24px;
}
#lefty {
    float:left;
}
#righty {
    float:right;
}
nav#sub {
    background: #004173;
    background: linear-gradient(to bottom, #004173 0%, #014f8d 100%);
    background: -moz-linear-gradient(top, #004173 0%, #014f8d 100%);
    background: -ms-linear-gradient(top, #004173 0%, #014f8d 100%);
    background: -o-linear-gradient(top, #004173 0%, #014f8d 100%);
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #004173), color-stop(100%, #014f8d));
    background: -webkit-linear-gradient(top, #004173 0%, #014f8d 100%);
    border-bottom: #00325a solid 3px;
    box-shadow: 0 4px 6px 0 #BFBFBF;
    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#004173', endColorstr='#014f8d', GradientType=0);
    webkit-box-shadow: 0 4px 6px 0 #BFBFBF;
}

MOBILE CSS ENDS

MOBILE JS BEGINS

$(function () {
    var state = 0;
    var maxState = 6;
    var winWidth = $(window).width();
    $(window).resize(function () {
        winWidth = $(window).width();
        $('.box,.container_element').width(winWidth - 100);
        $('.container_element').scrollLeft((winWidth - 100) * state);
    }).trigger('resize');
    $('#lefty').click(function () {
        if (state == 0) {
            state = maxState;
        } else {
            state--;
        }
        $('.container_element').animate({
            scrollLeft: ((winWidth - 100) * state) + 'px'
        }, 800);
    });
    $('#righty').click(function () {
        if (state == maxState) {
            state = 0;
        } else {
            state++;
        }
        $('.container_element').animate({
            scrollLeft: ((winWidth - 100) * state) + 'px'
        }, 800);
    });
});

MOBILE JS ENDS

谢谢你让我挑选你的大脑!有时你需要一个不同的pov。

1 个答案:

答案 0 :(得分:0)

我不确定,但我认为您的问题在于您使用float:leftfloat:rightdisplay:block来水平对齐菜单? 为什么不将display:inline-blocka一起使用并删除里面的div?

另外:您没有使用css媒体查询来调整移动菜单,但不应该像这样需要javascript。您可以使用它来添加动画和滚动位置,但我相信它甚至可以使用css-animations。 (虽然除非你使用某种框架,否则可能仍然需要一些js。)

我似乎无法找到问题的确切位置,但删除尽可能多的JS并从浮动切换到内联块可能会有很大帮助。