The images on the carousel fail to load unless I adjust the window size

After investing countless hours into finding a solution, I am still unable to resolve this issue.

The problem lies within my use of a Carousel and setting the images through a javascript file. Strangely enough, upon loading the page, only the first image appears as intended. The subsequent images (2nd, 3rd....) only show up when I resize the window, after which everything functions properly.

Below is the snippet of HTML code:

(function() {
var type='auto';
  console.log(type);
  for(var i=1; i < 9; i++){
     var newDiv = document.createElement('div');
     newDiv.id = 'sample-image-carousel-element'+i;
     newDiv.className = 'swiper-slide';
     document.getElementById('sample-image-carousal').appendChild(newDiv);
     var newImage = document.createElement('img');
     newImage.id = 'sample-image-carousel-element-image'+i;
     newImage.src = 'assets/img/dc_website/portfolio_images/'+type+'/'+type+'_'+i+'.jpg';
     document.getElementById('sample-image-carousel-element'+i).appendChild(newImage);
  }

})();
.portfolio-details-slider img {
  width: 100%;
}
.portfolio-details-slider{
  margin-top: 20px;
  position: relative;
  width: 12px;
  height: 12px;
  background-color: #fff;
  opacity: 1;
  border: 1px solid #4154f1;
  background-color: #4154f1;
}
.swiper-container{
  margin-left:auto;
  margin-right:auto;
  position:relative;
  overflow:hidden;
  list-style:none;
  padding:0;
  z-index:1
}
.swiper-slide {
  opacity: .3;
}
.swiper-slide img {
  opacity: 0.5;
  transition: 0.3s;
}
.swiper-slide img:hover {
  opacity: 1;
}
<section >
<div class="row gy-4">

    <div class="col-lg-8">
      <div class="portfolio-details-slider swiper-container">
        <div id="sample-image-carousal" class=" swiper-wrapper align-items-center">

        </div>
        <div class="swiper-pagination"></div>
      </div>
    </div>
    </div>
</section>
<script src="assets/vendor/swiper/swiper-bundle.min.js"></script>

Additionally, I am using Swiper bundle CDN's which are crucial to note.

Despite attempting various solutions by adding max-width and height properties, I have not been able to make any progress. Any advice or suggestions on how I can address this issue?

An intriguing discovery is that when I manually add the images to the DOM in HTML, everything functions as expected.

Answer №1

Is there a way to customize CSS based on different screen sizes? Can you display additional images when the window size meets certain criteria?



        @media screen and (max-width: 1680px) {

            html {
                font-size: 12pt;
            }

        }

        @media screen and (max-width: 1280px) {

            html {
                font-size: 11pt;
            }

        }

        @media screen and (max-width: 360px) {

            html {
                font-size: 10pt;
            }

        }

Answer №2

Thanks to some guidance from @Hans Spieß, I realized that the Swiper plugin was initializing the carousel element before the images were even loaded onto the page. This initialization process was happening in a separate js file. When the page was resized, Swiper would reassess and find all the images at that point.

To fix this issue, I simply rearranged my code so that the initialization statement came after the js mentioned earlier in the question. This adjustment helped me achieve the desired outcome!

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

Can a Django form be configured to hide submitted values using display:none?

As I was working on submitting a form in Django with multiple details, I came up with the idea to split it into sections. The first set of details would be hidden initially, and upon clicking the next button, the next set of details would be displayed fo ...

Update various components within a container

I have incorporated a function that automatically loads and refreshes content within a div every 10 seconds. Here is the script: $(function () { var timer, updateContent; function resetTimer() { if (timer) { window.clearTimeout(timer); ...

Having trouble stacking the video iframe over the menu content despite setting the z-index correctly

I can't seem to figure this out. I attempted adjusting the z-index on the iframe element, but it continues to be positioned behind the menu on this page. Lowering the z-index on various sections of the navigation menu hasn't helped either. link ...

Can a VS Code theme extension be designed using JavaScript or TypeScript rather than JSON?

Currently working on a VS Code theme extension, I am interested in exploring the possibility of using JavaScript or TypeScript files instead of a JSON file. The idea of having all the theme information crammed into one massive JSON file feels disorganize ...

Strip excess white space from a complex string using Javascript

I have the following sets of strings: 14/04/22 10:45:20 12.08N 87.65W 15.0 2.9ML Frente a Corinto 14/04/21 11:05:34 12.10N 87.70W 140.0 3.5MC Cerca de Masachapa 14/04/22 09:00:09 12.35N 86.44W 12.4 1.3ML Cerca del volcan Momotombo 14/04/21 23:33:37 1 ...

Creating a request again after a promise has been fulfilled in Angular

I'm struggling to understand a simple concept here. My objective is to retrieve data from a service, which fetches data from an endpoint, and then be able to update that stored data by refreshing the endpoint. .service('myService', function ...

Bring JavaScript Function into Vue's index.html File

Recently in my code files, I noticed the following: function example() { console.log("testing"); } In another file, I have something like this: <head> <script src="../src/example.js" type="text/babel"></sc ...

Refresh the view when the URL is modified

Utilizing angularjs alongside ui-router (using the helper stateHelperProvider) to organize views and controllers on the page. Encountering an issue where the views are not updating as expected. The relevant code snippet config.js app.config(function($h ...

I attempted to include multiple images in my HTML using Vue.js, but encountered an error message stating "illegal invocation"

Here is my HTML code: <form method="POST" v-on:submit.prevent="handleSubmit($event);"> <div class="row"> <div class="col-md-4"> <div class="form-group label-floating"> <label class="control-label">Name</label> <input ...

What is the best way to activate a click event on a dynamically generated input element with the type of 'file'?

I am facing a challenge in dynamically adding an input field of type 'file' to a form. The requirement is to allow the end user to add multiple files to the form, necessitating the use of an array concept. While I am able to inject dynamic elemen ...

Steps to have the initial link redirect to a designated webpage

I am working with tables that have links defined in the HTML. I want to modify the behavior so that the first link opens a different URL, for example: john.com. Is it possible to achieve this? Specifically, I want the link associated with the first name ( ...

Property float does not account for margin values

Currently delving into the world of the semantic-ui framework, I have been working with segments. Here is a snippet of my code: <!DOCTYPE html> <html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery ...

Issue: encountered a write EPIPE error while attempting to transfer a file to FTP via gulp

Whenever I try to deploy some stylesheets to a server using FTP, I encounter an error about 80% of the time. Here's the specific error message: Error: write EPIPE at _errnoException (util.js:1022:11) at WriteWrap.afterWrite [as oncomplete] (net.j ...

How can we trigger a function once the API requests within a for loop have completed?

Within this for loop, a maximum of 'time' iterations are specified. With each iteration, a GET call is made to retrieve data that must be added to the obj object. I am seeking a method to determine when all 3 GETS have been completed along with ...

The arrangement of columns and floating images along with the surrounding white spaces

There are 3 columns, each sized at 33% with images of varying heights inside. When viewed on a device the size of an iPad, I want to change the column sizes to be 50%. However, when I do this, the third column appears in the middle bottom of the top two co ...

Query Selector(jQuery selector) is a powerful tool for

Here is the HTML Table structure: <table class="display" style="width: 1000px;" id="failoverServers"> <thead> <tr> <th class="row_selector_head"> <input type='checkbox' class='select_all_ch ...

What advantages and disadvantages come with using timeouts versus using countdowns in conjunction with an asynchronous content loading call in JavaScript/jQuery?

It seems to me that I may be overcomplicating things with the recursive approach. Wait for 2 seconds before loading the first set of modules: function loadFirstSet() { $('[data-content]:not(.loaded)').each( function() { $(this).load($(thi ...

Is there a way to apply padding to a div element that has a display property set to table-cell?

I am trying to achieve a design where I have an image and a div containing vertically centered text. The div should have padding around it along with a border, similar to this example: https://i.sstatic.net/VlgIY.png Currently, this is the closest I have ...

Guide for displaying information as a grid or table format using Angular 4

I am tasked with handling data from an external microservice that is not always in a standard format. The data is dynamic and can include table data, along with metadata information above the grid. My challenge is to render or identify the table data, disp ...

Unable to activate check box button by clicking on it

Hi there! I am having trouble with a checkbox button on my website. The button appears fine, but for some reason it's not working properly (it doesn't get unchecked when clicked). Can anyone help me fix this issue? I'm still learning HTML an ...