Images displayed using jQuery do not fade into view

I have a collection of JSON image URLs that I am using to populate a div with a variety of images. Initially, the images are set at opacity 0 and should gradually fade in through a for loop.

Here is the jQuery code snippet:

$.ajax('http://example.com/images.json').done(function(data) {
    for (var i = 0; i < data.length; i++) {
        var image = data[i];
        $('#imgs').append('<img src="' + image.url + '" class="img-responsive" style="float:left; padding:5px;opacity:0;">');
    }
    var imgs = $('#imgs > img');
    for (var i = 0; i < imgs.length; i++) {
        if (imgs.eq(i).css('opacity') === 0) {
            imgs.eq(i).animate({
                'opacity': '1'
            }, 1000);
        }
    }
});

And here is the HTML structure for the div:

<div class="col-md-4">
    <div class="text-center">
        <h3>Our Image Gallery</h3>
    </div>
    <div id="imgs" class="col-md-12">

    </div>
</div>

Answer №1

There are a couple of things to keep in mind here. Firstly, ensure that you are not attempting to use AJAX between different domains as it may lead to blocking.

Instead of using two loops, consider combining them as shown below:

$.ajax('http://thesabreslicer.site.nfoservers.com/thesabreslicer/dw/json/imgs.json').done(function (data) {
    for (var i = 0; i < data.length; i++) {
        var image = data[i];
        var newImg = $('<img src="' + image.url + '" class="img-responsive" style="float:left; padding:5px;opacity:0;" />');
        $('#imgs').append(newImg);
        newImg.animate({
            'opacity': '1'
        }, 1000);
    }
});

Another point to note is that the duration for the animation is set to 1000 (1 second), which might be too quick to notice changes. Adjust this value to see better outcomes.

Additionally, make sure to confirm that the image has loaded before applying the fade effect. You could include a check like this:

newImg.load(function(){
   newImg.animate({
      'opacity': '1'
   }, 1000);
});

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

Is the iPhone height incorrect using jQuery?

When setting the viewport tag to 480px on an iPhone in portrait mode, the true dimensions of the website are as follows: 480x534px when the top bar is visible 480x594px when the top bar is hidden However, despite setting the viewport size correctly, fun ...

What is the best way to use CSS to connect the tips of shapes for perfect alignment?

In an attempt to utilize CSS3 for creating shapes and aligning them to resemble a certain image, I have successfully crafted a pink square along with 4 gray rectangles. Initially, my approach involved creating separate div elements for each shape and adjus ...

What steps can be taken to design a unique CSS pop-up that triggers upon page load, limited to one appearance per visitor every week

I have the following code snippet that allows me to display a pop-up either by clicking on the link or hovering over it. I am looking to modify it so that the pop-up opens when the page loads and restrict it to open only once per visitor per week. As some ...

What is the best way to change the size of an SVG image

I have exhausted all the methods I could find on stackoverflow and Google, but my SVG is refusing to resize. Would someone kindly shed some light on why this might be happening? <div> <svg viewBox="32 32 32 32" height="100" width="100"> ...

Hide all elements in jQuery that do not have a class assigned

I've implemented a straightforward jQuery script that toggles the "active" class on an li element when it is clicked: $('li').click(function() { $(this).toggleClass('active'); }); My goal is to hide all other li elements in t ...

What are your thoughts on using inline PHP to assign values to JavaScript variables?

Imagine you have a PHP document and need to determine if the request was made with or without query parameters using JavaScript. Since there is no built-in function to extract values from the URL, you may need to resort to some sort of workaround. While it ...

Is it possible to retrieve the values of #select1 and #select2 before initiating the AJAX request?

I am presented with the following snippet of HTML code: <select id="choice1"> <option value="0">Option 0</option> <option value="1">Option 1</option> <option value="2">Option 2</option> <option value="3 ...

Library of User Interface components for React Native applications

As I embark on my journey of building my first major app using React Native, a question comes to mind. Is there a UI framework available for React Native that offers pre-styled components right out of the box? Similar to how Ionic provides a base theme a ...

When a JavaScript/jQuery open window popup triggers the onunload event after the about:blank page has been

Imagine you have a button that triggers a popup using window.open(): <button id = "my-button">Open window</button>​ You also want to detect when this popup window closes. To achieve this, you can use the following code: $('#my-button& ...

Tips for eliminating null elements from an array with the help of jquery

Similar Question: How to delete empty values from an array using JavaScript? I'm looking for a way to eliminate null or empty elements from an array with the use of jquery var clientName= new Array(); clientName[0] = "jack"; clientName[1] = ""; ...

The z-index property does not seem to function properly when used in conjunction

I am experiencing an issue with the z-indexes of two divs: one with default positioning and the other with a fixed position. No matter how I adjust the z-index values, it appears impossible to make the fixed-positioned element go behind the statically pos ...

Transform two fixed elements into dynamic elements using JQuery

Is there a way to replace two static elements in the table <tr class="item-row"> with data from an ajax call instead of just appending to them? Currently, I am using this javascript line to append: $(".item-row:last").after('<tr class="item- ...

Exploring the use of $ in the outcome of a json for jQuery autocomplete functionality

Trying to implement auto complete functionality. Here is the URL I need to read JSON from: http://openscan.addi.dk/2.0/?action=openScan&field=phrase.title&lower=hest&limit=10&outputType=json After receiving the result, I'm attempting ...

Utilizing absolute and relative positioning for setting up a flexible sidebar layout

I'm finding myself a bit puzzled when it comes to using Absolute and Relative positioning. In essence, I have a sidebar with about 4 divs in it. My goal is for div 3 to stay in the same position on every page, even if div 1 or div 2 are missing and c ...

How to prevent href from scrolling without changing the URL using JQUERY?

Is there a way to prevent the browser from changing the vertical position of the page when adding an anchor link without using return false or event.preventDefault()? I have some links that need the #myHREF appended to the URL, but I don't want the p ...

What is the best way to center these icons vertically in my navigation menu using CSS?

My website has a navigation menu with third-party icon libraries (Material/FontAwesome) in use. I am facing an issue where the icons are not aligning vertically with the anchor text. Adjusting the font size of the icon to match the text results in it appe ...

Utilizing the ternary operator in Angular HTML templates

Using the ternary operator in HTML can simplify your code: <div> {{ showStringOneFlag ? 'Display String 1' : 'Display String 2' }} </div> This approach could save you from setting a string variable multiple times in JavaSc ...

Expanding the capabilities of the JQuery submit function

Within my web application, I find myself working with several forms, each possessing its own unique id. To streamline the process, I decided to create a custom handler for the submit event in my JavaScript file: $('#form-name').submit(function(ev ...

Switch between attributes of two divs using jQuery

I am currently utilizing the hidden attribute to control the visibility of the .block div within each .item. However, my goal is to ensure only one block is visible at a time. How can I modify the code so that if one block is already visible and another i ...

What is the best way to achieve the functionality of this ajax jquery using vanilla JavaScript?

I am attempting to replicate this jQuery ajax POST request in Vanilla JS, which currently looks like: $.ajax({ method: 'POST', url: window.location.href + 'email', data: { toEmail: to, fromName: from, ...