search

Home  >  Q&A  >  body text

Way to keep selected item in sidebar menu on top after page reload

I have a scrolling sidebar menu. So when I select a menu item it should appear at the top position of the sidebar. There is no problem setting up the active menu. I got it. But I can't set the top position of the sidebar. Please do not recommend jQuery. Instead I was advised to use plain JavaScript Here is some code reference

.sidebar{
    height:100px
    overflow-y:scroll;
}
.sidebar ul li{
    padding:30px;
}
<!-- CSS -->

 <!-- html  -->
<div class="sidebar">
  <ul>
    <li>item1</li>
    <li>item2</li>
    <li>item3</li>
    <li>item4</li>
    <li>item5</li>
    <li>item7</li>
    <li>item8</li>
    <li>item9</li>........
    <li>item100</li>
  </ul>
</div>

P粉955063662P粉955063662282 days ago336

reply all(1)I'll reply

  • P粉461599845

    P粉4615998452024-02-18 13:49:54

    Pure CSS solution using :target pseudo-class, flexbox layout, and order properties.

    .sidebar {
      background-color: #ddd;
      height: 200px;
      overflow-y: scroll;
    }
    .sidebar ul {
      display: flex;
      flex-direction: column;
      margin: 0;
    }
    .sidebar li {
      padding: 1em;
      order: 1;
    }
    .sidebar :target {
      background-color: #aaa;
      order: 0;
    }

    reply
    0
  • Cancelreply