I'm wondering why my images aren't fading out correctly

Today, I delved into JQuery and JavaScript for the first time. It's possible that I've made a very basic mistake. Essentially, I have a section in the markup, where JavaScript sets the height to match the viewport. Inside this section, there are 4 divs, each containing an image, with the intention of having them fade out sequentially. I wrote some code but it doesn't seem to be working, and after trying to debug it, I'm still unsure why. Can someone please help me figure out what I'm doing wrong? My goal is to create a simple full-screen slideshow from scratch because all I find online are pre-made templates, and I want to understand how to code it myself.

Here is the HTML

<!DOCTYPE html>
<html>
<head lang="pt-br">
    <meta charset="UTF-8>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!--CSS-->
    <link type="text/css" rel="stylesheet" href="css/reset.css">
    <link type="text/css" rel="stylesheet" href="css/main.css">
    <!--END CSS-->
    <!--SCRIPT-->
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="js/jquery/my_jquery_funcs.js"></script>
    <!--END SCRIPT-->
    <title>Patrick Oliveira</title>
</head>
<body>
<!--HERO COMECA AQUI-->
<section id="hero-slider">
    <div id="slide-1"></div>
    <div id="slide-2"></div>
    <div id="slide-3"></div>
    <div id="slide-4"></div>
</section>
<script>
    realTimeHeight();/*Atualiza em tempo real a altura do slide show*/
</script>
<!--HERO TERMINA AQUI-->
</body>
</html>

The CSS

#hero-slider{
    width: 100%;
}
#slide-1, #slide-2, #slide-3, #slide-4{
    width: 100%;
    height: inherit;
    position: absolute;
}
#slide-1{
    background: url("../imgs/imagem1.jpeg") no-repeat center;
    background-size: cover;
}
#slide-2{
    background: url("../imgs/imagem2.jpg") no-repeat center;
    background-size: cover;
}
#slide-3{
    background: url("../imgs/imagem3.jpg") no-repeat center;
    background-size: cover;
}
#slide-4{
    background: url("../imgs/imagem4.jpg") no-repeat center;
    background-size: cover;
}

And the JavaScript

var i = 4;
$(document).ready(function(){
    $(window).resize(function(){
        realTimeHeight();
    });
    setTimeout(function(){
        fadeItOut("#slide-"+i);
    }, 2500);
});
function fadeItOut(objectID){
    var fadeTime = 2500; /*time to fade out*/
    $("#"+objectID).fadeOut(fadeTime);
    if(i == 1){
        i = 5;
    }
    i--;
}
function realTimeHeight(){
    var altura = $(window).height();
    $("#hero-slider").css("height",altura);
}

P.s: I just want to learn how to make one from scratch, after that I'll start using templates for time saving.

Answer №1

Almost there, just a minor mistake.

itemID actually includes the hash symbol. So when you do '#' + itemID, it becomes ##product-7

$('#' + itemID).slideUp(slideTime);

The correct way is:

$(itemID).slideUp(slideTime);

(See Example)

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

Razor View does not support the activation of Javascript

I've been struggling to get some Javascript to work on a Razor View page, but for some reason it's not being activated. Any help would be greatly appreciated! Just a heads up: The View has a shared layout that loads jQuery & Bootstrap for all vi ...

Guide on executing ajax on hover using a scroll plugin

Previously, I could switch content specific to different IDs on hover over .album without using the plugin to insert ajax content into my scroll div. $('.album').hover(function(){ var id = $(this).attr('id'); ajax request here ...

What could be causing this template literal to display each column on its own row instead of in a

Incorporating Bootstrap 4 on the front end and utilizing a straightforward template literal for string passing. My concern lies in the fact that the columns are being outputted into separate rows, a behavior I find perplexing. c ...

When trying to access document.cookie, an empty string is returned despite the presence of cookies listed in developer tools, and the httpOnly flag is set

There are times when I encounter an empty string while trying to access document.cookie on the login page, despite the following conditions being met: The cookies are visible in the Chrome and Firefox developer tools, The httpOnly flag of the cookie I&ap ...

The table remains visible during an AJAX call

I need assistance with removing a table after successful deletion triggered by an AJAX to PHP call. Below is the function provided: list.php <script type="text/javascript"> function massDelete() { if (!confirm("Are you sure")) ...

How do I determine the dimensions of an object in Three.js?

Seeking advice on determining the size of a Three.js object. I am currently loading various objects from files and wish to find a way to automatically adjust scale or camera position to ensure the entire object fits within the frame. Since different files ...

Troubles with caching on Amazon S3

Just starting out in web development and I'm working on updating HTML and CSS files stored on Amazon S3. Our website's backend is hosted on Heroku, while the front-end is on Amazon S3. Whenever I make changes to a CSS file, I can immediately see ...

Mishap with ShareThis.com Social Media Buttons Hit Counter

Having some issues with the hitcount on my social media share buttons. Whenever I "Share" the page on Facebook, it gets counted as a "Like" when I refresh the page. <span class='st_facebook_hcount' st_title='$videoTitle' displayText ...

Social media strife brewing within my WordPress realms

My wordpress website is full of facebook interactions such as og.watch, fb.login, and others. One example is the usage of: for certain buttons, which includes all.js from facebook. This script is placed in the head section. Furthermore, in the head secti ...

Is there a way to optimize downloading multiple identical images using html, css, jquery, etc.?

My chess board features 64 squares, each with a picture of a board piece on either a light or dark square. To ensure proper formatting, a Clear knight is placed on the board. The design utilizes a div element with a background image (representing light or ...

Utilizing Grails Tag Libraries to Retrieve an Element's Value

I have my own TagLib with a custom tag named boxLink that generates a Remote Link and opens a Modal: Closure boxLink = { attrs, body -> Integer modalId = OwnUtil.getRandomNumber(1000) Map params = attrs.params ? attrs.params : [:] ...

Unique CSS animations that vary before and after

Looking to enhance a navbar with some interactive effects: Upon hovering over an item, I want it to disappear and be replaced by the same text in a different color. After the initial animation, I'm interested in adding another effect like a 360-degr ...

Can specific components of Gatsby.js be integrated into an already existing HTML file?

If I already have an HTML site, is it feasible to import only the necessary parts of Gatsby.js to integrate with a CMS like WordPress? Or do I need to rewrite everything in React? ...

"Duration parameter in jQuery animate() function not working as expected in Firefox

Just starting out with jQuery and I have a class for absolute centering (it's working as intended): .Absolute-Center { margin: auto; position: absolute; top: 0; left: 0; bottom: 0; right: 0; } #logo { background: url(logo.png) no-rep ...

There seems to be an infinite amount of padding between the menu bar and content space on the right side

I am having trouble aligning the menu bar and content side spaces. The issue is that the right side of the menu bar and content space doesn't have any side space. I would like to add some space to them similar to the left-hand side. Below, you'll ...

What is the equivalent of console.log(obj) for retrieving object properties?

When obtaining an object using mongoose, named doc, there is a property owner:{} that appears when doing 1console.log(doc)1. However, this property disappears when using doc.toJSON() or doc.toObject(). How can I retrieve this property without using for(p ...

Necessary Quasar Select Dropdown Class

Looking to set an asterisk (*) next to the Select component in Quasar when it is marked as "required". While I can achieve this with the input component, replicating the same behavior for dropdowns has proved challenging. I attempted using the :deep selec ...

What is the best way to retrieve all key-value pairs of PDF form fields using JavaScript in Acrobat?

I've been on the hunt for a code snippet that can retrieve values for specific fields in a PDF using JavaScript within Acrobat. However, my goal is to capture all fields as key-value pairs in JavaScript. I did try some existing code, but unfortunatel ...

Creating an event listener to conceal a popup with a click

One of the key elements in my project is a popup with the unique identifier of myPopup. The class show is responsible for toggling the display property from none to block, allowing the popup to appear on the screen. As a beginner in using event handlers, ...

What is the best way to wrap a Div element around this appended line of code?

So, I have this jQuery code snippet: $('.object1').append('<a href=" ' + linkURL + ' "> ' + headlineText + '</a>'); With that, a new link is generated on the webpage. I'm now looki ...