Hovering over an element can trigger a dynamic swing animation effect

My objective is to animate an item to swing only when hovered over, utilizing CSS and jQuery, and of course, it should stop swinging when the mouse moves away.

After browsing the web, I came across a code snippet that swings an element when the document is loaded (view complete jsFiddle).

$(document).ready(function() {

    function swing(){
        $('.swing').toggleClass('right');
        setTimeout(swing, 1000);
    }
    swing();
});

I attempted to use the .hover() method, but unfortunately, it did not work as expected. What steps can help me resolve this minor issue?

Answer №1

Here's an example of a similar code snippet:

$(document).ready(function() {
    var timer;
    
    function rotate(){
        $('.rotate').toggleClass('right');
        timer = setTimeout(rotate, 1000);
    }
    
    $(".rotate").hover(function() {
        rotate();
    },
    function() {
        clearTimeout(timer);
    });
});

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

The art of arranging elements on a webpage using HTML and

I designed a rectangle using position: relative; and placed an icon inside it. However, when I added a new div class called .Home, and included an element <h4>Home</h4> with position: relative;, the element appeared outside of the rectangle. Wh ...

When viewing my website on a mobile device, all elements that are supposed to be hidden on the desktop version are displayed properly. However, when I resize the

I have implemented code in my css file to hide specific items from the topbar on my website. When I view the site on my desktop and inspect it with mobile view, the hidden items appear correctly concealed. However, when I access the website on my phone, ...

What is the best way to implement a new search input for my datatable while also enabling the searchHighlight feature?

I'm attempting to implement a search bar for my datatables. I need to hide the default search engine that comes with datatables, so I added a script that I found in a forum. However, when I try to run it in my code, it doesn't work and displays a ...

Issue with Jquery code on Mac OS Chrome - it's functional on Windows 10 Chrome though!

Upon clicking the image, it should hide and the YouTube video plays automatically. This functionality works perfectly on Windows 10 but is not functioning on Mac OS or iOS. I am seeking assistance in resolving this issue. I do not own a MacBook, but my c ...

Determine the total count of files in queue with Uploadify prior to initiating the upload process

When I specify auto: false in the uploadify settings, the upload process will only start when the submit button is clicked. Once the onQueueComplete event is triggered, the form will be submitted. However, if no files are selected, the onQueueComplete even ...

Is it possible for CSS hover to have an impact on multiple divs that share the same class name

There are 5 divs on the page, all with the same class name as shown below: Here is the CSS: .test:hover{ color:red; } And here is the HTML: <div class="test"></div> <div class="test"></div> <div class="test"></div> ...

Move the internal array pointer forward to the next record in order to apply the insertAfter function within the jquery code

As a new jQuery user, I'm attempting to develop a jQuery function using data provided by PHP. My goal is to arrange DIV elements in order of their values using insertAfter method. However, I seem to be encountering some difficulty in advancing the poi ...

Tips for resizing images to fit within a TD cell in an HTML table

Currently, I have an issue with the image display within a TD element. Despite setting a specific width for the TD, the image appears to be wider than intended. Could you offer a solution to this problem? ...

Can Glychicons be utilized in form submission buttons?

While I can easily create buttons with glyphicons using the <a class="btn...", I'm facing an issue when trying to do the same with <input type="submit" class="btn btn-default" value="" />. Instead of displaying the button, it appears blank. ...

What is the best way to toggle the visibility of a div using script with Bootstrap 4's latest display classes?

Upon page load, I have a hidden div using the d-none class from BS4. While I understand how to utilize responsive display classes in BS4, I am encountering an issue with showing or hiding this div via script due to the !important declaration in the CSS. T ...

Concealing the Load More Data Button in ASP.net MVC using Ajax and Javascript

I'm trying to implement a feature in my asp.net mvc5 web application where more records are loaded via ajax when a button is clicked. However, as a newcomer to javascript, I'm struggling to hide the "load more" button when the current page number ...

Can a Highchart display a basic line chart with multiple values plotted against the x-axis?

I am looking to create a Highchart, specifically a basic line chart, that can display multiple values against the x-axis. My goal is to generate a line chart that represents the values (y-axis) over dates (x-axis). The challenge arises when there are mult ...

The Bootstrap Navbar fails to function properly when viewed on mobile devices

As a newcomer to BOOTSTRAP 5 and web development in general, I am currently working on creating a simple website using it. I have added a navigation bar, but when I switch my browser to mobile mode, the dropdown menu doesn't work properly and all the ...

Guidance on Implementing a Delay and FadeIn Effect for AJAX Responses from JSON Iterator

How can I iterate over the following foreach loop with a delay between each item and a fadeIn effect on each? I am debating whether .append() is the most suitable function to use since I want to load a templated div with the class #fan for each person in ...

Maintaining advanced multiple search criteria in jqgrid after grid reload - how can it be achieved?

I have a jqgrid that automatically reloads itself at regular intervals and includes a multiple search feature. Everything is functioning well, except for the fact that when the grid reloads, any previous filtering or searching criteria is lost, causing all ...

swapping images using jquery

I figured out how to change the image by clicking on it, but now I want to create a loop where clicking again will revert back to the original image and then click again to display the new image (only 2 images are needed). <script type="text/javascript ...

Enhance user interactivity on your website by incorporating jQuery and CSS for

<table id="tab"> <tr aaa="one" bbb="ooo"><td>xxx</td><</tr> <tr aaa="two" bbb="one"><td>xxx</td><</tr> <tr aaa="three" bbb="one"><td>xxx</td><</tr> ...

The link-dark bootstrap class fails to have an impact

As a beginner in using bootstrap, I am creating my first simple website. However, I am facing an issue with the footer copyright section where the class "link-dark" is not taking effect. <!-- Copyright --> <div class="container-fluid ...

"Enhance your images with dynamic captions using jQuery

Struggling to create an image caption that only appears when hovering over the specific image? Not sure how to select the correct object? The caption is working, but it's appearing for all images at once. I have attempted to target a single image wit ...

Update the class of the appropriate navigation tab when the corresponding div is scrolled into view

After reading similar questions and doing some research on scrollspy, I don't think it will provide the functionality I need. It seems to only support bootstrap style highlighting. If there is more to it that I'm not aware of, please inform me! ...