To make the scrolling continuous without visible jumps and add some space before the first scroll item initially, we need to:
- Add padding (spacing) before the first item in the scrolling content so it doesn't appear cropped when the page loads.
- Instead of resetting the scroll position directly to zero or the start, we use the duplicated content’s width to loop seamlessly, translating the content continuously.
Your current code duplicates the content and moves both the original .content and the .clone side by side, but when the scroll reaches the width of the content, it jumps back by contentWidth which causes the visible jump.
To fix that, the key trick is:
- Make sure the combined width of original and clone is enough for smooth looping.
- When the translateX reaches
-contentWidth (negative value of content width), reset to 0 without visually affecting the scroll position (since the clone is there to cover the gap).
- We need to add some left padding or margin to the content container or first item to add the space before the first scroll item.
Updated code:
CSS changes:
Add padding-left to .new-horizontal-scroll-bar-content (or margin-left to first span) to add the space before the first item is visible:
/* Add some left padding to the scroll content */
.new-horizontal-scroll-bar-content {
padding-left: 100px; /* adjust space as you like */
}
Alternatively, you can do:
.new-horizontal-scroll-bar-content span:first-child {
margin-left: 100px;
}
JavaScript changes:
Adjust the method that resets position to prevent jump:
(function() {
class NewHorizontalScrollBar {
constructor(container) {
this.container = container;
this.content = container.querySelector('.new-horizontal-scroll-bar-content');
// Duplicate the content
this.clone = this.content.cloneNode(true);
this.clone.setAttribute('aria-hidden', 'true');
container.appendChild(this.clone);
this.scrollSpeed = parseFloat(container.getAttribute('data-scroll-speed')) || 100; // px per second
// Current translateX position
this.x = 0;
// Width of original content + its computed left padding, so clone matches position properly
this.contentWidth = this.content.scrollWidth;
// Bind animation frame callback
this.animate = this.animate.bind(this);
// Pause on hover
this.isPaused = false;
this.container.addEventListener('mouseenter', () => { this.isPaused = true; });
this.container.addEventListener('mouseleave', () => { this.isPaused = false; });
// Start animation
this.start();
}
start() {
this.lastTimestamp = null;
requestAnimationFrame(this.animate);
}
animate(timestamp) {
if (!this.lastTimestamp) this.lastTimestamp = timestamp;
const elapsed = timestamp - this.lastTimestamp;
this.lastTimestamp = timestamp;
if (!this.isPaused) {
this.x -= (this.scrollSpeed * elapsed) / 1000;
// Reset scroll when fully scrolled by contentWidth, but do it smoothly
if (this.x <= -this.contentWidth) {
this.x += this.contentWidth;
}
// Apply translateX to original and cloned content
this.content.style.transform = `translateX(${this.x}px)`;
this.clone.style.transform = `translateX(${this.x + this.contentWidth}px)`;
}
requestAnimationFrame(this.animate);
}
}
document.addEventListener('DOMContentLoaded', () => {
const containers = document.querySelectorAll('.new-horizontal-scroll-bar-container');
containers.forEach(container => {
new NewHorizontalScrollBar(container);
});
});
})();
Full final updated code with CSS snippet
<style>
.new-horizontal-scroll-bar-container {
position: relative;
width: 100%;
overflow: hidden;
background-color: #222;
color: #eee;
white-space: nowrap;
box-sizing: border-box;
border: 1px solid #444;
font-family: Arial, sans-serif;
font-size: 18px;
height: 40px;
display: flex;
align-items: center;
}
/* Add left padding on the content for initial space */
.new-horizontal-scroll-bar-content {
display: inline-block;
white-space: nowrap;
will-change: transform;
margin: 0;
vertical-align: middle;
padding-left: 100px; /* <-- add this for initial space, adjust as needed */
}
/* Spacing between spans */
.new-horizontal-scroll-bar-content span {
margin-right: 50px;
display: inline-flex;
align-items: center;
}
.new-horizontal-scroll-bar-content span svg,
.new-horizontal-scroll-bar-content span img {
height: 1em;
width: auto;
margin-right: 8px;
}
</style>
<div class="new-horizontal-scroll-bar-container" data-scroll-speed="100">
<div class="new-horizontal-scroll-bar-content">
<span>🚀 Launching new features! </span>
<span>🔥 Hot deals all week! </span>
<span>📢 Subscribe now! </span>
<span>⭐️ Rated #1 in customer service! </span>
</div>
</div>
<div class="new-horizontal-scroll-bar-container" data-scroll-speed="50" style="margin-top:30px;">
<div class="new-horizontal-scroll-bar-content">
<span>🎉 Welcome to our website! </span>
<span>💡 Tips & Tricks every day </span>
<span>📩 Join our newsletter! </span>
</div>
</div>
<script>
(function() {
class NewHorizontalScrollBar {
constructor(container) {
this.container = container;
this.content = container.querySelector('.new-horizontal-scroll-bar-content');
// Duplicate the content to achieve seamless scrolling
this.clone = this.content.cloneNode(true);
this.clone.setAttribute('aria-hidden', 'true');
container.appendChild(this.clone);
this.scrollSpeed = parseFloat(container.getAttribute('data-scroll-speed')) || 100; // px per second
// Current translateX position
this.x = 0;
// Width of original content including left padding (scrollWidth includes padding)
this.contentWidth = this.content.scrollWidth;
this.animate = this.animate.bind(this);
this.isPaused = false;
this.container.addEventListener('mouseenter', () => { this.isPaused = true; });
this.container.addEventListener('mouseleave', () => { this.isPaused = false; });
this.start();
}
start() {
this.lastTimestamp = null;
requestAnimationFrame(this.animate);
}
animate(timestamp) {
if (!this.lastTimestamp) this.lastTimestamp = timestamp;
const elapsed = timestamp - this.lastTimestamp;
this.lastTimestamp = timestamp;
if (!this.isPaused) {
this.x -= (this.scrollSpeed * elapsed) / 1000;
if (this.x <= -this.contentWidth) {
this.x += this.contentWidth;
}
this.content.style.transform = `translateX(${this.x}px)`;
this.clone.style.transform = `translateX(${this.x + this.contentWidth}px)`;
}
requestAnimationFrame(this.animate);
}
}
document.addEventListener('DOMContentLoaded', () => {
const containers = document.querySelectorAll('.new-horizontal-scroll-bar-container');
containers.forEach(container => new NewHorizontalScrollBar(container));
});
})();
</script>
Summary of what has changed:
- Added
padding-left: 100px on .new-horizontal-scroll-bar-content for initial spacing before the first scroll item appears.
- The JS scroll logic stays largely the same but uses
<= -contentWidth and increases by contentWidth to avoid visual jump and create seamless looping.
- Scroll always animates continuously with no jump or flicker.
This will give you a smooth, continuous horizontally scrolling bar with space before first item on initial load. Adjust padding-left as needed to set your desired initial gap.