如何根据li项目的数量更改li项目之间的宽度

时间:2017-03-29 20:52:55

标签: javascript jquery html css

我正在尝试创建一个“可重复使用”的进度跟踪栏,我需要根据li项目的数量更改li项目之间的宽度。

我从this other question找到了这个JavaScript代码:

$('.lesson-nav ul li').width( 100 / $(".lesson-nav ul li").length + '%' );

我太近了。有人可以帮助解决使其无效的错误吗?

$('.progressTrackerBar ul li').width(100 / $(".progressTrackerBar ul li").length + '%');
/*row with border*/

.progressTrackerBarRow {
  display: inline-block;
  width: 700px;
  height: 80px;
  border-style: solid;
  border-width: thin;
  border-color: blue;
}


/*remove bullets*/

ul#progressTrackerBar {
  list-style-type: none;
}


/*horizontal list*/

ul#progressTrackerBar li {
  float: left;
  display: inline;
  /*width: 14%;  /*Space between circles*/
  /*width: var(--w);*/
  position: relative;
  text-align: center;
  font-size: large;
}


/*circles*/

ul#progressTrackerBar li:before {
  content: '';
  width: 30px;
  height: 30px;
  line-height: 30px;
  border: 1px solid #9bbdfa;
  display: block;
  text-align: center;
  margin: -5px auto 1px auto;
  /*top right bottom left*/
  border-radius: 50%;
  background: radial-gradient(at 70% bottom, #9bbdfa, white);
}


/*lines*/

ul#progressTrackerBar li:after {
  content: '';
  position: absolute;
  width: 100%;
  height: 2px;
  background-color: #ddd;
  top: 10px;
  left: -50%;
  z-index: -1;
}


/*remove first line*/

ul#progressTrackerBar li:first-child:after {
  content: none;
}


/*active circle text*/

ul#progressTrackerBar li.active {
  /*color: #8f6bfb;*/
  font-weight: 600;
}


/*active circle border*/

ul#progressTrackerBar li.active:before {
  background: radial-gradient(at 70% bottom, #5444e1, white);
  border-color: #5444e1;
}


/*active line*/

ul#progressTrackerBar li.active+li:after {
  background-color: #5444e1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="row progressTrackerBarRow" align="center">
  <div class="container" style="width: 100%;">
    <ul id="progressTrackerBar">
      <li id="1" class="active">Step 1</li>
      <li id="2">Step 2</li>
      <li id="3">Step 3</li>
      <li id="4">Step 4</li>
      <li id="5">Step 5</li>
      <li id="6">Step 6</li>
      <li id="7">Step 7</li>
    </ul>
  </div>
</div>

2 个答案:

答案 0 :(得分:3)

你的选择器不太对劲。请改用$('#progressTrackerBar li')

&#13;
&#13;
$('#progressTrackerBar li').width(100 / $("#progressTrackerBar li").length + '%');
&#13;
/*row with border*/

.progressTrackerBarRow {
  display: inline-block;
  width: 700px;
  height: 80px;
  border-style: solid;
  border-width: thin;
  border-color: blue;
}


/*remove bullets*/

ul#progressTrackerBar {
  list-style-type: none;
}


/*horizontal list*/

ul#progressTrackerBar li {
  float: left;
  display: inline;
  /*width: 14%;  /*Space between circles*/
  /*width: var(--w);*/
  position: relative;
  text-align: center;
  font-size: large;
}


/*circles*/

ul#progressTrackerBar li:before {
  content: '';
  width: 30px;
  height: 30px;
  line-height: 30px;
  border: 1px solid #9bbdfa;
  display: block;
  text-align: center;
  margin: -5px auto 1px auto;
  /*top right bottom left*/
  border-radius: 50%;
  background: radial-gradient(at 70% bottom, #9bbdfa, white);
}


/*lines*/

ul#progressTrackerBar li:after {
  content: '';
  position: absolute;
  width: 100%;
  height: 2px;
  background-color: #ddd;
  top: 10px;
  left: -50%;
  z-index: -1;
}


/*remove first line*/

ul#progressTrackerBar li:first-child:after {
  content: none;
}


/*active circle text*/

ul#progressTrackerBar li.active {
  /*color: #8f6bfb;*/
  font-weight: 600;
}


/*active circle border*/

ul#progressTrackerBar li.active:before {
  background: radial-gradient(at 70% bottom, #5444e1, white);
  border-color: #5444e1;
}


/*active line*/

ul#progressTrackerBar li.active+li:after {
  background-color: #5444e1;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="row progressTrackerBarRow" align="center">
  <div class="container" style="width: 100%;">
    <ul id="progressTrackerBar">
      <li id="1" class="active">Step 1</li>
      <li id="2">Step 2</li>
      <li id="3">Step 3</li>
      <li id="4">Step 4</li>
      <li id="5">Step 5</li>
      <li id="6">Step 6</li>
      <li id="7">Step 7</li>
    </ul>
  </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:3)

您需要定位正确的li元素:

$('#progressTrackerBar li').width( 100 / $("#progressTrackerBar li").length + '%' );