I have a simple text slider, but I’m now solving the following problems there.
- I only want to display what fits in 300px when it loads. So in this case it displays "Some longer text, Text, Some text, Some.." but to me it displays part of another element. Is it possible to limit it to show only the complete items?
- Moving. If only the visible items are displayed, is it possible to make it move two fully visible items each time? It doesn’t matter which side. And it always ends with the last item at the end of the slider or the first one at the beginning. Is it possible to do this?
Here is my code.
var currentPosition = 0;
function moveMenu(direction) {
var menu = document.getElementById('menu');
var menuWrapperWidth = document.querySelector('.menu-wrapper').offsetWidth;
var totalMenuWidth = [...menu.children].reduce((acc, el) => acc + el.offsetWidth + 20, 0);
var maxScroll = totalMenuWidth - menuWrapperWidth;
var scrollStep = menuWrapperWidth / 2;
if (direction === 'next' && currentPosition > -maxScroll) {
currentPosition -= scrollStep;
currentPosition = Math.max(currentPosition, -maxScroll);
} else if (direction === 'prev' && currentPosition < 0) {
currentPosition += scrollStep;
currentPosition = Math.min(currentPosition, 0);
}
menu.style.transform = `translateX(${currentPosition}px)`;
}
document.getElementById('prevButton').addEventListener('click', function() {
moveMenu('prev');
});
document.getElementById('nextButton').addEventListener('click', function() {
moveMenu('next');
});
.menu-wrapper {
width: 300px;
overflow: hidden;
}
.menu {
display: flex;
white-space: nowrap;
transition: transform 0.5s ease;
}
.menu-item {
margin-right: 20px;
flex-shrink: 0;
}
<div class="menu-wrapper">
<button id="prevButton">Prev</button>
<div id="menu" class="menu">
<span class="menu-item">Some longer text</span>
<span class="menu-item">Text</span>
<span class="menu-item">Some text</span>
<span class="menu-item">Some text</span>
<span class="menu-item">Longer text</span>
<span class="menu-item">Some longer text</span>
<span class="menu-item">Text</span>
<span class="menu-item">Some text</span>
</div>
<button id="nextButton">Next</button>
</div>
2
Answers
Is this what you want?