It seems like you're struggling with setting the scrollbar position dynamically based on the page width in your current code. One approach in Vue is to wrap your content in a div
container and use the scrollLeft
property within the mounted()
function to achieve this.
If you're looking for an example of how this can be done, check out this CodePen snippet:
Snippet
Link: CodePen
document.addEventListener('DOMContentLoaded', function () {
const scrollContainer = document.getElementById('scroll-container');
const content = document.getElementById('content');
const middlePosition = (content.offsetWidth - scrollContainer.clientWidth) / 2;
scrollContainer.scrollLeft = middlePosition;
});
#scroll-container {
width: 300px;
overflow-x: auto;
}
#content {
white-space: nowrap;
display: inline-block;
width: 600px;
}
<div class="scroll-container" id="scroll-container">
<div class="content" id="content">
</div>
</div>