Creating an animated background slide presentation

After creating a button group, I wanted the background of the previous or next button to move/slide to the selected one when the user clicks on it. I achieved this effect using pure CSS and simply adding or removing the 'active' class with jQuery. However, I encountered an issue where clicking on the 'All' button followed by the 'New' works correctly, but selecting 'Used' does not result in the correct slide positioning.

To achieve this effect, I utilized transitions and the ::before pseudo-element, aiming to solve the issue with minimal jQuery or pure CSS without extensive JavaScript or jQuery code.

The moving logic for the background is as follows:

.RadioButton .btn:first-child::before {
 right: 0;
 transition: .3s all ease;
}

.RadioButton .btn:nth-child(2)::before {
 transition: .3s all ease;
}

.RadioButton .btn:last-child::before {
  left: 0;
  transition: .3s all ease;
}

The problem arises with .RadioButton .btn:nth-child(2)::before where using right:0 or left:0 individually causes the slide effect to malfunction in the intended position. Any suggestions for a solution?

My attempted solutions so far include:

$('.RadioButton').each(function(){
$(this).find('.btn').each(function(){
$(this).click(function(){
$(this).parent().find('.btn').removeClass('btn-active');
$(this).addClass('btn-active');
});
});
});
body {
direction: rtl;
}

/* Other CSS styles are provided in the code snippet */

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="RadioButton btn-group" id="searchType">
<a class="btn btn-default" data-val="0">Used</a>
<a class="btn btn-default" data-val="1">New</a>
<a class="btn btn-default btn-active" data-val="2">All</a>
</div>

Answer №1

Place a single after pseudo-element on the last button. Utilize sibling selectors and the active class to adjust its position to the left, as needed. (refer to the last 2 styles in the CSS)

$('.RadioButton').each(function(){
$(this).find('.btn').each(function(){
$(this).click(function(){
$(this).parent().find('.btn').removeClass('btn-active');
$(this).addClass('btn-active');
});
});
});
body {
direction: rtl;
}

.RadioButton .btn {
    width: 33%;
    float: left;
    margin: 0;
    box-sizing: border-box;
    position: relative;
    font-size: 11px;
    min-height: 30px!important;
    line-height: 30px;
    border-radius: 5px;
    cursor: pointer;
    text-align: center;
}

.btn-default {
    border: 1px solid gray;
    color: gray;
}

.btn-group>.btn:first-child {
    border-top-right-radius: 0;
    border-bottom-right-radius: 0;
}

.btn-group>.btn:nth-child(2) {
    border-radius: 0;
}

.btn-group>.btn:last-child {
    border-top-left-radius: 0;
    border-bottom-left-radius: 0;
}
.btn.btn-default.btn-active {
    color: white;
}

.btn-group {
    clear: both;
    margin-top: 10px;
}

.RadioButton .btn:nth-child(3)::before {
    content: "";
    height: 100%;
    width: 100%;
    background: gray;
    position: absolute;
    top: 0;
    transition: .3s all ease;
    z-index: -1;
    left: 0px;
}

.btn-active:first-child ~ .btn::before {
    transform: translateX(calc(-4px - 200%));
}
.btn-active:nth-child(2) ~ .btn::before {
    transform: translateX(calc(-2px - 100%));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="RadioButton btn-group" id="searchType">
<a class="btn btn-default" data-val="0">Used</a>
<a class="btn btn-default" data-val="1">New</a>
<a class="btn btn-default btn-active" data-val="2">All</a>
</div>

Answer №2

modify center position

$('.RadioButton').each(function(){
$(this).find('.btn').each(function(){
$(this).click(function(){
$(this).parent().find('.btn').removeClass('btn-active');
$(this).addClass('btn-active');
});
});
});
body {
direction: rtl;
}

.RadioButton .btn {
    width: 33%;
    float: left;
    margin: 0;
    box-sizing: border-box;
    position: relative;
    font-size: 11px;
    min-height: 30px!important;
    line-height: 30px;
    border-radius: 5px;
    overflow: hidden;
    cursor: pointer;
    text-align: center;
}

.btn-default {
    border: 1px solid gray;
    color: gray;
}

.btn-group>.btn:first-child {
    border-top-right-radius: 0;
    border-bottom-right-radius: 0;
}

.btn-group>.btn:nth-child(2) {
    border-radius: 0;
}

.btn-group>.btn:last-child {
    border-top-left-radius: 0;
    border-bottom-left-radius: 0;
}

.RadioButton .btn::before {
    content: "";
    height: 100%;
    width: 0%;
    background: gray;
    position: absolute;
    top: 0;
    transition: .3s all ease;
    z-index: -1;
    left: 50%;
    transform: translateX(-50%);
}

.btn-active::before {
    width: 100%!important;
}

.RadioButton .btn:nth-child(2)::before {
 
 transition: .3s all ease;
}

.RadioButton .btn:last-child::before {
  
  transition: .3s all ease;
}

.btn-active:first-child::before {
 
 transition: .3s all ease;
}

.btn-active:last-child::before {
  
  transition: .3s all ease;
}

.btn.btn-default.btn-active {
    color: white;
}

.btn-group {
    clear: both;
    margin-top: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="RadioButton btn-group" id="searchType">
<a class="btn btn-default" data-val="0">Used</a>
<a class="btn btn-default" data-val="1">New</a>
<a class="btn btn-default btn-active" data-val="2">All</a>
</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

In the world of web design, blank areas can sometimes appear on a page as you

My website has a fixed header with a height of 44 pixels, content that takes up 89% of the screen, and a footer that occupies 6% of the space. Everything looks fine when the browser is at 100% zoom, but as I zoom out, white spaces start appearing before th ...

Remove the active class after it has been clicked for the second time

Currently, I am working on a menu/submenu system where I want to toggle active classes when clicked. However, I encountered an issue - if the same menu item is clicked twice, I need to remove the active class. Initially, I tried using jQuery's toggleC ...

Limit jQuery script to operate exclusively on a single page

Despite the abundance of answers to similar questions, my lack of JS skills hinders me from implementing them in my specific case. On my WordPress site, I have a script that changes the navigation bar's color when scrolling down to the #startchange CS ...

Updating the Wordpress menu: Get rid of the list items and instead add the class ".current-menu-item" directly to the anchor tag

Is there a way to customize the output of wp_nav_menu(); to display a navigation menu like this: <nav> <a>Menu Item</a> <a class="current-menu-item">Menu Item</a> <a>Menu Item</a> <a>Menu Ite ...

``There Seems to be an Issue with Loading Content in Tabs

Hey! I'm encountering an issue with my jquery tabs. The first tab content loads fine, but when I select another tab, the content doesn't display. Then, when I try to go back to the first tab, it doesn't load either. Here's the HTML cod ...

Tips for boosting the maximum upload size using an Ajax request

I am facing an issue with my file upload form that uses an Ajax request to handle the backend processing. Despite setting up my php.ini to allow larger files of up to 64mb, I encounter a validation error (ZF2) stating "File '' exceeds the defined ...

Stack div on top of div

It seems like I'm facing a simple problem that needs a solution. I have two container divs .topwrap{ position:relative; top:100px; background:#00C; } </i> and .lowerwrap{ position:relative; ...

Create a circular status button that overlays on top of another element using CSS

Looking to enhance my CSS skills! Seeking advice on creating a status button that changes color based on chat availability and positioning it on top of another button. ...

How to delete a specific element from the DOM using its ID in JavaScript

I am facing a challenge in removing an element from the page dynamically without refreshing using JavaScript. The script I have written successfully deletes the record from the database, but I need assistance in removing the container element based on it ...

Encountered an issue with locating the module 'webpack-cli/bin/config-yargs' while attempting to run webpack-dev

Encountering an error while trying to start the webpack dev server with the command provided below. Despite suggestions that it could be due to outdated webpack versions, I am confident that all components are up to date: [email protected] [email ...

Sending an array as a query string

I am trying to send an array to my server using jsonp. Here is an example of the JSON I want to pass: ["something","another_thing",4,{"iam" : "anobject"}] However, I am unsure about how to actually pass an array. I thought it might work like this: some ...

Is it possible to assign unique QFont types to individual Unicode characters?

I have a unique challenge of displaying multiple international scripts within the same string in a QLabel. For example, the text on the QLabel might look like this: چاچي चाची ćāćī (dim. of ćāćā, q.v.), s.f. A paternal aunt (=ćaćī ...

I'm struggling to understand how to insert the JSON response into an HTML element

I am encountering an issue with my code where the response from the API call is not displaying on the HTML page. I'm unsure why it's not showing up even though the page loads successfully. <!DOCTYPE html> <html> <head> <title ...

Displaying the overall count for a bar chart within the tooltip title of a c3js visualization

I have a bar chart that looks similar to the one presented in this example. There are two specific features I am interested in adding to this bar chart: Instead of numeric values, I would like the tooltip title to show the sum of the counts represente ...

Design a basic button that does not adopt any of the CSS attributes from Bootstrap

Currently, my styling is done using Bootstrap.css. However, I am interested in creating a straightforward <button> without incorporating any of the CSS properties from Bootstrap. Specifically for this <button>, I do not want it to inherit any ...

Exploring the world of design with React JS and MUI's diverse styling options

Exploring the various styling options offered by MUI From useTheme, styled, makeStyles to other methods - what sets them apart and how do they differ in use cases? We're looking for a comprehensive breakdown of their unique features, practical appli ...

Several JavaScript functions require a confirmation dialog to be displayed before they can be executed

There are three separate javascript/jquery functions in my code, all triggered by a button click. One function posts to a form handler, another creates a new tab, and the third one sends new data into the tab through an ajax call. These functions are inter ...

Change your favicon dynamically based on the user's online or offline status using Vue

I am currently working on setting up different icons to display when my browser is online (normal logo) and offline (greyed out logo). With Vue JS, I am able to detect the online and offline states, as well as set different favicons accordingly. However, t ...

Eliminating the use of <ul> <li> tag within the validation-summary-errors

I am facing an issue with a website where the error message is displaying in a specific format. <div class="validation-summary-errors"> <span>Password change was unsuccessful. Please correct the errors and try again.</span> <u ...

Unexpected token N error was thrown while using the Jquery .ajax() function

While working on a form submission feature using JQuery .ajax() to save data into a MySQL database, I encountered an error message that says "SyntaxError: Unexpected token N". Can someone please explain what this means and provide guidance on how to fix it ...