Is the background image slowly disappearing?

Instead of using the background property for a background image, I have opted for a different style. Here is how I implemented it:

<div id="bg">
    <img id="bgChange" src="image/image.jpg" />
</div>

This is the corresponding CSS code:

#bg {
  position: fixed; 
  top: -50%; 
  left: -50%; 
  width: 200%; 
  height: 200%;
}
#bg > div {
  position: absolute ; 
  top: 0; 
  left: 0; 
  right: 0; 
  bottom: 0; 
  margin: auto; 
  min-width: 50%;
  min-height: 50%;
}

To dynamically change the image source, I used this simple JavaScript function:

var imageRotating = document.getElementById("bgChange");
var i = 0;
setInterval(function imageRotator() {
    imageRotating.setAttribute("src", "image/image" + ++i + ".jpg");
    if (i == 3) { i = -1; }
}, 5000);

Now my question is: How can I incorporate the .fadeOut() method in this script using jQuery? Since I am new to jQuery, I would appreciate a detailed explanation.

Answer №1

Let me show you my approach

HTML

<div id="bg">
    <img src="image/image1.jpg" class="shown" />
    <img src="image/image2.jpg" />
    <img src="image/image3.jpg" />
</div>

CSS:

#bg {
  position: fixed; 
  top: 0; 
  left: 0; 
  width: 200%; 
  height: 200%;
}
#bg img{
  position: fixed; 
  top: 0; 
  left: 0; 
    display: none;
    width: 300px;
    height: 300px;
}
#bg img.shown{
    display: block;
}

JavaScript:

setInterval(function imageRotator() { $("#bg img.shown").fadeOut("slow", function(){ $(this).removeClass("shown"); });

    if ($("#bg img.shown").next("img").length > 0) // if there is an image after this one
    { 
        $("#bg img.shown").next("img").fadeIn("slow", function(){ $(this).addClass("shown"); });
    }
    else
    {
        $("#bg img:first-child").fadeIn("slow", function(){ $(this).addClass("shown"); });
    }
}, 2000);

EDIT:

Made a few small fixes. Here's a working fiddle

http://jsfiddle.net/k40rcpbf/

You had some CSS rules to govern divs within #bg but your HTML didn't have any

Answer №2

Following the setInterval

$( "#bgChange" ).fadeOut( "slow", function() {
  // ...
});

Edit: Let me clarify.

You need to have at least 2 images in your code. Please note that the code provided is untested and is simply meant to inspire you.

<div id='phototab'>
<img class=active_img src="picture1.png">
<img src="picture2.png">
</div>
<script>
function changePhoto(){
 var current_img=$("#phototab .active_img");
 var next_img=curent_img.next();
    curent_img.fadeOut( "slow", function() {
      current_img.removeClass('active_img');
      next_img.addClass('active_img');
    });
}
setInterval('changePhoto()', 1000);

</script>

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

Tips on utilizing the .ashx file for running scripts upon a webpage closing by a user

Is it possible to trigger some vb.net scripts when a user closes a webpage? I have some code but I'm not sure how to implement it. I came across the following instructions, but I'm having trouble using them: You can attach an ajax call to the "b ...

Align the font-awesome icon in the middle of the input box vertically

Is there a way to vertically center the font-awesome icon inside an input box using Bootstrap without setting margins, padding values, or adjusting the height of the input? .inset-0 { top: 0; right: 0; bottom: 0; left: 0; } .inset-y-0 { top: ...

Unused DIV elements in jQueryUI are identified using XSLT

Just a heads-up, I'm new to xslt so please bear with me if this seems like a silly question. In my xsl:template I have created a div element like this: <div id="objectLocked"> This object is locked by another user. Do you want to remove the lo ...

Is OnPush Change Detection failing to detect state changes?

Curious about the issue with the OnPush change detection strategy not functioning properly in this demonstration. My understanding is that OnPush change detection should activate when a property reference changes. To ensure this, a new array must be set e ...

Can Three.js be used to create a compact canvas?

I've successfully implemented a three.js scene on my website where users can drag to rotate an object. However, I don't want the scene to take up the entire webpage. I tried adjusting the field parameters with the following code: renderer.setSiz ...

Cross-browser compatibility issue: Chrome displays image class in HTML CSS, while Firefox does not

I am building a webpage and have encountered an issue with displaying images in different browsers. Specifically, the image appears correctly in Chrome and Safari but doesn't show up in Firefox. By "doesn't show," I mean that the image is not vi ...

Is it possible to implement a single OrbitControls with two cameras in ThreeJS?

Is there a way to link the OrbitControls of two canvases on the same page? For example, if the user zooms in on one canvas, I want the other canvas to also zoom in simultaneously. How could I achieve this synchronization between multiple canvases? ...

Using CSS3 translate will result in children becoming relatively positioned

I am facing an issue with a sidebar that contains 2 divs. <div class="sectionsContainer clearfix"><-- Sidebar --> <div class="leftSection pull-left"> <p>Maor</p> </div> <div class="rightSection pu ...

What is a clever way to monitor the completion of a forEach loop using promises?

I'm new to promises and I'm attempting to use them for the first time. After the completion of the forEach loop, I want to call another function for additional processing. However, when using forEach, the function insertIDBEvents is only printed ...

Updating select dropdown options dynamically with the help of jQuery AJAX

I have a form with two dropdowns for searching purposes. The second dropdown's values are supposed to change based on the selection made in the first dropdown. However, the ajax request is not returning any value from the PHP script. Any suggestions o ...

Slot context remains unclaimed

Context not available... Vue.component("custom-table", { name: 'CustomTable', template: "#custom-table", created: function() { console.log('Created', this.rows); }, mounted: function() { console.log(&a ...

Bring google map markers to life with animation

Is there a way to create an animation like the one shown in this example? I'd like for the map to highlight the corresponding place when a user clicks or hovers over a location in the list. How can I make this happen? I've tried searching on Go ...

Adding rows to a Datatable using an object array in rows.add()

Attempting to refresh my current Datatable by fetching new data using an Ajax function. Despite trying various solutions from other sources, the table does not update as expected. The function being used is shown below: $.ajax({ url: "<?php echo s ...

Basic AngularJS framework with minimal external dependencies

I have been searching for an AngularJS skeleton to set up my project, but it seems like all the skeletons I find online require the installation of numerous dependencies through npm and/or bower. Due to security concerns at the firm where I am working on ...

Why won't the state change when using Angular's ui-router $state.go method?

I have developed a factory that is responsible for detecting state changes and checking if a form has been modified. When the form is dirty, a modal window like a confirmation prompt should appear. However, I am encountering an issue where the $state.go() ...

Are there any solutions for the malfunctioning v8 date parser?

The V8 Date parsing functionality is not functioning properly: > new Date('asd qw 101') Sat Jan 01 101 00:00:00 GMT+0100 (CET) I have attempted to use a delicate regular expression like the following: \d{1,2} (jan|feb|mar|may|jun|jul|a ...

Using a conditional statement in JavaScript, create a mapping between the values in an array and string

I have a dropdown list that I want to populate with options. The functionality of the onchange event is handled by the following code snippet: const handleChange = (event) => { onFilterChange(filterName, event.target.value); } The value of event.ta ...

No JavaScript needed for this CSS framework

I have been tasked with creating a website without the use of JavaScript, following very specific instructions. Furthermore, it must be compatible with iPhone and other mobile devices, while still adhering to the no JavaScript rule. I am open to utilizing ...

Library or theme with CSS that does not require the use of HTML classes

While there are many popular CSS libraries like Bootstrap and Foundation, they all require adding specific classes to HTML tags for styling. However, I find it tedious to add individual classes like .form-control from Bootstrap to every input element on m ...

Having difficulty making changes to specific category fields in Magento 1.6

I've encountered an issue when trying to save specific fields in certain categories while editing them in Magento 1.6. The problem arises when attempting to edit the following fields within these categories: america/mini packages - description ameri ...