按钮:根据可用空间动态调整大小

时间:2018-08-24 11:10:24

标签: javascript jquery css css3

enter image description here

因此要求是,默认情况下,此处的选项卡或向导必须显示全名,而不活动的选项卡或向导必须根据右侧的可用空间缩小。知道我们如何实现这一目标吗?

enter image description here

默认情况下,按钮的宽度为:自动。但是当没有。按钮的数量更多,并且当右边的空间开始减小时,不活动的按钮必须缩小。

1 个答案:

答案 0 :(得分:1)

我认为您正在寻找类似的东西:

//Number of elemnts
let count = $('li').length;

//Width of each element
let width = 100 / count;
$('li').css('width', width + '%');

//Width of elements container (ul)
let ulWidth = parseFloat($("ul").css("width"));


for (let i = 0; i< count; i++) {

  $($('li')[i]).click(function() {
  
  	//Reset elements width
    $('li').css('width', 'auto');

    //Add active class to clicked element
    $('li').removeClass('active');
    $($('li')[i]).addClass('active');
    
    if ( $('li').hasClass('active') ) {
    
    	//Width of active element
      let activeWidth = parseFloat($("li.active").css("width"));
      let activeWidthPerc = (activeWidth * 100) / ulWidth;

      //Width of the rest of the elements
      let generalWidth = (100 - activeWidthPerc) / (count - 1);
      
      //Will only adapt when the active element is smaller than the rest of the elements
      if (activeWidthPerc < generalWidth) {
        
        $('li').css('width', width + '%');
        
      } else {
        
        $('li').css('width', generalWidth + '%'); //Set width to rest of the elements
        $('li.active').css('width', activeWidthPerc + '%'); //Set width to active element
        
      }
    
    }

  });

}
@import url("https://fonts.googleapis.com/icon?family=Material+Icons");

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box; 
}

body {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
}

nav {
  width: 80%;
  -webkit-box-shadow: 0px 0px 10px 0px rgba(0,0,0,0.5);
  -moz-box-shadow: 0px 0px 10px 0px rgba(0,0,0,0.5);
  box-shadow: 0px 0px 10px 0px rgba(0,0,0,0.5);
}

ul {
  width: 100%;
}

li {
  list-style: none;
  float: left;
  padding: 10px;
  text-align: center;
  background: #EEEEEE;
  cursor: pointer;
  color: #777;
  border-right: 1px solid #999;
  display: flex;
  align-items: center;
  justify-content: center;
}

li:last-child {
  border-right: none;
}

p {
  font-size: 20px;
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
}

i {
  vertical-align: bottom;
  margin-right: 5px;
}

.active {
  background: #3C8CBD;
  color: #fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
  <nav>
    <ul>
      <!--Try to add more li elements-->
      <li>
        <i class="material-icons">home</i><p>Home</p>
      </li>
      <li>
        <i class="material-icons">airplanemode_active</i><p>Plane</p>
      </li>
      <li>
        <i class="material-icons">directions_car</i><p>Car</p>
      </li>
      <li>
        <i class="material-icons">people</i><p>People</p>
      </li>
    </ul>
  </nav>
</body>

您可以在https://codepen.io/Nacorga/pen/MqaLGK

中找到完整的示例

我希望它能对您有所帮助;)