Pause the animation when the cursor is hovering over the box, then resume the animation when the cursor moves away

I am working on a project where I have a collection of images that need to be displayed side by side on an html page within a div. The challenge arises when the number of images exceeds the space in the div, causing it to scroll automatically from left to right and vice versa.

My goal is to create an interaction where the scrolling animation stops when the user hovers over the div, and resumes once they move their cursor away. However, I've encountered issues with this functionality as it seems to work inconsistently.

The HTML code below is generated through django:

<div class="row" style="position: relative;">
    <div id='mainDiv' class="col-sm-12" style="">
         <h1">Title</h1><hr>
         <div style="overflow:hidden;white-space: nowrap;">

             {% for img in all_images %}
                 <div class="" style="height:200px;display:inline-block"> 
                 <img height="100" title="" src="{{img.scr}}"> 
                 </div>      
             {% endfor %}

         </div>
    </div>
</div>

JS functionality:

function animateScroll(targetElement, speed) {
            var scrollWidth = $(targetElement).get(0).scrollWidth;
            var clientWidth = $(targetElement).get(0).clientWidth;
            $(targetElement).animate({ scrollLeft: scrollWidth - clientWidth },
            {
                duration: speed,
                complete: function () {
                    targetElement.animate({ scrollLeft: 0 },
                    {
                        duration: speed,
                        complete: function () {
                            animateScroll(targetElement, speed);
                        }
                    });
                }
            });
        };
        
        var animationSpeed = 20000;
        animateScroll($('#mainDiv'), animationSpeed);

        $("#mainDiv").hover(function(){
            $(this).stop(true)
        });
        $("#mainDiv").mouseout(function(){
            animateScroll($(this), animationSpeed);
        });

Answer №1

--- <div id="mainDiv" class="col-sm-12" style="">
+++ <div class="col-sm-12" style="">
---   <div style="overflow:hidden;white-space: nowrap;">
+++   <div id="mainDiv" style="overflow:hidden;white-space: nowrap;">

The mainDiv id tag is within the container component, which is not the correct div for animation.

For a working example, check out this link: https://codepen.io/anon/pen/KxgdWO

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using ESM imports in webpack configuration

I'm in the process of creating a webpack application and I am keen on utilizing ESM (ECMAScript Modules) throughout the entire project. This involves configuring the webpack.config file to allow for ESM imports. Previously, I knew that this could be ...

Tips for customizing the color scheme of Bootstrap 4 drop-down menus

I want to customize the background and font color of the dropdown navigation in Bootstrap 4. Here is the CSS code I tried: .nav.nav-tabs > li.dropdown.active.open > a, .nav.nav-tabs > li.dropdown.active.open > ul.dropdown-menu a:hover, .nav. ...

Can anyone recommend the best method for pinpointing the exact building the user is currently in - utilizing HTML5 geolocation or creating a customized map?

In the process of developing a web application using Google App Engine and Python, I have come across information suggesting that HTML5 geolocation provides more accurate results compared to IP geolocation. However, I am curious if this level of precisio ...

Obtain the content of the text in the row before by pressing the button within the

After researching answers to similar questions, I have been unable to find a solution that works for my specific situation. I am facing an issue with retrieving the text value of a dynamically appended row with the class idRow. The challenge arises when t ...

Convert an array of objects into JSON format

Presented here is an array containing objects: (the array can contain any number of objects...) [Object, Object] 0: Object 0: "2" 1: "Laza Lazic" id_tabele: "2" naziv: "Laza Lazic" __proto__: Object 1: Object 0: "1" 1: "Pera Per ...

What's the reason behind JQuery Trigger method being called three times?

$(document).ready(function(){ $("input").select(function(){ $("input").after(" Text marked!"); }); $("button").click(function(){ $("input").trigger("select"); }); }); <script src="https://ajax.googleapis.com/ajax/libs/jqu ...

What is the proper way to set up state with $stateProvider based on the folder organization in Angular JS?

I am currently utilizing $stateProvider for my routing needs. Below is an overview of my folder structure: app -css -style.css -js -app.js views -home.html -list.html index.html The state code I have implemented is as follows: app.config ...

Using JavaScript to enhance event handling for a `select` element in a progressive manner

I have an advanced <select> element in my HTML. It is structured like this: <ul> <li></li> <li></li> ... </ul> In the current setup, a click event handler is attached to each individual li element. If the ...

Jquery click on image causing mouse position issue (scrolling wrong element)

As a newbie to Jquery, I have encountered an issue with the click handler on an image. Every time the page is scrolled, the position seems to shift depending on how much the page has been scrolled. Is this normal? How can I obtain the relative position of ...

Customizing Fancybox 2 for AJAX Requests

I am faced with a situation where I have 2 files, namely index.htm and inner.htm. My goal is to load a specific div from inner.htm into index.htm using Fancybox 2. After exploring various options, I decided to employ AJAX for this task. However, the issue ...

Displaying and Concealing Elements with AngularJS and Implementing Alternating Styles

Within my collection of div elements showcasing product information, there is a need to display or hide specific fields based on the product type. While this functionality works smoothly, we now aim to enhance readability by implementing alternating row st ...

Is it possible for me to identify the original state of a checkbox when the page first loaded, or the value it was reset to when `reset()` was

When a webpage is loaded, various input elements can be initialized as they are declared in the HTML. If the user modifies some of the input values and then resets the form using reset(), the form goes back to its initially loaded state. I'm curious, ...

The appearance of responsive CSS is altered when deployed compared to when it is viewed on the

I'm a beginner in web development and facing an issue with my mobile CSS. The strange thing is that when I view it on localhost, everything looks great, but once deployed (tried both Heroku and GitHub), it appears distorted. Even when I make extreme c ...

What steps should be taken to ensure that the frame buffer is fully written to before retrieving the FBO texture?

Looking at the code in the Three.js RTT sample, it appears like this: renderer.render( sceneRTT, cameraRTT, rtTexture, true ); // Render full screen quad with generated texture renderer.render( sceneScreen, cameraRTT ); Is there a way to ensure that the ...

Showcasing diverse content with an Angular Dropdown Menu

I'm currently developing an angular application, and I've encountered a difficulty in displaying the user's selection from a dropdown menu. To elaborate, when a user selects a state like Texas, I want to show information such as the period, ...

Passing data through events from a parent component to a child component in Vue.js using the $emit method

Having trouble making this work! I have included the $emit code in my click event to trigger a custom event. <h2 class="form_section--header">Business Hours - <a href="javascript:void(0)" v-on:click="$emit('addBusinessHours', null)"> ...

Choosing Select2: Customizing the context of formatSelection

I've created a simple custom wrapper for Select2, which has been very helpful. However, I am facing an issue with the formatSelection field. When initializing Select2 through my wrapper, it looks like this: this.elem.select2({ allowClear : option ...

Browsers are failing to display any content during ajax calls

I am currently attempting to extract artist URLs from the following page: https://myspace.com/discover/artists?genreId=1002532 However, this page is making an AJAX call to retrieve user details. I have identified the URL in Firebug as: https://myspace.c ...

Comparing the benefits of using pure() versus React.PureComponent

Can someone help me understand the distinction between the pure() function in the Recompose library and React.PureComponent? It seems like they both aim to tackle a similar issue. Any insights would be appreciated. ...

Lazy Load immediately loads images that are visible on the screen without needing a click

I am facing an issue with Lazy Load on my image-heavy website. I want the images to load only when a button is clicked, but currently, it only partially works. Images below the fold follow the desired behavior of loading on click, but those above the fold ...