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

时间:2013-04-05 22:04:29

标签: 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;
}

4 个答案:

答案 0 :(得分:4)

我没有一个纯粹的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/

答案 1 :(得分:2)

这是另一种与Explosion Pills非常类似的解决方案:http://jsfiddle.net/pN6Fx/

CSS

ol{
    list-style-type: none;
    list-style-position: inside;
}
li:before{
    content: counter(list-items) '. ';
    counter-increment: list-items -1;
}

HTML

<ol style="counter-reset: list-items 5;">
    <li>An element</li>
    <li>An element</li>
    <li>Another element</li>
    <li>A third element</li>
</ol>

您只需要通过JavaScript更新counter-reset属性

答案 2 :(得分:0)

在此页面后http://www.impressivewebs.com/reverse-ordered-lists-html5/存在reversed元素的<ol>属性。

糟糕,我不明白是否支持

答案 3 :(得分:0)

如果您正在寻找纯css解决方案和Javascript - How to detect if document has loaded (IE 7/Firefox 3),则可以利用flex-direction属性来反转列表项的顺序。

这会给出反转列表的外观,但DOM命令实际上不会改变。