Tips for creating smooth transitions between images in a slideshow

I am looking to create a slideshow that displays images with crossfading while simultaneously highlighting radio buttons below it. I also want the slideshow to pause when the mouse hovers over an image. However, I am experiencing issues with the background briefly being visible and the image size increasing when the fadeout class is applied.

HTML:

<div id="s2">
    <img src="F:\destp\Matchball\slideshow-master\slideshow-master\img\forest.jpg" alt="slide" id="slide">

    <div id="rbtns">
        <input type="radio" name="im" id="b1" onclick=changeImage("F:\\destp\\Matchball\\slideshow-master\\slideshow-master\\img\\forest.jpg",this); />
        <input type="radio" name="im" id="b2" onclick=changeImage("F:\\destp\\Matchball\\slideshow-master\\slideshow-master\\img\\desert.jpg",this); />
        <input type="radio" name="im" id="b3" onclick=changeImage("F:\\destp\\Matchball\\slideshow-master\\slideshow-master\\img\\red-velvet-cup.jpg",this); />
        <input type="radio" name="im" id="b4" onclick=changeImage("F:\\destp\\Matchball\\slideshow-master\\slideshow-master\\img\\sea.jpg",this); />
    </div>


    <div id="vwl">
    <a href="#">
    VIEW ALL ARTICLES &gt;
    </a>
    </div>
</div>

CSS:

#slide{

height:100%;
width:auto;
padding-top: 1%;
margin-left: 21%;

opacity: 1;

transition: opacity 1s ease-in-out ;

}

#slide.fadeOut{

  opacity:0;

}

JS:

$(document).ready(function(){
$("#slide").mouseenter(function(){
    clearInterval(setI);
});

$("#slide").mouseleave(function(){
    setI=setInterval(slideImage,3000);
});
});


 images=["F:\\destp\\Matchball\\slideshow-master\\slideshow-
   master\\img\\forest.jpg","F:\\destp\\Matchball\\slideshow-
   master\\slideshow-master\\img\\desert.jpg",
   "F:\\destp\\Matchball\\slideshow-master\\slideshow-master\\img\\red-
   velvet-cup.jpg","F:\\destp\\Matchball\\slideshow-master\\slideshow-
   master\\img\\sea.jpg"];

function changeImage(imgName,obj){

    currentImage=document.getElementById("slide");

    currentImage.className+="fadeOut";

    setTimeout(function(){
        currentImage.src=imgName;
        currentImage.className="";
        $(obj).prop("checked",true);
    },1000);
}

i=0;
function slideImage(){

if(i>images.length-1){
    i=0;
}

radioButtons=document.getElementsByName("im");
changeImage(images[i],radioButtons[i]);


i++;
}

setI=setInterval(slideImage,3000);

Answer №1

If I were to approach it, I would suggest using at least two image containers stacked on top of each other for a cross-fade effect.

It's important to note that this code is not fully optimized and serves as a starting point for you to enhance. Feel free to take it and fine-tune it!

    //JavaScript code can be found above in the block
    
    /* CSS code can be found above in the block */
    
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Image Fader</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>

<div id="s2">
    <div class="slidewrapper">
        <img src="" alt="slide" id="slide0" class="slide">
        <img src="" alt="slide" id="slide1" class="slide">
        <div class="radiobuttons">
            <input type="radio" name="imageRadio" class="radio" id="radio0" data-index="0" />
            <input type="radio" name="imageRadio" class="radio" id="radio1" data-index="1" />
            <input type="radio" name="imageRadio" class="radio" id="radio2" data-index="2" />
            <input type="radio" name="imageRadio" class="radio" id="radio3" data-index="3" />
        </div>
    </div>

    <div class="contentwrapper">
        <div id="vwl">
            <a href="#">
                VIEW ALL ARTICLES &gt;
            </a>
        </div>
    </div>
</div>
</body>
</html>

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

Unable to retrieve data from JSON file using Ajax request

Trying to populate an HTML table with data from an external JSON file is proving to be a challenge. Despite making an AJAX request using pure JavaScript, nothing happens when the "Test" button is clicked. Take a look at the JSON data: { "row":[ { ...

Saving a session variable to the database using ajax technology

If you're like me and struggling to figure out how to add a session variable (username) to a database using ajax, I've been there. My limited knowledge of jQuery and JavaScript made it quite the challenge. The dynamic table I'm working with ...

Modal failing to update with latest information

My webpage dynamically loads data from a database and presents it in divs, each with a "View" button that triggers the method onclick="getdetails(this)". This method successfully creates a modal with the corresponding data. function getdetails(par) { ...

Tips for solving window dependency issues when executing code in node.js

I am in the process of expanding my browser-side library's capabilities to work with node.js. To achieve this, I have implemented the Universal Module Definition (UMD) pattern. While it successfully works with AMD implementation and <script> tag ...

No matter what I do, I can't seem to stop refreshing the page. I've attempted to prevent default, stop propagation, and even tried using

Below is the code I have written for a login page: import { useState } from "react"; import IsEmail from "isemail"; import { useRouter } from "next/router"; import m from "../library/magic-client"; import { useEffect ...

What is the best way to execute a JavaScript script each time a particular webpage is loaded? My browser is Edge and I am utilizing Tampermonkey for this task

Hey there, I know the title might seem confusing but my aim is to utilize tampermonkey to create a userscript that defines a function which I can execute in the console whenever a page (like Discord) loads. The userscript code appears like this: // ==UserS ...

Formatting Anchor Tags Inside a Table Data Element

My existing CSS section is as follows: .leftMemberCol { width:125px; vertical-align:top; padding: 13px; border-width:0px; border-collapse:separate; border-spacing: 10px 10px; text-align:left; background-color:#f2f3ea; } I have a td section (a left sideb ...

Sending data from an input field to a switch statement with dual parameters in a React application

My goal is to pass the state from a specific input field: <FormControl variant="outlined" fullWidth margin="normal"> <InputLabel htmlFor="isEbitda"> EBITDA ...

Changing images dynamically using Javascript when hovering over an element

Looking to create a dynamic image switch effect on hover, where multiple images cycle through when hovered over. Each time the mouse hovers over the image, it should switch to the next in a sequence of 5 images. <img id="switch" src="img1.jpg"> $(& ...

Tips for resolving a top bar on the left side Grid with CSS without it covering the adjacent right side Grid element

I am currently working with material-ui and react, and facing an issue with the positioning of a top bar within a Grid element. The top bar is meant to be fixed in its position, but not at the very top of the page as it is inside a parent Grid element. I ...

Is there a way to use NPM jsdom to determine the number of text lines in an HTML document?

const jsdom = require("jsdom"); const { JSDOM } = jsdom; var htmlContent = "<p>line 1</p><p>another line</p><p>line 3</p><script>//not a line</script><p>last line</p>" let domParser = new JSDOM(ht ...

optimal application of css with jquery

I have a question about using jQuery to set the padding of a class or id. I am able to successfully change the height of an element with this code: $('.menu').css({ height: '90px' }); But now, I need to apply a specific CSS rule in jQ ...

Cheerio's method of extracting data from an HTML table using web scraping

Currently, I am facing an issue with a web scraping project. The specific webpage that I am attempting to scrape looks something like this: <table style="position..."> <thead>..</thead> <tbody id="leaderboard_body"> ...

The line break is being disregarded by jQuery's insertBefore function

In my simple HTML, I have a div with a limited width. Inside are several span tags styled with nowrap. Here is the CSS: .content { width: 50%; margin: 10px; padding: 10px; border: 1px solid black; } .adder { color: blue; cursor: po ...

Unable to initiate the server generated by the express.js generator

Currently, I am trying to set up an Express.js server using their generator. Following the documentation, I was able to successfully create the basic structure. However, when attempting to run the prescribed command (SET DEBUG=transcriptverificationserver: ...

Pull in class definitions from the index.js file within the node_modules directory

In my project, I have the package called "diagram-js" in the node_modules. The file node_modules/diagram-js/lib/model/index.js contains multiple class definitions as shown below: /** * @namespace djs.model */ /** * @memberOf djs.model */ /** * The b ...

jQuery fails to post serialized data to the action method

I'm facing an issue with submitting a form using jQuery and Ajax. My Ajax function looks like this: $('#submitForm').click(function (e) { e.preventDefault(); $.ajax({ url: '/Letter/Create', ...

Changing Text in React Native is Customizable

As a newcomer to react native, I find myself in need of some guidance. I am currently working on implementing a TextInput feature where users can update the text value as they please. For instance, if a user initially creates a folder named "Bar" but later ...

Tips for adding jQuery UI styling to elements generated dynamically

I have a challenge with generating text fields using jquery while applying jquery ui styling to the form. The issue is that the dynamically created elements do not inherit the css styles from jquery ui: let index = 0; $('#btn_generate').on(& ...

Novice in AngularJS routing

Having trouble with my first AngularJS routing code. The error console isn't much help. Here is my HTML page: <body ng-app="myApp"> <div ng-controller="AppController"> <div class="nav"> <ul> <li> ...