简体   繁体   English

jQuery将活动水平菜单项滚动到中心

[英]jQuery to scroll active horizontal menu item to the center

I am working on a responsive sub menu that on small screens will be scrollable horizontally instead of stacking the links. 我正在一个响应式子菜单上工作,该菜单在小屏幕上可以水平滚动而不是堆叠链接。 I can make that work. 我可以做的。 What I would like to do is center the active menu item on the current page. 我想做的是将活动菜单项放在当前页面的中心。 Im using wordress, so the class is already there .current-menu-item . 我正在使用wordress,所以该类已经在.current-menu-item I have been attempting to use jQuery to calculate the center and scroll the menu item on page load, and it works in my jsfiddle, but not on an active website. 我一直在尝试使用jQuery计算中心并在页面加载时滚动菜单项,它在我的jsfiddle中有效,但在活动的网站上不起作用。 I'm not certain that I am using the correct approach but here is what I am currently doing: 我不确定我使用的是正确的方法,但是这是我目前正在做的事情:

<div id="ds-sub-menu">
  <ul id="menu-new-here" class="menu">
    <li><a href="#">Link 1</a></li>
    <li><a href="#">Link 2</a></li>
    <li class="current-menu-item"><a href="#">Link 3</a></li>
    <li ><a href="#">Link 4</a></li>
    <li><a href="#">Link 5</a></li>
    <li><a href="#">Link 6</a></li>
  </ul>
</div>

    jQuery(document).ready(function($) {

  var totalWidth = $("#ds-sub-menu").outerWidth()

  $('#ds-sub-menu ul').css('width', totalWidth);

    var myScrollPos = $('#ds-sub-menu .current-menu-item').offset().left + $('#ds-sub-menu .current-menu-item').outerWidth(true) / 2 +
      $('#ds-sub-menu .menu').scrollLeft() - $('#ds-sub-menu .menu').width() / 2;

    $('#ds-sub-menu .menu').scrollLeft(myScrollPos);
});

#ds-sub-menu {
  width: 100%;
  max-width: 300px;
  background: #000;
  overflow: hidden;
}

#ds-sub-menu ul {
  overflow-y: hidden;
  overflow-x: scroll;
  white-space: nowrap;
  -webkit-overflow-scrolling: touch;
}

#ds-sub-menu li {
  display: inline-block;
  padding: 10px 14px;
  text-align: -webkit-match-parent;
}

#ds-sub-menu li a {
  color: #ccc;
}

#ds-sub-menu li.active a {
  color: #fff;
}

My jsfiddle is a here 我的jsfiddle 在这里

Ok, sorry for wasting everyone's time. 好的,很抱歉浪费大家的时间。 This was a CSS issue. 这是一个CSS问题。

In my fiddle, I set a max-width of 300px for the primary container so it would mimic a small device. 在我的小提琴中,我为主要容器设置了300px的最大宽度,以便它模仿一个小型设备。 I also did this in my website css, and it conflicted with the jQuery .outerWidth() function because that was returning as 300px as well. 我也在我的网站CSS中执行了此操作,它与jQuery .outerWidth()函数冲突,因为它也返回了300px。 I simply removed the max-width from the container element and now it works perfectly. 我只是从容器元素中删除了最大宽度,现在它可以完美地工作了。

So full website code if anyone else ever wants to do something like this: 如此完整的网站代码,如果有人想做这样的事情:

Wordpress Menu Sample WordPress菜单样本

<!--Mobile Version -->
<div id="ds-mobile-sub-menu" class="ds-sub-menu">
  <ul id="menu-new-here" class="menu">
    <li><a href="#">Link 1</a></li>
    <li><a href="#">Link 2</a></li>
    <li class="current-menu-item"><a href="#">Link 3</a></li>
    <li ><a href="#">Link 4</a></li>
    <li><a href="#">Link 5</a></li>
    <li><a href="#">Link 6</a></li>
  </ul>
</div>

<!-- Desktop Version -->    
<div id="ds-desktop-sub-menu" class="ds-sub-menu">
  <ul id="menu-new-here" class="menu">
    <li><a href="#">Link 1</a></li>
    <li><a href="#">Link 2</a></li>
    <li class="current-menu-item"><a href="#">Link 3</a></li>
    <li ><a href="#">Link 4</a></li>
    <li><a href="#">Link 5</a></li>
    <li><a href="#">Link 6</a></li>
  </ul>
</div>

jQuery jQuery的

   jQuery(document).ready(function ($) {    

    if( $('#ds-mobile-sub-menu').length ){ 
        // Dynamically set the width of #ds-sub-menu li by adding the widths of all the contained li's
        var totalWidth = $("#ds-mobile-sub-menu").outerWidth()

        $('#ds-mobile-sub-menu ul').css('width', totalWidth);

        var myScrollPos = $('#ds-mobile-sub-menu .current-menu-item').offset().left + $('#ds-mobile-sub-menu .current-menu-item').outerWidth(true) / 2 + $('#ds-mobile-sub-menu .menu').scrollLeft() - $('#ds-mobile-sub-menu .menu').width() / 2;

        $('#ds-mobile-sub-menu .menu').scrollLeft(myScrollPos);

        $(window).on("orientationchange",function(){
            var totalWidth = $("#ds-mobile-sub-menu").outerWidth()

            $('#ds-mobile-sub-menu ul').css('width', totalWidth);

            var myScrollPos = $('#ds-mobile-sub-menu .current-menu-item').offset().left + $('#ds-mobile-sub-menu .current-menu-item').outerWidth(true) / 2 + $('#ds-mobile-sub-menu .menu').scrollLeft() - $('#ds-mobile-sub-menu .menu').width() / 2;

            $('#ds-mobile-sub-menu .menu').scrollLeft(myScrollPos);

        });
    }

});

The CSS CSS

#ds-mobile-sub-menu {
    display: none;
}

.ds-sub-menu {
    background: #000;
    text-align: center;
    width: 100%;
}

.ds-sub-menu ul {
    text-align: center;
    padding: 20px 0;
    display: block;
}

.ds-sub-menu li {
    display: inline-block;
    padding: 0;
    position: relative;
}

.ds-sub-menu li a {
    color: #818181;
    font-family: Montserrat;
    font-weight: 400;
    text-transform: uppercase;
    padding: 18px 14px;
}

.ds-sub-menu li.current-menu-item a, .ds-sub-menu li a:hover {
    color: #fff;
}

Mobile Media Query This is a single sample of CSS to set the overflow to scroll and hide the scrollbar. 移动媒体查询这是CSS的单个示例,用于设置溢出来滚动和隐藏滚动条。

@media only screen and (max-width : 320px) {
    #ds-desktop-sub-menu { display: none; }
    #ds-mobile-sub-menu { display: block; }
    .ds-sub-menu ul { 
        overflow-y: hidden; 
        overflow-x: scroll; 
        white-space: nowrap; 
       -webkit-overflow-scrolling: touch; 
    }
}

Other Notes This doesn't come at perfectly centered, but is in the ballpark. 其他说明这并不是完全居中,而是在球场上。 There are probably some more tweaks that could really fine tune things. 可能还有其他一些调整可以真正调整一些东西。

UPDATE I have made some additional tweaks in the css to get the centering perfected. 更新我在css中做了一些其他调整,以使居中完美。 Here are the changes: 更改如下:

remove padding and margin from the list-item add padding to the anchor 从列表项中删除填充和边距将填充添加到锚点

UPDATE #2 Upon using this feature a bit more, I have made some additional tweaks to make it better handle responsiveness for different devices and eliminate an error I was getting. 更新#2在进一步使用此功能后,我进行了一些其他调整,以使其更好地处理不同设备的响应性并消除了我遇到的错误。

First: I was getting a jQuery error on pages that did not have the submenu. 第一:在没有子菜单的页面上出现jQuery错误。 So I added the following line to only run the function if the sub-menu does exist. 因此,我添加了以下行以仅在子菜单确实存在的情况下运行该功能。 if( $('#ds-mobile-sub-menu').length ){}

second: I noticed that if I rotated a mobile device, the function had already been run, so the calculated width no longer applied and the menu didn't look right. 第二:我注意到,如果我旋转移动设备,该功能已经运行,因此不再应用所计算的宽度,并且菜单看起来也不正确。 So I added a desktop class and a mobile class. 因此,我添加了一个桌面类和一个移动类。 The function actually only fires now on the mobile class and then I added the following function to recalibrate things on screen change: $(window).on("orientationchange",function(){}); 该函数实际上仅在移动类上触发,然后我添加了以下函数来重新校准屏幕更改: $(window).on("orientationchange",function(){});

Now when you rotate a mobile device, or change your desktop screen to a smaller format, the appropriate menu will be displayed and the function will calculate the size of the menu based on the screen size. 现在,当您旋转移动设备或将桌面屏幕更改为较小的格式时,将显示适当的菜单,并且该功能将根据屏幕大小计算菜单的大小。

I have updated the code to reflect these changes. 我已经更新了代码以反映这些更改。

This line seems wrong: 这行似乎是错误的:

var myScrollPos = $('li.active').offset().left + $('li.active').outerWidth(true) / 2 + $('.menu').scrollLeft() - $('.menu').width() / 2;

There isn't any li.active . 没有任何li.active Should be then .current-menu-item : 然后应该是.current-menu-item

var myScrollPos = $('.current-menu-item').offset().left + $('.current-menu-item').outerWidth(true) / 2 + $('.menu').scrollLeft() - $('.menu').width() / 2;

Fiddle https://jsfiddle.net/k0q2rh97/2/ 小提琴https://jsfiddle.net/k0q2rh97/2/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM