jQuery Variable Function Not Executing Properly

My goal is to create HTML boxes with a hover effect that makes the icon in the box spin while fading out, and another image fades in without spinning. I managed to achieve this, but the code became too lengthy so I attempted to create a function to reduce it. Unfortunately, I haven't been able to find a solution yet. Here is a snippet of the code:

http://jsfiddle.net/SSZVN/

If you only need this part of the code to assist me, here it is:

<div id="info-boxes">
  <div class="box b-one">
    <span class="rotate">
      <center><img src="images/house.png" alt="" title="Stability" class="rotata" /></center>
      <center><img src="images/house-h.png" alt="" title="Stability" class="rotati" /></center>
    </span>
    <h2>Stable & Secure</h2>
    <p>Lorem ipsum dolor sit amet,consectetura
      dipiscing elit. Donec porta diam
      massa. Fusce molestie nisl in posuere 
      fermentum.</p>
    <center><a href="#" class="button-blue">More Info</a></center>
  </div>
  <div class="box b-two">
    <span class="rotate">
      <center><img src="images/note.png" alt="" title="Realiablity" class="rotata" /></center>
      <center><img src="images/note-h.png" alt="" title="Realiablity" class="rotati" /></center>
    </span>
    <h2>Reliable Information</h2>
    <p>Lorem ipsum dolor sit amet,consectetura
      dipiscing elit. Donec porta diam
      massa. Fusce molestie nisl in posuere 
      fermentum.</p>
    <center><a href="#" class="button-green">More Info</a></center>
  </div>
  <div class="box b-thr">
    <span class="rotate">
      <center><img src="images/power.png" alt="" title="Savability" class="rotata" /></center>
      <center><img src="images/power-h.png" alt="" title="Savability" class="rotati" /></center>
    </span>
    <h2>Power Saver</h2>
    <p>Lorem ipsum dolor sit amet,consectetura
      dipiscing elit. Donec porta diam
      massa. Fusce molestie nisl in posuere 
      fermentum.</p>
    <center><a href="#" class="button-red">More Info</a></center>
  </div>
</div>  

Here's the jQuery code:

var spinMe = function(nameSpin) {
    $(nameSpin).hover(
        function() {
            $(this).children('.rotata').fadeOut('slow');
        }, function() {
            $(this).children('.rotata').fadeIn('slow');
        }
    );
    $(nameSpin).hover(
        function() {
            $(this).hasClass('.rotati').fadeIn('slow');
        }, function() {
            $(this).hasClass('.rotati').fadeOut('slow');
        }
    );
};
spinMe('#info-boxes .box');

Answer №1

In my opinion, it would be best to update the object reference.

    $(this).find('img.rotata').fadeOut('slow').fadeIn('slow');

    and

    $(this).find('img.rotati').fadeIn('slow').fadeOut('slow');

However, during the process, I had to remove some of your JS code from your fiddle (which unfortunately wasn't saved), as there were JS errors causing issues with your spinning animation. Once I resolved that problem, I noticed that the references were incorrect for the elements you intended to spin. Therefore, I made the changes as shown above.

Answer №2

Experiment

$(this).find('.spin').each(function () {
  $(this).hide('slow');
});

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

Extending the number of rows in an Angular mat-table beyond the size of the dataSource

Is there a way to display more rows of data in a mat-table than the length of the provided dataSource? For instance, consider the following sample data: people: [ { name: "John", birthday: "01.01.2000", hobbies: ...

What makes NPM thrive while submodules struggle with construction?

My experience with utilizing npm has often involved encountering seemingly meaningless errors - such as Visual Studio projects failing to build and build tools (e.g. python.exe / CL.exe) being unavailable on the command line. For instance, some packages t ...

Suggestions for enforcing type checking on props at compile time in Vue templates?

Vue in conjunction with Typescript enables the specification of types for props, allowing for more robust component development. For example, check out this discussion on Strongly typing props of vue components using composition api and typescript typing s ...

Ionic: Fixed button located at the bottom of a specific ion-slide

I've been creating a series of slides with questions, and the final slide serves as a summary of the previously answered questions. I want to ensure that the submit button is always visible at the bottom of this last slide. However, I've encounte ...

Save the Vue.js array into a SQL column as JSON, and ensure that any empty values are stored

After creating a form to save data in the "users" column of my "pack" table, I encountered an issue. The form data is stored in the "user" variable. When making an axios request, I push the data from the "user" variable into the empty array called "users. ...

The rationale behind implementing jQuery's .one function

Is there a more elegant way to use jQuery's .one event handler for setting up actions on the first click and different actions on subsequent clicks? $('#start').one('click', function(){ alert('one'); // could a ...

Matching request parameters with a JSON object using node.js and express framework

After uncommenting the lines, the code runs smoothly. However, whenever I enable the 'else' statement, I consistently receive a 'not found' message even when there is a match between req.params.code and data.airports[i].code. var exp ...

The MERN application is functioning on the deployed Render site, however it is no longer operational when run locally

After a few months, I decided to revisit an app that was deployed on Render and noticed it was still working fine there. However, when I tried running it locally, I encountered a 500 error response in the developer tools. network error This is what my te ...

Located at the lower section of the parent container

In my flex container, I have two boxes arranged side by side (collapsing into one column on smaller screens). I've ensured that these boxes have the same height when displayed together, as shown in the image below: https://i.sstatic.net/ZjsfW.png No ...

While building with Next.js, a ReferenceError may occur if the sessionStorage is not defined

While using Next.js 13 App router, I encountered an issue with storing the JWT token received upon login in session storage. It all worked smoothly when accessing the token in my page.js pages across different routes as long as the page was a client compon ...

When using Jquery ajax, a newly added input field may lose its focus if it is clicked on

I encountered an issue where the focus on a newly created input element is lost during an ajax update. var options = { url: aURL, type: "POST", dataType: "xml", data: params, ...

Error in React regarding the tri-state checkbox's indeterminate state being null

I am attempting to create a triple-state checkbox in React. The functionality I'm aiming for is that each click on the checkbox will cycle through blank->checked->crossed->blank->.... After doing some research, I stumbled upon a helpful re ...

the figure's styling does not affect the image tag

I am attempting to show HTML text that is stored in a server database. However, the style of the figure tag does not seem to be working correctly and the image is displaying in its original size. <body> <figure style="width:50%;"> ...

Ways to conceal Bootstrap-designed elements behind a fullscreen overlay

I'm attempting to create a transparent layer to conceal the entire content of my page behind it. It works fine without the Bootstrap columns, but as soon as I add the columns for responsive design, it stops working (or maybe I'm just not understa ...

Eliminate any excess margin, padding, or space below the label

The image displays a tag next to an anchor link. CSS for both tags: .btn { zoom: 1; vertical-align: baseline; cursor: pointer; text-align: center; text-decoration: none; -webkit-border-radius: 3px; -moz-border-radius: 3px; ...

Tips on pausing a moving image from left to right and restarting it later

Is there a way to pause the left to right moving image at the end and then restart the loop? I tried utilizing this website link for assistance: https://www.w3schools.com/cssref/tryit.asp?filename=trycss3_animation-delay Unfortunately, I couldn't fi ...

Uploading Images with Ajax

Is it still the same action if I replace Code 1 with Code 2? Code 1 $(document).ready(function(){ var thumb = $('img#thumb'); new AjaxUpload('imageUpload', { action: $('form#newHotnessForm').attr('actio ...

Issue with slideout menu hyperlinks malfunctioning

Currently developing a site at , everything seemed ready for deployment until testing in 320x480 mode on mobile revealed that the links on the slideout menu were not working on any mobile device I tried, regardless of resolution or page. I've tried u ...

Adjust the dimensions of a div element based on the screen size

I'm struggling with adjusting the size of a div responsively based on the surrounding divs. My layout includes buttons on both sides of the page and text in the middle that varies from 0 to 140 characters. What I aim to achieve is for the buttons to ...

Three divs arranged side by side with responsive design for mobile viewing

This code generates 3 divs for display. However, I am looking to optimize them for mobile devices. What steps can I take to accomplish this? Currently, the divs are oriented to the left and are not visible on a mobile device. I would like them to display ...