如何为已实现的jQuery Marquee添加悬停暂停功能?
Hey there! Adding hover pause to your jQuery Marquee so users can click links is a smart move—let’s get this sorted with the cleanest, most reliable approach using the plugin’s built-in features.
Optimal Solution: Use the Plugin's Native Pause/Resume Methods
The jquery.marquee plugin you’re already using comes with official pause() and resume() methods—this is the best approach because it leverages the plugin’s internal animation logic, avoiding glitches that can come from custom workarounds.
Modified Code with Hover Pause
Here’s how to update your existing script to add the hover functionality:
<script src="//cdn.jsdelivr.net/jquery.marquee/1.3.1/jquery.marquee.min.js"></script> <script> $(document).ready(function() { $("#marq").fadeIn(); $('.marquee').marquee({ duration: 20000, gap: 50, delayBeforeStart: 2000, direction: 'left', duplicated: true }); // Add hover pause/resume logic here $('.marquee').hover( // Mouse enters: pause the marquee function() { $(this).marquee('pause'); }, // Mouse leaves: resume the marquee function() { $(this).marquee('resume'); } ); }); </script>
Why This Is the Best Approach
- Native Compatibility: Using the plugin’s official methods ensures you won’t break its internal animation cycle—no stuck scrolling or inconsistent behavior like you might get with manual
stop()calls. - Per-Element Control: The
$(this)selector means if you have multiple marquee instances, each will pause/resume independently. - Lightweight & Simple: No extra dependencies or complex code—just a straightforward hover event binding.
Extra UX Tips
- Add a pointer cursor to signal interactivity to users:
.marquee { cursor: pointer; } - For smoother transitions (instead of an immediate pause), you could tweak the
durationtemporarily on hover, but an immediate pause is usually most user-friendly for link clicks. - Test across target browsers to confirm consistency—this plugin is well-maintained, but quick verification never hurts.
内容的提问来源于stack exchange,提问作者Scott O'connor
相关产品推荐
相关产品推荐

