css - CSS中有序列表计数器的反转顺序

标签 css

我有一个摘要让您更好地理解我的问题:

$(window).load(function() {
  var total;

  $(document).ready(function() {
    $('#dupe').click(function() {
      $('#uploadForms').prepend($('#htmlTemplate').html());
      $($('#uploadForms .upload_form')[0]).animate({
        height: $($('#uploadForms .upload_form')[$('#uploadForms .upload_form').size() - 1]).css('height'),
        opacity: 1
      }, 500);
      totalForms();

      $(".upload_form").each(function() {
        if (typeof $(this).find('a.close')[0] === 'undefined') {
          if ($('#uploadForms .upload_form').size() > 1) $(this).prepend('<a class="close">x</a>');
        }
      });

      $(".upload_form").on("click", ".close", function() {
        $(this).parent().animate({
          height: 0,
          opacity: 0,
          paddingTop: 0,
          paddingBottom: 0
        }, 500, function() {
          $(this).parent().remove();
          totalForms()
        });
        if ($('#uploadForms .upload_form').size() - 1 <= 1) $('#uploadForms .upload_form').find('a.close').remove();
      });
    });

    function totalForms() {
      total = $('#uploadForms .upload_form').size();
      $('#total').html(total);
    }
  });
});
.upload_form {
  background-color: rgba(0, 0, 0, .3);
  padding: 16px 64px 10px 76px;
  border-bottom-left-radius: 15px;
  border-bottom-right-radius: 15px;
  margin-bottom: 10px;
}
.coverArt {
  margin-right: 48px;
  margin-bottom: 36px;
}
label input,
label textarea {
  width: 252px;
}
.info {
  display: block;
  vertical-align: top;
}
.close {
  float: right;
  color: white;
  margin: 10px;
  display: block;
  cursor: hand;
  border-radius: 100%;
  cursor: pointer;
  width: 18px;
  height: 18px;
  text-align: center;
  -moz-user-select: none;
  -webkit-user-select: none;
}
.close:hover {
  background-color: rgba(0, 0, 0, 0.5);
}
ol {
  counter-reset: li;
  margin-left: 0;
  padding-left: 0;
}
ol > li {
  position: relative;
  margin: 0 0 6px 2em;
  padding: 0px 0px 8px;
  list-style: none;
  border-top: 2px solid #666;
}
ol > li:before {
  content: counter(li);
  counter-increment: li;
  position: absolute;
  top: -2px;
  left: -2em;
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  width: 2em;
  margin-right: 8px;
  padding: 4px;
  border-top: 2px solid #666;
  color: #fff;
  background: #666;
  font-weight: bold;
  font-family: "Helvetica Neue", Arial, sans-serif;
  text-align: center;
}
li ol,
li ul {
  margin-top: 6px;
}
ol ol li:last-child {
  margin-bottom: 0;
}
.template {
  display: none
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="button" value="Add file..." id="dupe">
<input type="button" value="Upload file(s)" id="dupe">

<hr>

<p>Total: <span id="total">1</span></p>

<div id="htmlTemplate" class="template">
  <li>
    <div class="upload_form" style="height:0;opacity:0;">
      <span class="number"></span>
      <a class="close">x</a>
      <div class="info">
        <p>Filler</p>
      </div>
    </div>
  </li>
</div>

<ol id="uploadForms">
  <li>
    <div class="upload_form">
      <span class="number"></span>
      <a class="close">x</a>
      <div class="info">
        <p>Filler</p>
      </div>
    </div>
  </li>
</ol>


在此演示中,如果单击“添加文件...”,则将添加一个新的列表元素,并在其中添加新的<div>。我的问题是:如何颠倒柜台的顺序?

相关CSS:

ol {
    counter-reset:li;
}
ol > li {
    position:relative;
    margin:0 0 6px 2em;
    padding: 0px 0px 8px;
    list-style:none;
    border-top:2px solid #666;
}
ol > li:before {
    content: counter(li);
    counter-increment:li;
    position:absolute;
    top:-2px;
    left:-2em;
    box-sizing:border-box;
    width:2em;
    margin-right:8px;
    padding:4px;
    border-top:2px solid #666;
    color:#fff;
    background:#666;
    font-weight:bold;
    font-family:"Helvetica Neue", Arial, sans-serif;
    text-align:center;
}

最佳答案

我没有纯粹的CSS解决方案,但是您的页面已经包含大量JS,因此我认为这是可以的。

对于li添加

counter-increment: li -1;

使它倒数。这意味着它不能重置为0,但必须重置全部元素。

对于ol
counter-reset:li 2;

然后更新您的JS:
$('#total').html(total);
$("ol").css('counter-reset', 'li ' + (+total + 1));

http://jsfiddle.net/ExplosionPIlls/ArZUW/1/

关于css - CSS中有序列表计数器的反转顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15844387/

相关文章:

html - 如何放置我的页脚?

jquery - 更改同级元素 z-index 值的增量倒计时

javascript - 自动中断文本,用户名背后的理由?

html - 无法将图像设置为适合整个 div

css - 我的阴影过滤器语法不正确吗?

jquery - 定位表中的第一个 TR 类

css - 自定义字体(不使用@font-face)不会在 HTTPS 协议(protocol)上加载

c# - 如何让一个 <div> 响应另一个 <div>?

html - 带 Bootstrap 的 Z 索引

javascript - 如何在点击时删除(转换 - 翻译)元素?