Is there a way to include a background image on the slider I designed for when the page initially loads?

As I created a slider that changes the background when clicked, I encountered an issue where the initial background is white. How can I incorporate a background image at the start of the page?

My goal is to have a background image on the first page. Subsequently, clicking on a feature icon should trigger a change in the background image:

https://i.sstatic.net/Rr9eA.png

https://i.sstatic.net/WSS1y.jpg

However, after clicking the icon, the intended image swap does not happen. What I desire is for a background image to be displayed when the page is initially opened and then replaced upon clicking the icon.

https://i.sstatic.net/PN83m.jpg


$('.center').slick({
  centerMode: true,
  centerPadding: '60px',
  slidesToShow: 6,
  speed: 300,
  focusOnSelect: true,
  variableWidth: true,
  variableHeight: true,
  adaptiveHeight: true,
  arrows: false,
  responsive: [{
      breakpoint: 768,
      settings: {
        arrows: false,
        centerMode: true,
        centerPadding: '40px',
        slidesToShow: 3
      }
    },
    {
      breakpoint: 480,
      settings: {
        arrows: false,
        centerMode: true,
        centerPadding: '40px',
        slidesToShow: 1
      }
    }
  ]
});

$(document).on('click', '.sg-feature', function() {
  $('.sg-feature-desc').addClass('hidden');
  $(this).find('div').removeClass('hidden');
});

// Event listeners for each clickable image
let gambar1 = document.querySelector('#gambar1');
let gambar2 = document.querySelector('#gambar2');
// Add more event listeners for remaining images...
      
/* CSS styles for the layout */
body {
  // Insert relevant CSS properties
}

.slider {
  // Insert relevant styling
}

.center .slick-center h3 {
  // Modify the style as needed
}

.hidden {
  display: none;
}

// Additional hidden classes for other elements

Answer №1

The main issue arises due to the dynamic loading of #gambarN elements by the Slick library. As a result, attaching an event handler during page load results in the destruction of that event handler.

To resolve this problem, you should bind your logic for updating the background image to both the init and afterChange events. The former is triggered during slider initialization, while the latter operates when the slide changes. Within a single event handler designated for these specific events, you can fetch the URL of the image from a data attribute in the HTML of the respective slide.

Additionally, it's worth noting that utilizing incremental IDs like #gambarN or #newpostN can lead to code complexity. It's advisable to use common classes instead, as illustrated in the provided example below.

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

Instructing Webpack on Locating Static Resources in a Vue Project

Recently, I made the switch to Vue 3 CLI. After setting up a Vue project, I installed webpack and included the following file in my project's base directory: vue.config.js const webpack = require('webpack') module.exports = { configur ...

Basic CSS element placement

Visit my website here. There is a black video box under the product image that I want to align to the left. I tried to adjust the CSS but couldn't find anything causing the issue. When I move the video box above the Facebook button and align it to t ...

Why is Handlebars {{body}} not rendering my HTML tags properly?

I am perplexed by the fact that the example in the other file is not showing. While working on a CRUD project with Mongo, Express, and Node, I encountered an issue. The code wasn't functioning as expected, so I paused to figure out why. <!DOCTYPE ...

What could be causing my Angular Ngrx app's state not to render properly on the application?

Is there a way to automatically render the state when the app loads without having to click a button? I am currently facing an issue where the state is empty until I manually trigger the click function by pressing a button. I have tried using this.store.se ...

Problem with navigating in AngularJs

I am encountering an issue with angularjs routing. My objective is to append different views based on the path. I aim to achieve this by using separate ng-apps within a single HTML document like so: <body> <div ng-app="header" id='header ...

Unable to assign an ID to an event in Angular when the eventReceived event is triggered

I am currently working with the Angular fullcalendar module that includes drag and drop functionality. My goal is to assign an ID to new events dropped on the calendar by users, but I am unsure of the proper way to do this. Here is the Stackblitz link. Ad ...

Setting the selected value of a COREUI multiselect widget

I received the following JSON response from an API. {"first_name":"Person 1","last_name":"Person 1","email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="78081d0a0b17163 ...

Displaying a Div element containing dynamically calculated values based on selected options in Angular

As a newcomer to Angular, I decided to challenge myself by building a simple app. Currently, my select options only display the keys of a data object. What I really want to achieve is to show a value beneath the second select box for each team, which displ ...

Ensuring thoroughness in validation without the use of specific text strings

Implementing the assignment or assertion of never at the end of a function is a strategy commonly used in Typescript to ensure exhaustive checks at compile time. To enable the compiler to recognize this, explicit strings are needed for it to check against ...

creating a ribbon design with CSS

Currently, I am in the process of creating a ribbon using CSS. You can find the page that it is located on HERE Although I realize that there are other areas of my page that need improvement, my main focus right now is on perfecting this ribbon. I am loo ...

Tips for optimizing text retrieval in headless mode with Selenium to avoid unnecessary duplicate runs

My current task involves scraping specific parts of the materials listed on the Zara website. The process I am following includes the following steps: step#1 - opening the website in headless mode step#2 - clicking the "see more" button to reveal the p ...

Vue.js behaving abnormally due to certain circumstances

Currently, I am working on a vue application where I encountered some errors related to script execution on certain websites. By implementing the following code snippet, the issue is resolved: if ( window.location.href === 'chrome-extension:// ...

Methods for incorporating rtl functionality into Angular Chosen

I am currently using Angular with Chosen (source) and I am attempting to implement right-to-left (RTL) support. Here is my demo, but despite referencing resources, I am encountering issues. The main problem arises when the body element has a dir="rtl" att ...

Unable to deactivate button with jQuery in Django

Having some trouble with a simple jQuery function that should disable a Button if the selected dropdown value is blank. Can't figure out why it's not working. Here's the snippet of HTML code: <form action="{% url 'select_controller ...

What is the best way to save a beloved pet for a user who is signed in?

Is there a way to store a favorite pet for a logged-in user using React and Express? I am currently retrieving pets from an API and using PostgreSQL as the database. const favPetDetails = { id, name, media, breeds, location, dista ...

Error: Invalid data type detected in cpvlap stats, resulting in termination

I came across a critical error while reviewing the error statistics file. Fatal error: Unsupported operand types in /home1/bestdail/public_html/cpvlap/cpv_lab_install_files/stats.php on line 338 Here is the specific code from line 338: $totalsRow[0] += ...

What are some best practices for implementing responsive design using CSS @media queries with makeStyles in React.js Material UI?

const useStyles = makeStyles(theme => ({ wrapper: { width: "300px" }, text: { width: "100%" }, button: { width: "100%", marginTop: theme.spacing(1) }, select: { width: "100%", marginTop: theme.spacing(1) } })); I ...

Is there a way I can align these items in the center of the span?

Issue at Hand: The SVG and text elements are not aligning to the center of my spans as desired. Attempts Made: I have experimented with different display modes, used margin properties for alignment, and tried adjusting text-align. I even searched for so ...

When arriving on a page via an HTML anchor tag, the CSS style does not appear. How can I "reinstate" it?

I have set up a website with an index.html page that links to a contact.html page using anchor tags. On the contact.html page, there are anchor tags that should navigate the user back to specific sections of the index.html page. The navigation works fine, ...

Ways to divide different paths within a React Application

In my index.js file, I currently have the following code: <Router routes={routes} /> I want to move the routes section to a separate file. Here's what I've tried so far: routes.js export default ( <div> <Route path= ...