简体   繁体   English

根据内容长度将选项卡或下拉菜单调整为相同的宽度

[英]Adapt tab or dropdown menu to the same width based on either content length

In the horizontal drop down menu using lists my problem is that the drop down menu and its main tab has different width based on its content length. 在使用列表的水平下拉菜单中,我的问题是下拉菜单及其主选项卡根据其内容长度具有不同的宽度。

see image attached: 看附图: 在此输入图像描述

What i need to be able to do is that content in the drop down has a long word or small word, the tab also has to adapt dynamically based on dropdown content length or vice versa. 我需要做的是下拉内容有一个长字或小字,标签也必须根据下拉内容长度动态调整,反之亦然。

在此输入图像描述

 $("li.dropdown a").hover(function () { $(this).parent().toggleClass('zclass'); }); $("li.dropdown:before").hover(function () { $(this).parent().toggleClass('zclass'); }); $(".dropdown-content").hover(function () { $(this).parent().toggleClass('zclass'); }); 
 #other-menu {width:850px; background-color:#eee;} #other-menu li{ list-style:none; float:left; padding:10px;} li a, .dropbtn { display: block; color: #828282; text-decoration: none; } li.dropdown { display: block; border-radius: 2px; -webkit-border-radius: 2px; -moz-border-radius: 2px; position:relative; } .dropdown-content { display: none; position: absolute; background-color: #fff; min-width: 100%; white-space: nowrap; box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); z-index: 999999; padding: 5px 7px; margin-top: 15px; margin-left: -9px; height: auto; top: 25px; font-family: 'Fira-Sans-Semibold', sans-serif; text-transform: uppercase; border-top: 1px solid #ccc; border-radius: 2px; -webkit-border-radius: 2px; -moz-border-radius: 2px;s } .dropdown > .dropdown-content a { padding: 3px 6px; text-decoration: none; text-align: left; font-size: 12px; letter-spacing: 1px; } .zclass { z-index: 200; } .dropdown > .dropdown-content a:hover,.dropdown > .dropdown-content a:focus { color:#5A5A5A !important; text-decoration: none;} .dropdown:hover:before { min-width: 100%; height: 50px; content:''; position:absolute; /* set styling box to be absolute position to not affect parent */ z-index:-1; /* set it behind the parent */ /*copy the properties the parent had*/ border-radius: 2px; -webkit-border-radius: 2px; -moz-border-radius: 2px; /* use positioning to grow its size in relation to parent */ top:-5px; bottom:-5px; left:0; right:-15px; box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); background: #fff; border: 1px solid #eeeeee; } .dropdown a:hover { text-decoration: none; color: #828282 !important; } .dropdown:hover .dropdown-content { display: block; } #other-menu { z-index: 2000; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul id="other-menu" class="hidden-xs"> <li><a href="#">Home</a></li> <li class="dropdown"> <a class="dropbtn" href="#">News</a> <div class="dropdown-content"> <a href="#">national</a> <a href="#">international</a> <a href="#">states</a> <a href="#">cities</a> </div> </li> <li class="dropdown"> <a class="dropbtn" href="#">opinion</a> <div class="dropdown-content"> <a href="#">cartoon </a> <a href="#">columns </a> <a href="#">editorial </a> <a href="#">interview </a> <a href="#">lead </a> <a href="#">letters </a> <a href="#">States </a> <a href="#">cities </a> <a href="#">National </a> <a href="#">International </a> <a href="#">States </a> <a href="#">cities </a> <a href="#">National </a> <a href="#">International </a> <a href="#">States </a> <a href="#">cities </a> </div> </li> <li class="dropdown"> <a class="dropbtn" href="#">States</a> <div class="dropdown-content"> <a href="#">Tamil Nadu</a> <a href="#">Andha</a> <a href="#">Kerala</a> <a href="#">Thiruvananthapuram</a> </div> </li> <li class="dropdown"> <a class="dropbtn" href="#">Cities</a> <div class="dropdown-content"> <a href="#">Tamil Nadu</a> <a href="#">Andha</a> <a href="#">Kerala</a> <a href="#">Karnataka</a> </div> </li> <li class="dropdown"> <a class="dropbtn" href="#">World</a> <div class="dropdown-content"> <a href="#">Tamil Nadu</a> <a href="#">Andha</a> <a href="#">Kerala</a> <a href="#">Karnataka</a> </div> </li> <li class="dropdown"> <a class="dropbtn" href="#">Opinion</a> <div class="dropdown-content"> <a href="#">Tamil Nadu</a> <a href="#">Andha</a> <a href="#">Kerala</a> <a href="#">Karnataka</a> </div> </li> <li class="dropdown"> <a class="dropbtn" href="#">Life &amp; Style</a><div class="dropdown-content"> <a href="#">Tamil Nadu</a> <a href="#">Andha</a> <a href="#">Kerala</a> <a href="#">Karnataka</a> </div></li> <li class="dropdown"> <a class="dropbtn" href="#">Entertainment</a><div class="dropdown-content"> <a href="#">Tamil Nadu</a> <a href="#">Andha</a> <a href="#">Kerala</a> <a href="#">Karnataka</a> </div></li> <li class="dropdown"> <a class="dropbtn" href="#">EBusiness</a><div class="dropdown-content"> <a href="#">Tamil Nadu</a> <a href="#">Andha</a> <a href="#">Kerala</a> <a href="#">Karnataka</a> </div></li> <li class="dropdown"> <a class="dropbtn" href="#">ESport</a><div class="dropdown-content"> <a href="#">Tamil Nadu</a> <a href="#">Andha</a> <a href="#">Kerala</a> <a href="#">Karnataka</a> </div> </li> </ul> 

Fiddle demo 小提琴演示

Im find a solution base on Jquery and I'm still working to find an pure Css option. 我找到了一个基于Jquery的解决方案,我仍在努力寻找一个纯粹的Css选项。

Edit: 编辑:

and i find a css solution with small changes in your html! 我发现你的HTML中有一些小变化的css解决方案!

Im change your html li block to this: 我将你的html li块更改为:

<li class="dropdown">
  <div class="holder">
    <a class="dropbtn"  href="#">News</a>
    <div class="dropdown-content">
      <a href="#">national</a>
      <a href="#">international</a>
      <a href="#">states</a>
      <a href="#">cities</a>
    </div>
  </div>
</li>

And change your css to this: 并将你的CSS改为:

#other-menu { 
  display: flex;
  width:850px; 
  background-color:#eee;
}

#other-menu li { 
  list-style:none;
}

li a, .dropbtn {
    display: block;
    color: #828282;
    text-decoration: none;
}

.dropbtn {
  padding: 10px;
  white-space: nowrap;
}

.dropdown {
  position: relative;
}

.dropdown-content {
    display: none;
    position: relative;
    background-color: #fff;
    min-width: 100%;
    white-space: nowrap;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 999999;
    padding: 5px 0;
    height: auto;
    top: 0px;
    font-family: 'Fira-Sans-Semibold', sans-serif;
    text-transform: uppercase;
    border-top: 1px solid #ccc;
    border-radius: 2px;
    -webkit-border-radius: 2px;
    -moz-border-radius: 2px;s
}
.holder > .dropdown-content a {
    padding: 3px 6px;
    text-decoration: none;
    text-align: left;
    font-size: 12px;
    letter-spacing: 1px;
}

.zclass {
  z-index: 200;
}

.holder > .dropdown-content a:hover,.dropdown > .dropdown-content a:focus { color:#5A5A5A !important; text-decoration: none;}

.holder:hover {
  z-index: 999;
  position: absolute;
  top: 0;
  left: 0;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  background: #fff;
  border: 1px solid #eeeeee;
  border-radius: 2px;
  -webkit-border-radius: 2px;
  -moz-border-radius: 2px;
}

.holder a:hover {
  text-decoration: none; color: #828282 !important; 
}

.holder:hover .dropdown-content {
    display: block;
}

#other-menu {
  z-index: 2000;
}

Css Fiddle demo Css小提琴演示


This is example get your dropdown width and insert into the closest tab (the parent tab), check it out. 这是示例获取您的下拉宽度并插入最近的选项卡(父选项卡),检查出来。

My Jquery option: 我的Jquery选项:

$(function(){
    $('.dropdown-content').each(function(){
    var dropdownWidht = $(this).width();
    $(this)
    .closest('.dropdown')
    .find('.dropbtn')
    .css('width', dropdownWidht);
  })
})

Jquery Fiddle demo Jquery小提琴演示

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

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