Reputation: 8722
I have a list of items in a container. The container takes the full width of the parent, allowing the items to overflow horizontally if needed. Here's what it looks like:
ul {
list-style: none;
display: flex;
overflow-x: auto;
padding: 0;
margin: 0;
border: 1px dotted black;
}
li {
margin-right: 10px;
}
li:last-child {
margin-right: 0;
}
a {
display: block;
text-decoration: none;
white-space: nowrap;
padding: 4px 8px;
color: black;
background-color: white;
border: 1px solid black;
}
a.active {
background-color: blue;
}
<ul id="container">
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li><a href="#" class="active" id="element">Item</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
</ul>
In the list of items, there is one that has the .active
class. I want to horizontally scroll the container in a way such that this item is exactly centered. To do this, I wrote the following function:
function toMiddle(element, container) {
if (container === undefined) {
container = window;
}
var elementRect = element.getBoundingClientRect();
var absoluteElementLeft = elementRect.left;
var middleDiff = (elementRect.width / 2);
var scrollLeftOfElement = absoluteElementLeft + middleDiff;
var scrollX = (scrollLeftOfElement - (container.getBoundingClientRect().width / 2));
container.scrollTop = 0;
container.scrollLeft = scrollX;
}
And I'm trying to call this to center the container as follows:
toMiddle(document.getElementById("element"), document.getElementById("container"));
Unfortunately, this doesn't seem to work properly. Any idea how I can fix this? And what exactly am I doing wrong here?
Please note, scrollIntoView()
works perfectly well, but it scrolls the whole page vertically as well, which is not an acceptable thing for my use case.
Upvotes: 5
Views: 364
Reputation: 3480
You can use .offsetLeft
method to find distance from left instead of .getBoundingClientRect()
method. And count width of active item with .clientWidth
method instead of .width
and I also added .resize
method for active item keeping it centered
.
Useful links:
https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetLeft
https://developer.mozilla.org/en-US/docs/Web/API/Element/clientWidth
function toMiddle(container_id) {
var container = document.getElementById(container_id);
var elementRect = document.querySelector('#' + container_id + ' .active');
var absoluteElementLeft = elementRect.offsetLeft;
var middleDiff = (elementRect.clientWidth / 2);
var scrollLeftOfElement = absoluteElementLeft + middleDiff;
var scrollX = (scrollLeftOfElement - (container.clientWidth / 2));
container.scrollTop = 0;
container.scrollLeft = scrollX;
}
/*Initialize on page load*/
toMiddle("container");
/*Window resize then active item tab should be centered*/
var timeout = 0;
window.onresize = () => {
clearTimeout(timeout);
timeout = setTimeout(() => {
toMiddle("container");
}, 500)
}
/*Click on item tab then should be centered*/
var allItem = [...document.querySelectorAll('#container li a')]
allItem.forEach(element => {
element.addEventListener('click', (e)=> {
allItem.forEach(ele => {
ele.classList.remove('active');
})
/*Add active class*/
e.target.classList.add('active');
/*Call the function for set item tab in centered*/
toMiddle("container");
})
});
ul {
list-style: none;
display: flex;
overflow-x: auto;
padding: 10px;
margin: 0;
border: 1px dotted black;
position: relative;
}
li {
margin-right: 10px;
}
li:last-child {
margin-right: 0;
}
a {
display: block;
text-decoration: none;
white-space: nowrap;
padding: 4px 8px;
color: black;
background-color: white;
border: 1px solid black;
transition: 0.5s ease-in;
}
a.active {
background-color: #0156e8;
border: 1px solid #00389a;
color: #fff;
}
<ul id="container">
<li><a href="#">Item 1</a></li>
<li><a href="#">Item 2</a></li>
<li><a href="#">Item 3</a></li>
<li><a href="#">Item 4</a></li>
<li><a href="#">Item 5</a></li>
<li><a href="#">Item 6</a></li>
<li><a href="#">Item 7</a></li>
<li><a href="#">Item 8</a></li>
<li><a href="#">Item 9</a></li>
<li><a href="#">Item 10</a></li>
<li><a href="#">Item 11</a></li>
<li><a href="#">Item 12</a></li>
<li><a href="#">Item 13</a></li>
<li><a href="#">Item 14</a></li>
<li><a href="#">Item 15</a></li>
<li><a href="#">Item 16</a></li>
<li><a href="#">Item 17</a></li>
<li><a href="#" class="active">Item 18</a></li>
<li><a href="#">Item 19</a></li>
<li><a href="#">Item 20</a></li>
<li><a href="#">Item 21</a></li>
<li><a href="#">Item 22</a></li>
<li><a href="#">Item 23</a></li>
<li><a href="#">Item 24</a></li>
<li><a href="#">Item 25</a></li>
<li><a href="#">Item 26</a></li>
<li><a href="#">Item 27</a></li>
<li><a href="#">Item 28</a></li>
<li><a href="#">Item 29</a></li>
<li><a href="#">Item 30</a></li>
<li><a href="#">Item 31</a></li>
<li><a href="#">Item 32</a></li>
<li><a href="#">Item 33</a></li>
</ul>
Upvotes: 3