Change the body class depending on the current slide with Cycle2

Welcome fellow developers,

I am in need of a little guidance as I am struggling to find a starting point. Please excuse my lack of working code for this issue.

I have searched far and wide on the web, but I can't determine if the solution to my problem is simple or if it requires some specialized knowledge.


Current Scenario: I have chosen Cycle2 as my slider plugin (and I strongly prefer sticking with it - switching to another slider would be challenging for me). Each slide will be assigned a specific "colour" class along with a 'slide' class.

What I'm Looking For: I want to switch classes on the <body> element based on the active slide's colour class.

Ideas: I haven't decided on a naming convention for the colour classes yet, but I am considering something like .col-[name] where [name] could be yellow, red, etc. So, when the active slide has the class col-yellow, this class should be added to the <body>, allowing me to apply different background styles (CSS) to the body element.

If the active slide changes its colour class to something like col-red, then the <body> class should match accordingly.

I hope this explanation is clear and that someone out there in the Stack Overflow community can assist me with this challenge.


Thank you for taking the time to read through my query!

Answer №1

I recently provided a solution to a similar question not too long ago.

Check out this demonstration: http://jsfiddle.net/kevinPHPkevin/7VzaQ/3/

$('.cycle-slideshow').on('cycle-before', function (event, optionHash, outgoingSlideEl, incomingSlideEl, forwardFlag) {
var bg = $(incomingSlideEl).find('img').css('backgroundColor');
$('body').css('background', bg);
});

I hope this information is helpful.

[edited] I have made changes to the code so that it now assigns the same class to the body as the active slide's class.

Demonstration of edited code: http://jsfiddle.net/kevinPHPkevin/7VzaQ/4/

To clarify my changes - In the initial code snippet, I created a variable named 'bg' which stored the CSS background color of the active slide. I then applied this variable as the background of the body tag. However, as per your request for a class change rather than just a style change, I modified the code to identify and assign the class attribute instead of the CSS background color.

Original 'Var' Snippet:

var bg = $(incomingSlideEl).find('img').css('backgroundColor');
    $('body').css('background', bg);

Modified 'Var' Snippet:

var bg = $(incomingSlideEl).find('img').attr('class');
$('body').attr('class', bg);

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

The ion-content scrolling directive is failing to show any displayed information

Within my Ionic application, I have a very simple HTML structure: <ion-pane> <ion-header-bar class="bar-stable"> <h1 class="title">Ionic Blank Starter</h1> </ion-header-bar> <ion-content> ...

Explore and Conceal Images with Jquery

Currently, I am utilizing jQuery to showcase images prior to uploading. However, when newer files are uploaded, I wish to either remove or conceal the previous upload files. Here is my jQuery script: $(function() { // Allowing multiple image ...

Tips for styling a PHP file using CSS and styling more than one element:1. Begin by creating a

I am currently attempting to style a php file using a linked stylesheet, but I am encountering some difficulties. At the moment, I have the , it will affect either the font or the border.</p> Is there a way to apply multiple styles to elements on a ...

Using JQuery to assign text to labels within web controls is a simple and effective way to enhance user interaction and improve

I have a situation where I need to use jQuery to assign the text "hello" to the label "lbl". When accessing the label "lbl", it should now display the text "hello" instead of the original value "hai". In my aspx.cs file, I have added the following code: ...

How to Customize Bootstrap 4 Form Styles

Looking to customize the appearance of a checkbox in a web framework utilizing bootstrap 4, I attempted to override the custom-control-label class by setting the position property to auto instead of its default value of relative. Although I achieved the ...

The issue with using the jQuery blur() event on a Div element

I am facing an issue with hiding specific popups that are based on divs. When I click outside those divs, they do not hide. Here is a sample code of what I am currently attempting: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "h ...

The Bootstrap 4 navigation bar item unexpectedly jumps below and then wraps around

I am working on creating a navbar using the latest version of Bootstrap 4 (alpha6). <nav role="navigation" class="navbar navbar-light bg-faded"> <a class="navbar-brand" href="/"> <img src="/public/img/logo.png" height="36" alt="myLogo"&g ...

Tips for condensing text using ellipsis and Angular-FlexLayout

I am working on a flex row setup that includes three nested flex rows. Within the second row, I need to display a title that should not be shortened, and a text that should be truncated. Additionally, the first and last row must maintain a fixed width with ...

Spacing between cards using Bootstrap

I have a collection of cards styled with Bootstrap and I am looking to add a left margin. I want the cards to be evenly spaced across the width. Right now, there is excess red on the right side that I want to remove... .azer { background: red; } .car ...

Sending information from a webpage to an application in Express

Seeking guidance on a problem where I'm trying to send a string to the server from a jade view. The value is returning on the frontend, but when attempting to store it in an object, I get [object Object]. I suspect the issue lies around the parameter ...

Using jQuery Ajax in ASP.NET MVC to send arguments from JavaScript via POST requests

I have a controller action in ASP.NET MVC that has the following signature in VB.NET: <HttpPost()> Public Function ClosestCities (ByVal position As MapCoordinate, ByVal citiesCount As UInteger) As JsonResult The MapCordinate class is defined as ...

Does Material-ui v4 display component names within the class attributes of the html DOM?

Upon transitioning from material-ui v3 to v4, it was observed that the react component/function names were included in the class attribute within the HTML. Is this normal? Could this potentially cause issues when trying to override class properties with n ...

PHP dropdown menu is a dynamic feature that provides users

Can anyone help me identify the issue with this code? I am struggling to display the sub menu... I suspect the problem lies in the section where I retrieve the sub menu from the database. However, upon inspection, everything seems correct... It's po ...

Using nested jQuery .each to append content to a specific class

Having trouble with nested .each loops $.each(result.postsViewModel.Posts, function(index, item) { $('#postList').append('<div class="col-lg-4 postItem"><div class="ibox"><div class="ibox-content contact-box">' + ...

Is there a way to update a model value based on a button press in the view?

Currently, I am working on a web project that involves dealing with a large amount of data. Unfortunately, the loading time seems to be taking forever. My plan is to change the strategy by incrementally loading the data when the user clicks a button. In m ...

The absence of a semi-colon in JSLint

I encountered an error message indicating a semicolon is missing, however I am unsure of where to place it. Here is the snippet of code: $('.animation1').delay(350).queue(function(){ $(this).addClass("animate-from-top") }); ...

Is there a way to automatically refresh the woocommerce checkout page every 20 seconds?

I'm currently facing an issue with my woocommerce website. The dilemma I'm encountering is the need to automatically refresh the checkout page every 20 seconds upon clicking the place order button. This requirement stems from a plugin that prov ...

Retrieve JSON data from a PHP script

Hey there everyone, I'm new to working with JSON and I need some help. I have data (an array) that was sent from a PHP file in encoded format, and all I want to do is simply get this data and display an alert with it. The object sent from the PHP fi ...

Ways to enhance the maxDate functionality in a jQuery datepicker

I need to implement a feature where there are two input boxes: in the first box, the user selects a date and in the second box, the date available for selection should be set as 2 days after the selected day. Below is the code snippet that I have been work ...

When external API validation fails, the JavaScript form is submitted

I am working on implementing form validation using an external email validation API. Below is the form tag I am using: <form class="form-horizontal" role="form" onsubmit="return validateForm()" method="post"> Here is a snippet of my JavaScript co ...