【问题标题】:How can I reverse the order of div elements upside down on 'onclick'?如何在“onclick”上颠倒 div 元素的顺序?
【发布时间】:2020-05-05 22:21:32
【问题描述】:

我有以下代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>StackOverflow Question</title>
    <style>
    .container--wrap {
        background-color: #000000;
        border-radius: 15px;
        margin: 5px;
        overflow: hidden;
        justify-content: stretch;
        align-items: flex-end;
    }
    .scroller2{
        height: 250px;
        overflow-y: scroll;
    }
    .circle {
      position: relative;
      margin-bottom: 40px;
      height: 20px;
      display: flex;
      align-items: center;
    }

    .circle::before {
      content:"";
      display: inline-block;
      width: 20px;
      height: 20px;
      border-radius: 50%;
      margin-right: 5px;
    }

    .circle::after {
      position: absolute;
      content: '';
      left: 7.5px;
      top: 20px;
      width: 5px;
      background: #4d4d4d;
      height: 40px;
    }
    .minor::before {
      background-color: purple;
    }

    .major::before {
      background-color: red;
    }

    .gray::before {
      background-color: gray;
    }
    .tlbtn {
      background-color: #343434;
      border-radius: 10px;
      color: white;
      padding: 10px 20px;
      text-align: center;
      text-decoration: none;
      display: inline-block;
      font-size: 16px;
      margin: 4px 2px;
      cursor: pointer;
      font-family: robotobold;
    }
    #tltitle,.tlbtn{
      display: inline-flex;
    }

    </style>
</head>
<body>

    <script>
    function btn1Click() {
        document.getElementById("btn1").style.backgroundColor="#5c5c5c";
        document.getElementById("btn2").style.backgroundColor="#343434";
    }
    function btn2Click() {
        document.getElementById("btn2").style.backgroundColor="#5c5c5c";
        document.getElementById("btn1").style.backgroundColor="#343434";
    }
    </script>
    <div class="container--wrap-scroll scroller2" id="timeline">
        <p id="tltitle" style="color: #D4E1E4; font-size: 20px; text-align: left; padding-top: 0px; padding-right: 10px;color: black;"><b>Timeline</b></p>
        <div id="btn1" class="tlbtn" style="margin-bottom: -10px; background-color: #5c5c5c;" onclick="btn1Click();"><text style="font-size: 15px; font-family: robotobold;text-align: center;">Newest</text></div>
        <div id="btn2" class="tlbtn" style="margin-bottom: -10px;" onclick="btn2Click();"><text style="font-size: 15px; font-family: robotobold; text-align: center;">Oldest</text></div>
        <div class="circle major "><text style="color:red; font-family:robotolight; font-size: 12px;">Major Event | Yesterday</text></div>
        <div class="circle gray"><text style="color:black; font-family:robotolight; font-size: 12px;">Information | 2 days ago</text></div>
        <div class="circle gray"><text style="color:black; font-family:robotolight; font-size: 12px;">Information | 1 week ago</text></div>
        <div class="circle minor"><text style="color:purple; font-family:robotolight; font-size: 12px;">Minor Event | 1 month ago</b></text></div>
    </div>
</body>
</html>

当按下“最旧”时,我希望 div 元素的顺序反向排序 - 请注意最后一个圆圈在其下方有一条线,我希望它保持不变:

例如当按下 OLDEST 时,Major Event 将位于底部,并且下方也有一条灰线。

有什么办法可以做到吗?

截图:

【问题讨论】:

  • 如果您将div 元素保留在一个数组中,那么您所要做的就是反转数组。
  • 好吧,Array.from(parent.childNodes).reverse() 并不是什么大问题
  • 您最初要求将其倒置...所以我为您准备了这个:jsfiddle.net/6xnhs84w:D
  • @ScottMarcus 标题中引用的答案包含近 10 年前的解决方案。 Danzinger 的提议是最新的答案。 2011 年还没有 Flexbox!
  • 我不得不说,flexbox 可能是我最喜欢的解决方案。 flexbox 真是天赐之物。但是,公平地说,这仍然是一个骗局。你可以随时把它发到那里,这个仍然可以被点赞和接受。

标签: javascript html css


【解决方案1】:

如果您只想使用 CSS,可以尝试使用 Flexbox 并在 flex-direction columncolumn-reverse 之间切换:

const timeline = document.getElementById('timeline');
const newestButton = document.getElementById('button--newest-first');
const oldestButton = document.getElementById('button--oldest-first');

newestButton.onclick = () => {
  timeline.classList.remove('timeline--oldest-first');
  timeline.classList.add('timeline--newest-first');
  oldestButton.classList.remove('button--selected');
  newestButton.classList.add('button--selected');
};

oldestButton.onclick = () => {
  timeline.classList.add('timeline--oldest-first');
  timeline.classList.remove('timeline--newest-first');
  oldestButton.classList.add('button--selected');
  newestButton.classList.remove('button--selected');
};
body {
  margin: 0;
  padding: 8px;
  font-family: monospace;
  font-size: 14px;
}

.header {
  display: flex;
  padding-bottom: 8px;
  margin-bottom: 16px;
  align-items: center;
  border-bottom: 2px solid black;
}

.title {
  margin-right: auto;
}

.button {
  font-family: monospace;
  font-size: 14px;
  border-radius: 2px;
  padding: 4px 8px;
  margin-left: 8px;
  cursor: pointer;
  border: 0;
  outline: none;
  border: 2px solid black;
  background: transparent;
}

.button--selected {
  background: yellow;
}

.timeline {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
}

.timeline--newest-first {
  flex-direction: column;
}

.timeline--oldest-first {
  flex-direction: column-reverse;
}

.circle {
  position: relative;
  margin-bottom: 16px;
  height: 20px;
  display: flex;
  align-items: center;
}

.timeline--newest-first > .circle:last-child,
.timeline--oldest-first > .circle:first-child {
  margin-bottom: 0;
}

.circle::before {
  content:"";
  display: inline-block;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  margin-right: 16px;
  border: 2px solid black;
}

.circle::after {
  position: absolute;
  content: '';
  left: 9px;
  top: 20px;
  width: 2px;
  background: #000;
  height: 16px;
}

.timeline--newest-first > .circle:last-child::after,
.timeline--oldest-first > .circle:first-child::after {
  display: none;
}

.minor::before {
  background-color: purple;
}

.major::before {
  background-color: red;
}

.gray::before {
  background-color: gray;
}
<header class="header">
  <strong class="title">TIMELINE</strong>

  <button id="button--newest-first" class="button button--selected">
    NEWEST
  </button>

  <button id="button--oldest-first" class="button">
    OLDEST
  </button>
</header>

<ul class="timeline timeline--newest-first" id="timeline">
  <li class="circle major ">
    Major Event | Yesterday
  </li>

  <li class="circle gray">
    Information | 2 days ago
  </li>

  <li class="circle gray">
    Information | 1 week ago
  </li>

  <li class="circle minor">
    Minor Event | 1 month ago
  </li>
</ul>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-03
    • 2012-04-15
    • 1970-01-01
    • 2021-06-10
    • 1970-01-01
    • 1970-01-01
    • 2015-06-21
    相关资源
    最近更新 更多