Animating the appearance and behavior of radio buttons using CSS transitions and JavaScript

My jQuery code allows me to fetch attributes from the .item element, but I'm having trouble with my CSS transitions not working as intended. To see the desired transition effect, try replacing .item with .item-tile.

How can I make my CSS transitions work harmoniously with my jQuery code?

$(".item-wrap").on("click", ".item", function() {
  var itemColor = $(this)
    .find(".icon")
    .css("background-color");
  $(".category-color").css("background-color", itemColor);
  return false;
});
.item-wrap {
  display: flex;
}

.icon {
  margin: 0px 5px 0px 5px;
  position: relative;
  height: 35px;
  width: 35px;
  border-radius: 50%;
}

.item-wrap .item {
  position: relative;
}

.item-wrap .item .radio-button {
  opacity: 0;
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  left: 0;
  height: 100%;
  width: 100%;
  margin: 0;
  z-index: 999;
  cursor: pointer;
}

.item-wrap .item .item-tile {
  -webkit-transition: -webkit-transform 300ms ease;
  transition: -webkit-transform 300ms ease;
  -o-transition: transform 300ms ease;
  transition: transform 300ms ease;
  transition: transform 300ms ease, -webkit-transform 300ms ease;
}

.item-wrap .item .radio-button:checked+.item-tile {
  border: 2px solid #079ad9;
  -webkit-transform: scale(1.1, 1.1);
  -ms-transform: scale(1.1, 1.1);
  transform: scale(1.1, 1.1);
}

.category-color {
  height: 50px;
  width: 50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="item-wrap">
    <div class="item">
      <input id="clothing" class="radio-button" type="radio" name="radio" />
      <div class="item-tile">
        <div class="icon" style="background-color:blue;">
        </div>
      </div>
    </div>
    <div class="item">
      <input id="elephant" class="radio-button" type="radio" name="radio" />
      <div class="item-tile">
        <div class="icon" style="background-color:red;">
        </div>
      </div>
      <div class="item">
        <input id="fish" class="radio-button" type="radio" name="radio" />
        <div class="item-tile">
          <div class="icon" style="background-color:green;">
          </div>
        </div>
      </div>
    </div>
  </div>
  <div class="copy-container">
    <div class="category-color">
    </div>
  </div>

Answer №1

Replace :checked with :focus.

Main Idea:

The issue in the jQuery code is caused by a false return value, preventing the radio input from being checked by default.

$(".item-wrap").on("click", ".item", function() {
  $("#text-preview").text($(this).find(".item-text").text());
  var path = $(this)
    .find("#item-path")
    .attr("d");
  var pathb = $(this)
    .find("#item-path")
    .attr("d");
  var pathc = $(this)
    .find("#item-path")
    .attr("d");
  // Rest of the jQuery code...
});
.item-wrap {
  // CSS styling for item wrap
}

// Other CSS declarations...

.item-wrap .item .radio-button:focus+.item-tile {
  // CSS styles applied when radio button is focused
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="item-wrap">
    <div class="item">
      <input id="clothing" class="radio-button" type="radio" name="radio" />
      <div class="item-tile">
        <div class="icon" style="background-color:blue;">
          <svg xmlns="http://www.w3.org/2000/svg" height="30" viewBox="0 0 35 35" preserveAspectRatio="xMinYMax meet">
               <!-- SVG content -->
          </svg>
        </div>
        <label for="clothing" class="item-tile-label"><text class="item-text">Clothing</text></label>
      </div>
    </div>
    // Other items with radio buttons
  </div>
</div>


Additionally, the use of return false seems unnecessary and can be removed without affecting the functionality of the code:

$(".item-wrap").on("click", ".item", function() {
  $("#text-preview").text($(this).find(".item-text").text());
  var path = $(this)
    .find("#item-path")
    .attr("d");
  var pathb = $(this)
    .find("#item-path")
    .attr("d");
  var pathc = $(this)
    .find("#item-path")
    .attr("d");
  // Rest of the jQuery code...
});
.item-wrap {
  // CSS styling for item wrap
}

// Other CSS declarations...

.item-wrap .item .radio-button:checked+.item-tile {
  // CSS styles applied when radio button is checked
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="item-wrap">
    <div class="item">
      <input id="clothing" class="radio-button" type="radio" name="radio" />
      <div class="item-tile">
        <div class="icon" style="background-color:blue;">
          <svg xmlns="http://www.w3.org/2000/svg" height="30" viewBox="0 0 35 35" preserveAspectRatio="xMinYMax meet">
          <!-- SVG content -->
          </svg>
        </div>
        <label for="clothing" class="item-tile-label"><text class="item-text">Clothing</text></label>
      </div>
    </div>
    // Include other item elements here...
  </div>
</div>

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

Persistent error function arises from Ajax request in Laravel

Greetings everyone. I'm currently working on my Laravel application and trying to verify the attendance for a specific date, subject, grade in my database table. If the data exists, I have implemented an if statement to display the desired results bas ...

Finding your way to a particular section within a webpage through an external source

Hey there! I'm currently working on creating a link that will direct users to a specific section within my webpage. For example, redirecting them to https://blabla.github.io/my-website. My code is quite straightforward and it functions properly when ...

What is the relationship between three.js transforms and CSS3 3D-transforms?

I am developing a unique open-source tool for exploring and visualizing the complexities of human anatomy. At the center of this tool is a dynamic 'chessboard' that occupies the majority of the screen. As you drag the board, various CSS3 3D-tran ...

Troubleshooting Problem with Uploading Multiple Files to Server Using PHP, MySQL, and

Recently, I completed building a simple website for uploading files to the server and storing their names in a database for search functionality. The site was created following a tutorial available at www.formget.com. However, I encountered an issue when ...

How can one effectively manage a dynamic web element ID and xpath as they evolve?

After generating an XPath for a dynamic web element, I came across two nodes that are strikingly similar. The XPath used is: //*[contains(@id, "gwt-uid-")]/span[2]. Despite my best efforts to distinguish between the two nodes, I have not been successful. A ...

I am having difficulty retrieving all the cssRules for certain pages

Attempting to retrieve all CSS rules using JavaScript has yielded varying results. For instance, on StackOverflow, the code used is: console.log( document.styleSheets[1].href,'rules', document.styleSheets[1].cssRules,'Should be css rules, ...

What is the best way to perfectly center the navbar-nav class element in a menu using Bootstrap 5?

Currently, I am in the process of developing a backend for a novice website while also revamping the front end. My knowledge of frontend development is limited, so I have opted to utilize Bootstrap 5. One of my tasks involves transferring the menu from the ...

Prevent web browsers from interpreting media queries

Creating a responsive website has been challenging, especially when it comes to accommodating IE7. I'm considering turning off ALL media queries for just IE7 while maintaining the styling and layout in desktop mode. Is this possible? ...

Guide to placing an image in a designated position

I am looking to achieve the following scenario: Whenever a user uploads an image, it should appear in one of the smaller boxes on the right. Subsequent image uploads by clicking on the big box should populate the other small boxes on the right. Please refe ...

The navigation menu links that are meant to stay fixed at the top of the page are not functioning

On my website, I implemented Foundation's Magellan sticky menu at the bottom. Strangely, when I view the site in Firefox, the menu functions properly. However, when I try to open it in Google Chrome, the links don't work (and the cursor pointer d ...

Fixed header in a div, allowing content to scroll when hovering over the header

How can I ensure that the header remains fixed at the top while allowing the content to scroll when hovered over? The current issue is that when a user hovers over the header and tries to scroll, the content does not move. body { margin: 0; ...

MaterialUI Box reigns supreme in the realm of background images

In my React component, I have a form that is structured like this: <Box display="flex" justifyContent="center" alignItems="center" style={{ minHeight: "100vh", backgroundColor: "gray", opacity: "0.8" }} > ... </Box> ...

Sharing Iframes across various Components within a Single Page Application (like Youtube)

Did you know that Youtube now lets you minimize the player while browsing the site? It's similar to the functionality on LolEsports.com with Twitch and embedded Youtube players. I'm interested in adding this feature to my own website. Here' ...

Tips for inserting a footer at the bottom of every page during the conversion process from HTML to PDF

Could use some assistance with converting HTML into a PDF report. In particular, I want the footer to always appear at the bottom of the last page, even if there is minimal content on that page. Any suggestions? I've been utilizing the wkhtmltopdf to ...

Are there any similar tools to Graphstream in Java that can be used with HTML5 using canvas and JavaScript?

GraphStream is a revolutionary graph library created in Java that offers Java developers a simple way to visually represent dynamic graphs either in memory, on screen, or in files. Check out this demo video. With GraphStream, you can effectively handle th ...

Tips for inserting a rotated image using CSS

I've created the following code snippet. However, there is a new requirement stating that the image needs to be rotated by 180 degrees. How can I make this happen? #cell { background-image: url("../images/logo.PNG"); background-attachment: fixed; b ...

tips on displaying a div dynamically in a specific location

Currently, I have implemented HTML textBoxes on my website. However, I am looking to validate these textBoxes using JavaScript. Rather than displaying a traditional alert popup for invalid data input, I would like to show a div next to the specific textBox ...

What is the best way to align paragraphs vertically within a required square-shaped div using CSS (with images for reference)?

I am trying to style a table in a specific way, but I'm facing some challenges. First, I want to make the internal div inside the table square with a percentage height. Next, I need to have two paragraphs stacked on top of each other within the same ...

The PHPMailer send method fails to provide a return result

I am encountering an issue with the PHPMailer send function, as it is not returning true or false and therefore causing problems with my code. I have verified that JSON is functioning correctly by testing it with other functions. The problem seems to lie ...

The JQuery(document).ready function does not seem to be executing on the webpage, but it works as expected when placed in a

I have encountered a peculiar problem. It's strange to me because I can't figure out the root cause of it, despite trying everything in the Chrome Developer Tools debugger. Here is a snippet of code that works when I run it from a file on my desk ...