ArticleZip > Very Simple Very Smooth Javascript Marquee

Very Simple Very Smooth Javascript Marquee

Looking to add a touch of dynamism to your website with a smooth scrolling marquee? You're in the right place! Today, we'll walk you through creating a very simple and very smooth JavaScript marquee.

Before we dive into the nitty-gritty of coding, let's quickly understand what a marquee is. A marquee is a scrolling text or images section that moves either horizontally or vertically on a web page. It's a great way to grab your visitors' attention and add some visual interest to your site.

To create a smooth JavaScript marquee, we'll be using basic HTML, CSS, and JavaScript. We'll keep the code simple and easy to understand for beginners.

First things first, let's set up the HTML structure for our marquee. We'll create a container div that will hold our scrolling content:

Html

<div class="marquee">
  <p>This is a very simple and very smooth JavaScript marquee!</p>
</div>

Next up, let's style our marquee using CSS. We'll give it a smooth scrolling effect by using keyframes to animate the text:

Css

.marquee {
  white-space: nowrap;
  overflow: hidden;
}

.marquee p {
  animation: marquee 10s linear infinite;
}

@keyframes marquee {
  0% { transform: translateX(100%); }
  100% { transform: translateX(-100%); }
}

In the CSS code above, we set the `white-space` property to `nowrap` to ensure our text doesn't wrap to the next line. We also hide any overflow to keep our marquee clean. The keyframes animation `marquee` slides the text horizontally infinitely.

Now, let's add the JavaScript part to make our marquee interactive. We'll pause the animation on mouse hover and resume it when the mouse leaves:

Javascript

const marquee = document.querySelector('.marquee');

marquee.addEventListener('mouseenter', () =&gt; {
  marquee.style.animationPlayState = 'paused';
});

marquee.addEventListener('mouseleave', () =&gt; {
  marquee.style.animationPlayState = 'running';
});

With this JavaScript code, we target our marquee element and add event listeners for mouse enter and leave. When the mouse enters the marquee, the animation pauses, and it resumes when the mouse leaves.

And there you have it - your very own very simple and very smooth JavaScript marquee! Feel free to customize the text, styling, and animation duration to fit your website's design.

Remember, it's always a good idea to test your marquee across different browsers to ensure compatibility. If you encounter any issues, check the browser console for error messages that can help troubleshoot the problem.

Now go ahead and impress your website visitors with a sleek and dynamic scrolling marquee that adds that extra flair to your web page! Happy coding!