Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在重新调整响应布局的大小时操作jquery菜单_Javascript_Jquery_Html_Css_Responsive Design - Fatal编程技术网

Javascript 在重新调整响应布局的大小时操作jquery菜单

Javascript 在重新调整响应布局的大小时操作jquery菜单,javascript,jquery,html,css,responsive-design,Javascript,Jquery,Html,Css,Responsive Design,有简单的菜单和列表。ulli。LI的宽度和数量是动态的。在鼠标悬停/点击时,会有“更多”的下拉列表,它会显示剩余的LI,这将不适合可用空间 当用户从右向左调整窗口大小时,我尝试使用jquery,它将隐藏最后一个可见的菜单项。什么是可能的方式来做这个逆转,也增加了李在“更多”的链接 尝试了一些选项,当我们调整大小时,宽度变小,然后列表项移到下面,并增加UL的高度,所以使用这种方法,我能够隐藏最后可见的内容。 代码 步骤1 步骤2 步骤3 当用户重新调整大小(增加宽度)时,这些步骤将相反 加价

有简单的菜单和列表。ulli。LI的宽度和数量是动态的。在鼠标悬停/点击时,会有“更多”的下拉列表,它会显示剩余的LI,这将不适合可用空间

当用户从右向左调整窗口大小时,我尝试使用jquery,它将隐藏最后一个可见的菜单项。什么是可能的方式来做这个逆转,也增加了李在“更多”的链接

尝试了一些选项,当我们调整大小时,宽度变小,然后列表项移到下面,并增加UL的高度,所以使用这种方法,我能够隐藏最后可见的内容。 代码

步骤1

步骤2

步骤3

当用户重新调整大小(增加宽度)时,这些步骤将相反

加价

<div class="twelve columns filter-wrapper">
    <ul class="nav-bar-filter" id="nav-bar-filter">
        <li><a href="#">All</a></li>
        <li><a href="#">Small</a></li>
        <li><a href="#">Medium</a></li>
        <li><a href="#">Extra large</a></li>
        <li><a href="#">Text</a></li>
        <li><a href="#">Small-1</a></li>
        <li><a href="#">Medium-1</a></li>
        <li><a href="#">Extra large text</a></li>
        <li><a href="#">Large text</a></li>
        <li><a href="#">Text</a></li>
    </ul>
<ul id="more-nav">
  <li><a href="#">More > </a>
    <ul class="subfilter"><li><a href="#">Text</a></li></ul>
  </li>    
</ul>
</div>

基本上,此菜单将用于响应布局菜单。这方面的任何帮助都会很有帮助。
编辑1:添加标记

尝试以下操作:让jQuery检测
nav
栏中有多少术语需要截断。然后,使用
document.createElement()
JavaScript方法,根据截断的术语数量,将
li
元素添加到
ul
。例如,如果jQuery检测到5个术语被截断,则使用以下代码:

var truncated_elements = 5;
for (i=1; i<truncated_elements; i++){
    var new_li = document.createElement('li');
    new_li.innerHTML = "truncated_term";
    document.getElementsByTagName('ul')[0].appendChild(new_li);
}
var截断元素=5;

对于(i=1;i好吧,我已经尝试构建一些脚本来完成它,下面是我得到的:

$().ready(function () { 

    //we reconstruct menu on window.resize
    $(window).on("resize", function (e) {                                                   
        var parentWidth = $("#nav-bar-filter").parent().width() - 40;
        var ulWidth = $("#more-nav").outerWidth();                  
        var menuLi = $("#nav-bar-filter > li");                 
        var liForMoving = new Array();      
        //take all elements that can't fit parent width to array
        menuLi.each(function () {                       
            ulWidth += $(this).outerWidth(); 
            if (ulWidth > parentWidth) {
                console.log(ulWidth);
                liForMoving.push($(this));
            }
        });                         
        if (liForMoving.length > 0) {   //if have any in array -> move them to "more" ul
            e.preventDefault();                     
            liForMoving.forEach(function (item) {
                item.clone().appendTo(".subfilter");
                item.remove();
            });                         
        }
        else if (ulWidth < parentWidth) { //check if we can put some 'li' back to menu
            liForMoving = new Array();
            var moved = $(".subfilter > li");
            for (var i = moved.length - 1; i >= 0; i--) { //reverse order
                var tmpLi = $(moved[i]).clone();
                tmpLi.appendTo($("#nav-bar-filter"));
                ulWidth += $(moved[i]).outerWidth();
                if (ulWidth < parentWidth) {                                
                    $(moved[i]).remove();
                }
                else {
                    ulWidth -= $(moved[i]).outerWidth();
                    tmpLi.remove();
                }                           
            }                       
        }                       
        if ($(".subfilter > li").length > 0) { //if we have elements in extended menu - show it
            $("#more-nav").show();
        }
        else {
            $("#more-nav").hide();
        }
    });

    $(window).trigger("resize"); //call resize handler to build menu right
});
$().ready(函数(){
//我们在window.resize上重建菜单
$(窗口).on(“调整大小”,函数(e){
var parentWidth=$(“#导航栏过滤器”).parent().width()-40;
var ulWidth=$(“#更多导航”).outerWidth();
变量menuLi=$(“#导航条过滤器>li”);
var liForMoving=新数组();
//获取所有不能使父宽度适应数组的元素
menuLi.each(函数(){
ulWidth+=$(this).outerWidth();
if(ulWidth>parentWidth){
控制台.日志(ulWidth);
liForMoving.push($(this));
}
});                         
if(liForMoving.length>0){//if数组中有任何内容->将它们移动到“更多”ul
e、 预防默认值();
liForMoving.forEach(功能(项目){
item.clone().appendTo(“.subfilter”);
项。删除();
});                         
}
否则如果(ulWidthli”);
对于(var i=moved.length-1;i>=0;i--){//相反顺序
var tmpLi=$(移动[i]).clone();
tmpLi.appendTo($(“#导航条过滤器”);
ulWidth+=$(移动[i]).outerWidth();
if(ulWidthli”).length>0{//如果扩展菜单中有元素,则显示它
$(“#更多导航”).show();
}
否则{
$(“#更多导航”).hide();
}
});
$(window).trigger(“resize”);//调用resize处理程序以生成右菜单
});

必须更改css样式以使其正常工作。我们所做的是:

  • 获取父容器的宽度,主菜单的初始
    width
    为零,但我们可能必须显示附加菜单,因此我们也可以获取其大小
  • 窗口上。调整大小
    我们循环浏览主菜单中的所有元素(水平),累积
    ulWidth
    变量中的每个元素宽度
  • 一旦主菜单的
    width
    超过父容器的
    width
    ,我们需要将其余菜单项移动到子菜单(垂直)-因此我们将这些元素推到数组
    liForMoving
  • 如果数组
    liForMoving
    不是空的-我们克隆它的元素,将它们附加到子菜单中,并从主菜单中删除它们
  • 如果
    main menu
    中所有元素的
    width
    小于其容器的宽度,我们需要检查是否可以将一些元素从子菜单移动到主菜单
  • 我们迭代子菜单元素,抓取每个项目,附加到主菜单(如果菜单中有不同的字体和填充-元素的最终大小将不同),检查其大小是否适合父容器。如果-我们从子菜单中删除元素(
    $(moved[i])。remove()
    ),如果它不是
    ,则删除附加元素(
    tmpLi.remove()
  • 最后,我们检查子菜单是否有任何项,并显示/隐藏它
  • $(文档).ready(函数(){
    变量菜单=$(“#导航栏过滤器”),
    子菜单=$(“.subfilter”),
    更多=$(“更多资产净值”),
    父项=$(“.filter wrapper”),
    ww=$(窗口).width(),
    smw=more.outerWidth();
    菜单。子菜单(“li”)。每个(功能)(){
    var w=$(this.outerWidth();
    如果(w>smw)smw=w+20;
    返回smw
    });
    更多.css('width',smw);
    职能合同(){
    var w=0,
    outerWidth=parent.width()-smw-50;
    对于(i=0;i$(document).ready(function () {
        var menu = $("#nav-bar-filter"),
            subMenu = $(".subfilter"),
            more = $("#more-nav"),
            parent = $(".filter-wrapper"),
            ww = $(window).width(),
            smw = more.outerWidth();
    
        menu.children("li").each(function () {
            var w = $(this).outerWidth();
            if (w > smw) smw = w + 20;
            return smw
        });
        more.css('width', smw);
    
        function contract() {
            var w = 0,
                outerWidth = parent.width() - smw - 50;
            for (i = 0; i < menu.children("li").size(); i++) {
                w += menu.children("li").eq(i).outerWidth();
                if (w > outerWidth) {
                    menu.children("li").eq(i - 1).nextAll()
                        .detach()
                        .css('opacity', 0)
                        .prependTo(".subfilter")
                        .stop().animate({
                        'opacity': 1
                    }, 300);
                    break;
                }
            }
        }
    
        function expand() {
            var w = 0,
                outerWidth = parent.width() - smw - 20;
            menu.children("li").each(function () {
                w += $(this).outerWidth();
                return w;
            });
            for (i = 0; i < subMenu.children("li").size(); i++) {
                w += subMenu.children("li").eq(i).outerWidth();
                if (w > outerWidth) {
                    var a = 0;
                    while (a < i) {
                        subMenu.children("li").eq(a)
                            .css('opacity', 0)
                            .detach()
                            .appendTo("#nav-bar-filter")
                            .stop().animate({
                            'opacity': 1
                        }, 300);
                        a++;
                    }
                    break;
                }
            }
        }
        contract();
    
        $(window).on("resize", function (e) {
            ($(window).width() > ww) ? expand() : contract();
            ww = $(window).width();
        });
    
    });
    
    $().ready(function () { 
    
      var setWaiAria = function(menuLi){
        menuLi.each(function (i,el) {
          var $el = $(el);
          $el.attr('aria-setsize',menuLi.length);
          $el.attr('aria-posinset',i+1);
        });
      }
    
      // set wai aria aria-setsize and aria-posinset before cloning elements in other list
      setWaiAria($("#nav-bar-filter > li"));
    
      //we reconstruct menu on window.resize
      $(window).on("resize", function (e) {                         
        var parentWidth = $("#nav-bar-filter").parent().width() - 40;
        var ulWidth = $("#more-nav").outerWidth();          
        var menuLi = $("#nav-bar-filter > li");         
        var liForMoving = new Array();
        var activeElement = $(document.activeElement)[0];
    
        // before remove item check if you have to reset the focus
        var removeOriginal = function(item,clone){
          // check focused element
          if(item.find('a')[0] === activeElement){
            activeElement = clone.find('a')[0];
          }
    
          item.remove();
        }
    
        //take all elements that can't fit parent width to array
        menuLi.each(function () {
          var $el = $(this);            
          ulWidth += $el.outerWidth(); 
          if (ulWidth > parentWidth) {
            liForMoving.unshift($el);
          }
        }); 
    
        if (liForMoving.length > 0) { //if have any in array -> move em to "more" ul
          e.preventDefault(); 
    
          liForMoving.forEach(function (item) {
            var clone = item.clone();
            clone.prependTo(".subfilter");
    
            removeOriginal(item, clone);
          });
    
        }
        else if (ulWidth < parentWidth) { //check if we can put some 'li' back to menu
          liForMoving = new Array();
    
          var moved = $(".subfilter > li");
          for (var i=0, j = moved.length ; i < j; i++) { 
            var movedItem = $(moved[i]);
    
            var tmpLi = movedItem.clone();
            tmpLi.appendTo($("#nav-bar-filter"));
    
    
            ulWidth += movedItem.outerWidth();
            if (ulWidth < parentWidth) {
              removeOriginal(movedItem, tmpLi);
            }
            else {
              // dont move back
              ulWidth -= movedItem.outerWidth();
              tmpLi.remove();
            }
    
          }
        }           
        if ($(".subfilter > li").length > 0) { //if we have elements in extended menu - show it
          $("#more-nav").show();
        }
        else {
          // check if 'more' link has focus then set focus to last item in list
          if($('#more-nav').find('a')[0] === $(document.activeElement)[0]){
            activeElement = $("#nav-bar-filter > li:last-child a")[0];
          }
    
          $("#more-nav").hide();
        }
    
        // reset focus
        activeElement.focus();
      });
    
      $(window).trigger("resize"); //call resize handler to build menu right
    });