Switch the background image to a new one when the checkbox is selected

I am currently working on implementing a fading animation that transitions between background images of a div when a checkbox is checked.

You can view a live example here

Here is my jQuery code:

$(function(){
$('#check').on('change', function(){
if ($(this).is(':checked')) 
{
   $(".icons")
   .css({
       'background-image': 'url(/files/images/iconsfilled.svg)' });
    }
    else $(".icons").css({'background':''});
   });
});

And this is my CSS:

.icons {
background: url('/files/images/icons.svg') no-repeat;
margin-left: 32px;
margin-right: 32px;
margin-top: 32px;
margin-bottom: 32px;
}
.basketball {
background-position: -52px 0;
width: 50px;
height: 50px;
}
.mail {
background-position: 0 0;
width: 50px;
height: 54px;
 }

Lastly, here is the HTML code I am using:

<div class="center iconbox">
            <ul class="nostyle inline">
                <li><div class="icons mail wow slideInDown" data-wow-delay="1.4s" data-wow-duration=".8s"></div></li>
                <li><div class="icons basketball wow slideInDown" data-wow-delay="1.2s" data-wow-duration=".8s"></div></li>
                <li><div class="icons document wow slideInDown" data-wow-delay="1s" data-wow-duration=".8s"></div></li>
                <li><div class="icons watch wow slideInDown" data-wow-delay=".8s" data-wow-duration=".8s"></div></li>
                <li><div class="icons weather wow slideInDown" data-wow-delay=".6s" data-wow-duration=".8s"></div></li>
                <li><div class="icons smiley wow slideInDown" data-wow-delay=".4s" data-wow-duration=".8s"></div></li>
            </ul>
        </div>

Answer №1

Method 1: Implementing CSS 3

To add smooth transitions to your elements, consider adding a transition property in the .icons CSS rule.

For example:

.icons {
background: red;
margin-left: 32px;
margin-right: 32px;
margin-top: 32px;
margin-bottom: 32px;
transition: 0.5s;
}

View an Example on jsFiddle

Method 2: Utilizing jQuery

$(function(){
    $('#check').on('change', function(){
    if ($(this).is(':checked')) 
    {
        $('.icons').fadeTo('slow', 0.3, function(){
        $(this).css({'background': 'blue' });
        }).fadeTo('slow', 1);
    }
        else $(".icons").css({'background':''});
    });

});

See a Sample Using jQuery

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

Div that automatically expands

Currently facing a bit of an issue. I’m attempting to expand and retract the height of #inner upon clicking, with the intention for the height of #container to increase automatically without manually setting it to ‘auto’. Unfortunately, it seems that ...

Generating a vertical bar adjacent to a bullet point in a list

Is there a way to add a vertical line next to each list item (except the last) without adding a new div? Adding a border line added an extra one that I didn't want. Sample HTML: <ul class="list"> <li><span id = "home">H ...

Why does JQuery AJAX require a nested DIV to function properly?

Consider the index.php file below : <!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-latest.min.js"></script> </head> <body> <form id="MY_FORM" action="./index.php" method="get" ...

Adjust the dimensions of a specific image in a lineup

I'm currently in the process of developing a website and I've encountered an issue with lists. Specifically, I'm trying to make one particular image within the list slightly larger as it appears smaller compared to the others. For visual re ...

I am experiencing difficulty with the modal box not opening

I am attempting to create a CSS/HTML modal without using JavaScript. Below is the code snippet I have been working with. Despite indicating the link, the modal does not open as expected. I have included 3 CSS files, integrating them into bootstrap.css an ...

Accessing ASP control within a web service method

Recently, I came across some ASP 2.0 webforms code that utilizes a webmethod to query data and return a string. A simplified version of the code looks something like this: Code Behind <System.Web.Services.WebMethod()> _ Public Function StockLevel() ...

The offspring selector and the after pseudo-element

So, I have this CSS snippet here: .nav-item > .nav-link:not(.active)::after { content: "test"; display: block; width: 0; border-bottom: solid 2px #019fb6; transition: width .3s; } But now, I want to try something different: .nav-ite ...

Retrieving a value from an Ajax response using JQuery

Utilizing Ajax to trigger a PHP script which then sends back an array of data. When implementing the following code: .done(function( response ) { if(response === false || response === 'false') { $('.main-container').css(&ap ...

Tips for including dynamic textbox input into a database using the combination of Ajax and PHP

I am facing an issue with a dynamic textbox inside my bootstrap modal. It only saves the first item entered, but when using the native submit without Ajax, it captures all data. I am unable to identify the problem in my Ajax code. My goal is to insert al ...

modify the content of a select box using jQuery

Recently, I encountered an issue with a jQuery modal that is retrieving content dynamically through an onClick event and utilizing the data attributes. My goal is to update the text inside a <select></select> element to display a dynamic email ...

Customize your application with Kendo UI localization features

Is there a more efficient way to localize a front-end application that utilizes a framework like Kendo UI? For example, you can start by dynamically changing text using JavaScript: $(document).ready(function(){ $("#myText").html(<grab text based on ...

Make sure to link the hover effects of .content and img together, allowing them to both activate simultaneously

I'm attempting to synchronize the activation of these two hover classes: #recent-posts .content:hover { -webkit-box-shadow: 0px 0px 10px 3px rgba(0,0,0,0.04); -moz-box-shadow: 0px 0px 10px 3px rgba(0,0,0,0.04); box-shadow: 0px 0px 10px 3p ...

Is it possible to enable auto-completion for IDs in a separate CSS file using WebStorm 7?

I am new to using WebStorm 7 and currently working on building a simple HTML/CSS website. Initially, I included my CSS within the HTML file using the style tag, but now I have decided to move it to a separate file. Auto completion is functioning for all h ...

The jQuery script tag fails to recognize click events once dynamically loaded with the load event

Hey there, I recently utilized this script in a SAP WebDynpro setup to dynamically load and employ jQuery. The onload event of the script tag is functioning well as I can select the text of the focused element. However, I am facing issues with registering ...

Safari displays text in a noticeably bigger font size compared to other browsers

I'm currently working on a website and have noticed that the fonts appear larger on Mac's Safari compared to other browsers. We are using the 'Open Sans' font from Google Fonts for our website. For example, here is a CSS snippet for ti ...

Identify changes in a select element using JQuery

I have a Jqgrid that is capable of dynamically generating selects similar to the following: <select id="win" style="width: 65px;"> <option value="">-WIN?</option> <option value="1&qu ...

Error: The reference to 'ko' is not defined while loading with jQuery

After numerous attempts, I am still encountering the ReferenceError: ko is not defined issue while trying to load KnockoutJS using jQuery's getScript function. Below is the code snippet I have been testing to see if everything is functioning correctl ...

AJAX: Unable to retrieve an Associative array from the database

In my HTML form, I have two combo boxes that need to be dynamically populated from a database using jQuery and PHP. Whenever one of the combo boxes is changed, I trigger a function in jQuery which sends the selected value to a PHP page to fetch data from t ...

AngularJS Treeview with Vertical Line Styling using CSS

Struggling with styling, I am attempting to create a tree view in AngularJS. Currently, I am facing an issue aligning vertical and horizontal lines for the tree items. Check out my Codepen for reference. <html lang="en"> <head> <meta cha ...

Preventing simultaneous ajax requests

Similar Question: How to stop a jquery.load() request? So I'm dealing with an autocomplete field that's sending an ajax request every time a key is pressed, causing significant slowdown. If you check out this link and log in with the details ...