Sliding right is done by activating the Switch Function, while sliding to the left can be achieved by deactivating it with the help

My objective is to create a functionality where flipping the switch button will cause it to slide automatically to the right, revealing a red background div. When switched off, it should return to its original position with a yellow background using Jquery. Please review my code snippets for a demonstration. Thank you!

.container {
    margin: 0;
    padding: 0;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
/*     overflow: hidden;
    overflow-y: auto; */
    transition: .22s ease left;
    background: #f3faff;
}

.wrapper {
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    overflow: auto;
    text-align: left;
    display: flex;
    align-items: stretch;
    flex-direction: row;
    flex-wrap: nowrap;
    width: 200%;
}

.left-100 {
    background: yellow;
    width: 50%;
    padding: 10px;
}

.right-100 {
    background: red;
    width: 50%;
    padding: 10px;
}


.switch {
     display: inline-block;
     position: relative;
     margin: 0 0 10px;
     font-size: 16px;
     line-height: 24px;
}
 .switch__input {
     position: absolute;
     top: 0;
     left: 0;
     width: 36px;
     height: 20px;
     opacity: 0;
     z-index: 0;
}
 .switch__label {
     display: block;
     padding: 0 0 0 44px;
     cursor: pointer;
}
 .switch__label:before {
     content: '';
     position: absolute;
     top: 5px;
     left: 0;
     width: 36px;
     height: 14px;
     background-color: rgba(0, 0, 0, .26);
     border-radius: 14px;
     z-index: 1;
     transition: background-color 0.28s cubic-bezier(0.4, 0, 0.2, 1);
}
 .switch__label:after {
     content: '';
     position: absolute;
     top: 2px;
     left: 0;
     width: 20px;
     height: 20px;
     background-color: #fff;
     border-radius: 14px;
     box-shadow: 0 2px 2px 0 rgba(0, 0, 0, .14), 0 3px 1px -2px rgba(0, 0, 0, .2), 0 1px 5px 0 rgba(0, 0, 0, .12);
     z-index: 2;
     transition: all 0.28s cubic-bezier(0.4, 0, 0.2, 1);
     transition-property: left, background-color;
}
 .switch__input:checked + .switch__label:before {
     background-color: rgba(63, 81, 181, .5);
}
 .switch__input:checked + .switch__label:after {
     left: 16px;
     background-color: #3f51b5;
}

.switcher-c {
    background: #ffffff;
    width: 100%;
    position: fixed;
    z-index: 11;
    left: 0;
}
    
    .margin-top {
    margin-top: 50px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="switcher-c">
<div class="switch">
        <input type="checkbox" id="switch2" class="switch__input">
        <label for="switch2" class="switch__label">Switch 2</label>
</div>
</div>
<div class="container">
  <div class="wrapper">
    <div class="left-100"><div class="margin-top">Left slide 100% Width</div></div>
    <div class="right-100"><div class="margin-top">Right Slide 100% Width</div></div>
  </div>
</div>

Answer №1

$('#switch2').on('change', function() {
  $('.wrapper').toggleClass('active');
})
.container {
    margin: 0;
    padding: 0;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    overflow: hidden;
    transition: .22s ease left;
    background: #f3faff;
}

.wrapper {
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    overflow: auto;
    text-align: left;
    display: flex;
    align-items: stretch;
    flex-direction: row;
    flex-wrap: nowrap;
    width: 200%;
}
.left-100, .right-100 {
  position: absolute; 
  top: 0;
  bottom: 0;
  transition: all 0.3s ease;
}

.left-100 {
  left: 0;
  right: 50%;
}

.right-100 {
  left: 50%;
  right: 100%;
}

.active .left-100 {
  left: -50%;
  right: 0;
}

.active .right-100 {
  left: 0;
  right: 50%;
}

.left-100 {
    background: yellow;
    width: 50%;
    padding: 10px;
}

.right-100 {
    background: red;
    width: 50%;
    padding: 10px;
}


.switch {
     display: inline-block;
     position: relative;
     margin: 0 0 10px;
     font-size: 16px;
     line-height: 24px;
}
 .switch__input {
     position: absolute;
     top: 0;
     left: 0;
     width: 36px;
     height: 20px;
     opacity: 0;
     z-index: 0;
}
 .switch__label {
     display: block;
     padding: 0 0 0 44px;
     cursor: pointer;
}
 .switch__label:before {
     content: '';
     position: absolute;
     top: 5px;
     left: 0;
     width: 36px;
     height: 14px;
     background-color: rgba(0, 0, 0, .26);
     border-radius: 14px;
     z-index: 1;
     transition: background-color 0.28s cubic-bezier(0.4, 0, 0.2, 1);
}
 .switch__label:after {
     content: '';
     position: absolute;
     top: 2px;
     left: 0;
     width: 20px;
     height: 20px;
     background-color: #fff;
     border-radius: 14px;
     box-shadow: 0 2px 2px 0 rgba(0, 0, 0, .14), 0 3px 1px -2px rgba(0, 0, 0, .2), 0 1px 5px 0 rgba(0, 0, 0, .12);
     z-index: 2;
     transition: all 0.28s cubic-bezier(0.4, 0, 0.2, 1);
     transition-property: left, background-color;
}
 .switch__input:checked + .switch__label:before {
     background-color: rgba(63, 81, 181, .5);
}
 .switch__input:checked + .switch__label:after {
     left: 16px;
     background-color: #3f51b5;
}

.switcher-c {
    background: #ffffff;
    width: 100%;
    position: fixed;
    z-index: 11;
    left: 0;
}
    
.margin-top {
    margin-top: 50px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="switcher-c">
<div class="switch">
        <input type="checkbox" id="switch2" class="switch__input">
        <label for="switch2" class="switch__label">Switch 2</label>
</div>
</div>
<div class="container">
  <div class="wrapper">
    <div class="left-100"><div class="margin-top">Left slide 100% Width</div></div>
    <div class="right-100"><div class="margin-top">Right Slide 100% Width</div></div>
  </div>
</div>

Verify this

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

Tips for formatting numerical output in EJS by rounding the value

I am displaying a value in EJS and I am looking to format it to show fewer decimal places. This is related to the EJS engine on the frontend. Value displayed: 4.333333333333333 I want it to be displayed like this: 4.3 The EJS code snippet used: <p cl ...

"Enhance your website with dynamic uploader forms and AJAX scripts - Railscast #383 shows you

I've successfully implemented the uploader functionality from Railscast #383, but I'm wondering if it's possible to dynamically add and remove the uploader link using ajax? Additionally, I'd need to include the "service" instance id wh ...

Is there a way to apply multiple colors to a h1 tag in HTML?

I need help with adding multicolor to a heading on my website using CSS. I am trying to define colors using the span element with different classes for each text section. However, the colors are not showing up as expected and there seems to be spacing issu ...

Learn the process of utilizing AJAX to upload files in CodeIgniter

I have reviewed several solutions How to upload files using ajax in codeigniter File uploads in codeigniter through ajax but none seem to work with my specific code. I am trying to implement a feature where users can upload various types of files (such ...

How can I use Jquery to loop through each combobox on the page and delete those with a specific class name?

I am currently dealing with a page that contains multiple combo-boxes. Each combo-box has a default option set as empty, which is given the class name 'hide'. This functionality seems to work perfectly in Chrome and Firefox, as the hidden options ...

Refresh the data in a table upon clicking the menu

I am looking to develop an accordion MENU, but unsure if I should use divs or <ul>. When a submenu is clicked, I want to execute a mysql query to update the data in the table next to the menu. I'm not sure of the best approach and whether to uti ...

What are the steps to implementing partial page functionality with the $http service in Angular?

Could someone assist me with executing an operation once a partial page has been successfully loaded using the $http service in Angular? The operation involves checking a checkbox based on the scope value. I have included the source code below: Here i ...

How can you compare input text to the input value attribute using jQuery?

Consider this scenario: the page displays the following input element... <input class="textInput error" id="accountMailaddress" name="user[email]" size="30" type="text" value="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail=" ...

Capturing numerous data points with JavaScript

<span> <label class="label">Color</label> <span class="foo"><input name="Color" value="Blue" class="customs" maxlength="100" type="text"/></span> </span> </span> <span> <label cla ...

Is it possible for XSS attacks to exploit the HREF attribute when used with jQuery?

Burp suite displaying an error message. The application seems to have a potential vulnerability related to DOM-based cross-site scripting. Data is retrieved from the location and passed to jQuery() using the following statement: jQuery(location). ...

Dealing with errors when working with cross-domain JSONP requests in jQuery can be a

My attempts to capture a bad request error using my jquery code have been unsuccessful. $.get('http://example.com/', {}, function () {}, 'jsonp') .done(function () { // never fires }) .fail(function () { // never fires }) .a ...

Creating a personalized design for MUI TextField spin button

Looking to customize the appearance of the up/down spin buttons in MUI TextField. https://i.sstatic.net/DcG66.png Desiring white arrows and a black surrounding area that's slightly larger, akin to this: https://i.sstatic.net/ZxMJw.png I'm aware ...

Guide on implementing JSON auto-complete for dynamically created textboxes in ASP.NET

Struggling to generate dynamic HTML textboxes on my ASPX page with autocomplete functionality? Desperately seeking a solution, I scoured various answers on StackOverflow without success. Even my script for generating new textboxes dynamically failed to del ...

Issue with Jquery firing function during onunload event

I'm having trouble adding a listener to a form in order to trigger an ajax call when the user leaves it. I can't seem to identify any errors in Firefox and nothing is getting logged in the console, indicating that my code might be incorrect. As s ...

Tips for successfully passing an array containing an object to an AJAX action

I am facing an issue with passing an object in AJAX that has a list of properties to my action. The problem is that the list always ends up being null. How can I resolve this? Below is the code snippet: Class Definitions : public class SaveProduct { ...

What is the best way to responsively center an after pseudo-element above its parent?

Looking to create a dynamic tooltip without any fixed widths or constraints on the parent element. The process seems simple enough, but I'm facing an issue with centering the after element due to an existing transform attribute of transform: translat ...

Refreshing/Redrawing/Resizing a panel in jQuery Layout when toggling the visibility of a header or footer

Currently utilizing a standard jQuery layout with north, south, west, east, and center sections as outlined in the documentation. In both the west and center panels, there are header and footer panes included. While everything is functioning correctly, I ...

Is it possible to create a CSS code that targets specific browsers for a particular style to be applied?

My project has external CSS that contains all the styles for my site. The site looks great in Firefox, but it becomes a mess in Internet Explorer (as usual). Is there a way to write CSS so that specific styles only apply to certain browsers? For example, ...

Why do I continue to encounter the error message saying 'jQuery is not defined'?

As a newcomer to the world of jQuery and Visual Studio Environment, I embarked on the journey of creating a circular navigation menu bar following the instructions provided in this link. Despite my efforts, I encountered a hurdle in the head section where ...

Leveraging jQuery event listeners within a Javascript class initialization

Recently delving into OOP in JavaScript, I've been revamping some of my old code to make it more reusable. Instead of leaving them as inline scripts, I decided to encapsulate them within JavaScript classes. Here is an example of one of my classes: ...