Way to keep selected item in sidebar menu on top after page reload
P粉955063662
P粉955063662 2024-02-17 14:49:08
0
1
344

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粉955063662
P粉955063662

reply all(1)
P粉461599845

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;
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template