Having problems implementing FadeIn/Out effect while transitioning images on list item hover

I have managed to create a menu where the photo changes when hovering over a menu list item. However, I am struggling to add a fade in/out effect for a smooth transition between the photos.

Here is the code:

<div id="menu">
    <img src="menu1.jpg" alt="">
    <ul>
        <li><a data-image="menu1.jpg" href="#">Item 1</a></li>
        <li><a data-image="menu2.jpg" href="#">Another item</a></li>
        <li><a data-image="menu3.jpg" href="#">One more item</a></li>
    </ul>
</div>
<!-- /#menu -->

<script>
    var image = $('#menu').find('img').attr('src');
        $('#menu ul li a').mouseover(function() {
            var currentImage = $(this).attr('data-image');
            $(this).parent().parent().parent().find('img').attr('src', currentImage);

        });
</script>

I attempted to include a fade in/out effect like this but it did not work as expected:

$('#menu ul li a').mouseover(function() {
    $(this).parent().parent().parent().find('img').hide().attr('src', currentImage).fadeIn(2000);
}, function () {
    $(this).parent().parent().parent().find('img').fadeOut(2000,function(){
      $(this).parent().parent().parent().attr('src', currentImage).fadeIn();
    });
});

Answer №1

To implement the image change effect, simply follow these steps:

  • Identify the image using an ID or a class name: id="loadImage"
  • For smooth transitions, initially hide the image and then apply .fadeIn();

Here is how your HTML code should look like:

<div id="menu">
    <ul>
        <li><a data-image="1.jpg" href="#">Item 1</a></li>
        <li><a data-image="2.jpg" href="#">Another item</a></li>
        <li><a data-image="3.jpg" href="#">One more item</a></li>
    </ul>
    <img src="1.jpg" alt="" id="loadImage">
</div>

And here is the corresponding JavaScript code:

$(document).ready(function () {
    $('#menu ul li a').on("mouseover", function() {
        var currentImage = $(this).attr('data-image');
        $("#loadImage").attr('src', currentImage).hide().fadeIn();
    });
});

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

I'm having an issue with my progress bar in ReactJS - it seems to move when I scroll the window instead of

My code includes a progress bar that works fine - it shows a movable progress bar when the window is scrolled. However, I want the progress bar to move when scrolling inside my div, not on the entire window. I am currently using jQuery for this functionali ...

Why does the checked pseudo class seem to be unresponsive?

I have been attempting to customize the appearance of radio buttons, but I am encountering issues with the checked pseudo-class. Interestingly, the hover effect is working perfectly. Although this is just a small section of a larger form, I am focusing o ...

Issue: "Access-Control-Allow-Origin does not allow origin null" error message is returned when attempting to access a PHP file using jQuery's ajax method with dataType text/html selected

Why am I encountering this issue in Chrome when using dataType text and HTML? It seems to work fine if I output JavaScript and set the dataType to script. How can I return non-JavaScript data from a PHP file? Client-side code: $.ajax({ url: ...

Is it possible to have scrollbars on opposite sides of a div?

My table has a multitude of columns, prompting me to enclose it within a div with overflow:auto to facilitate horizontal scrolling. Currently, the scrollbar is situated at the bottom of the div. Is it possible to have two scrollbars present, one at the t ...

Saving HTML table as Excel document but naming the file accordingly

Trying to implement this particular function var exportTableToExcel = (function() { var uri = 'data:application/vnd.ms-excel;base64,' , template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft- ...

Ensure this div adapts to different screen sizes

I have attempted using a width of 100%, but unfortunately, it did not result in a responsive design. To illustrate my point, here is the link to what I am striving to achieve in terms of responsiveness: codepen link div { background:url(https://i.stac ...

Is there a way to use JavaScript/jQuery to randomly resize images every time the page loads

Instead of generating thumbnails for my images, I am interested in displaying the original images on my page. However, I still need to resize these images for optimal viewing. Instead of applying a fixed width of 200px, I would like each image to be displa ...

Accessing a JavaScript variable in another <script> section

I'm facing a challenge where I need to access a JavaScript variable that is declared in one block of an HTML page from another block on the same page. This is crucial for me to be able to halt an AJAX call that is currently ongoing. However, I'm ...

Configuring static files in Django for hosting a website in production mode

I have been attempting to serve my static files from the same production site in a different way. Here are the steps I took: First, I updated the settings.py file with the following: DEBUG = False ALLOWED_HOSTS = ['12.10.100.11', 'localhos ...

Automated line wrapping within a div

My goal is to showcase images within a unique div structure. Currently, the arrangement appears as follows: It seems that the green divs are extending beyond their parent div (the black one). This is how it's structured: <div class="photoView c ...

jqgrid - activate event to determine if rows are checked or unchecked

Currently, I am utilizing the jQuery jqGrid plugin in my project. With the inclusion of the multiselect: true option in the configuration settings, every row now contains a checkbox, allowing for multiple row selections within the grid. However, I am int ...

Tips for preserving order of items in MVC framework

I have been working on organizing my carousel items and I need a sortable list view to help me choose the right format for the site. I managed to make the items sortable using this jQuery command: $(function() { $("#subsortsortable tbody.content") ...

The browser has blocked access to XMLHttpRequest from a specific origin due to the absence of the 'Access-Control-Allow-Origin' header in the requested resource

After developing an Asp.Net Core 3.1 API and deploying it on the server through IIS, everything worked fine when sending GET/POST requests from Postman or a browser. However, I encountered an error with the following code: $.ajax({ type: 'GET' ...

Transmit information from javascript to the server

I am currently working on an ASP.NET MVC web application and have a query: If I have a strongly typed view and I submit it, the object (of the strongly type) will be filled and sent to the server. But what if one of the properties of the strongly typed i ...

Mastering the complexities of Javascript, AJAX, and PHP intertwined

Hello everyone, I'm currently working on a website in Codeigniter and I have a form that I submit using PHP, which works perfectly. However, I also have a small dropdown menu on the side of my page that allows users to jump between pages by clicking o ...

Restore the .selected class name to the HTML table using local storage upon automatic page refresh

I have verified that the local storage is successfully storing the row id using sessionStorage.setItem(rowId, 'true'); My goal is to reapply the CSS class .selected to the row id upon page refresh using The main objective is to ensure that the ...

Webpack is failing to recognize certain CSS files

My development stack includes Vue.js 2.5.15, Webpack 4.12.0, css-loader 0.28.11, ASP.Net Core 2.1 in Visual Studio 2017. Starting with the Visual Studio asp.net core template project for Vue and Typescript, I prefer to have individual small CSS files with ...

Utilize CSS to position an element absolutely above the scrollbar

I'm encountering an issue with CSS absolute positioning, where the element appears above the scrollbar... Here is my code: document.addEventListener("DOMContentLoaded", () => { const h1 = overflow.querySelector("header h1") overflow.ons ...

Update the chosen option of the <select> element that was generated through PHP if there is a parameter value present in the URL

I'm currently working on a code snippet like this: <select name="id" id="id"> <OPTION VALUE="null">Select<OPTION> <? foreach ($genres as $row){ ?> <OPTION VALUE="<? echo $row->id; ?>"><? echo ...

Modify the appearance of a nested div using CSS hover on the main JSX container

Within the material-ui table component, I am dynamically mapping rows and columns. The className is set to clickableRow. The TableRow also includes a hover options div on the right side: const generateTableHoverOptions = () => { if (selected) { ...