Animate a div to swipe left using CSS transition

I am currently designing a drawer that needs to slide from left to right when a button is clicked. I have attempted to achieve this using CSS transitions and jQuery, but unfortunately, it's not working as expected. Here is what I have tried so far:

$(document).ready(function() {
  $(".openUserEditBox").on("click", function() {
    $(".user-drawer").show();
  });
});
.user-drawer {
  position: relative;
  width: 600px;
  top: 50px;
  background-color: #f2f2f2;
  border: 1px solid #bbbbbb;
  min-height: 450px;
  float: right;
  display: none;
  left: 100px;
  transition: left 1s ease-in-out;
}

.user-menu {
  padding: 10px 20px;
  position: absolute;
}

.btn-default {
  background-color: #fff;
  padding: 10px;
  border: 1px solid #bbbbbb;
  width: 150px;
}

.btn-default:hover {
  cursor: pointer;
  box-shadow: inset 0 1px 3px 0 #bbbbbb;
}

.btngroup {
  float: right
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="btngroup">
  <button class="btn-default openUserEditBox">Show Drawer</button>
  <button class="btn-default closeUserEditBox">Hide Drawer</button>
</div>
<div class="userForm">
  <div class="user-drawer">
    <div class="user-menu">
      <h1>Edit User</h1>
    </div>
  </div>
</div>

Answer №1

If you need to animate the left property, avoid using show() and hide(). Instead, manipulate the left property directly in JavaScript.

$(document).ready(function() {
  $(".openUserEditBox").on("click", function() {
    $(".user-drawer")[0].style.left = '0';
  });
  $(".closeUserEditBox").on("click", function() {
    $(".user-drawer")[0].style.left = '602px';
  });
});
.userForm {
  overflow-x: hidden;
}

.user-drawer {
  position: relative;
  width: 600px;
  background-color: #f2f2f2;
  border: 1px solid #bbbbbb;
  min-height: 450px;
  margin-left:auto;
  left: 602px;
  transition:left 1s;
}

.btn-default {
  background-color: #fff;
  padding: 10px;
  border: 1px solid #bbbbbb;
  width: 150px;
}

.btngroup {
  text-align:right;
  margin:8px 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="btngroup">
  <button class="btn-default openUserEditBox">Show Drawer</button>
  <button class="btn-default closeUserEditBox">Hide Drawer</button>
</div>
<div class="userForm">
  <div class="user-drawer">
    <div class="user-menu">
      <h1>Edit User</h1>
    </div>
  </div>
</div>

The floats were removed as they caused issues with the overflow property in the user form. The code should still be functional as is.

Answer №2

I recently utilized absolute positioning to show and hide a section. Additionally, I eliminated the floating property for the section in order to resolve alignment issues.

$('.openUserEditBox').click(function() {
    $(".userForm").removeClass("hide");
    $(".userForm").addClass("show");
});
$('.closeUserEditBox').click(function() {
    $(".userForm").removeClass("show");
    $(".userForm").addClass("hide");
});
* {
    box-sizing: border-box;
    position: relative;
}

body {
    overflow: hidden;
}

.user-drawer {
    position: relative;
    width: 600px;
    top: 50px;
    background-color: #f2f2f2;
    border: 1px solid #bbbbbb;
    min-height: 450px;
}

.user-drawer .active {
    left: 20px;
    transform: translateX(0%);
}

.userForm::after {
    content: "";
    clear: both;
    display: table;
}

.clearfix::after {
    content: "";
    clear: both;
    display: table;
}

.user-menu {
    padding: 10px 20px;
    position: absolute;
}

.btn-default {
    background-color: #fff;
    padding: 10px;
    border: 1px solid #bbbbbb;
    width: 150px;
}

.btn-default:hover {
    cursor: pointer;
    box-shadow: inset 0 1px 3px 0 #bbbbbb;
}

.btngroup {
    text-align: right
}

.userForm {
    position: absolute;
    right: -100%;
    visibility: visible;
    transition: 0.6s;
}

.userForm.show {
    visibility: visible;
    right: 0;
}

.userForm.hide {
    visibility: hidden;
    right: -100%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="btngroup">
    <button class="btn-default openUserEditBox">Show Drawer</button>
    <button class="btn-default closeUserEditBox">Hide Drawer</button>
</div>
<div class="userForm">
    <div class="user-drawer">
        <div class="user-menu">
            <h1>Edit User</h1>
        </div>
    </div>
</div>

Answer №3

To achieve this effect, you need to add and remove classes on click events and apply animations to them.

$(document).ready(function() {
  $(".openUserEditBox").on("click", function() {
       $(".user-drawer").removeClass("slide-out");
       $(".user-drawer").addClass("slide-in");
  });
  $(".closeUserEditBox").on("click", function() {
       $(".user-drawer").removeClass("slide-in");
       $(".user-drawer").addClass("slide-out");
  });
});
.userForm {
  overflow-x: hidden;
}

.user-drawer {
  position: relative;
  width: 600px;
  background-color: #f2f2f2;
  border: 1px solid #bbbbbb;
  min-height: 450px;
  margin-left:auto;
  left: 100%;
}

.btn-default {
  background-color: #fff;
  padding: 10px;
  border: 1px solid #bbbbbb;
  width: 150px;
}

.btngroup {
  text-align:right;
  margin:8px 0;
}

.slide-in {
  animation: slide-in 0.5s forwards;
  -webkit-animation: slide-in 0.5s forwards;
}
.slide-out {
  animation: slide-out 0.5s forwards;
  -webkit-animation: slide-out 0.5s forwards;
}
@-webkit-keyframes slide-in {
  100% {
    left: 0;
  }
}
@keyframes slide-in {
  100% {
    left: 0;
  }
}
@-webkit-keyframes slide-out {
  0% {
    left: 0;
  }
  100% {
    left: 100%;
  }
}
@keyframes slide-out {
  0% {
    left: 0;
  }
  100% {
    left:100%;
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="btngroup">
  <button class="btn-default openUserEditBox">Show Drawer</button>
  <button class="btn-default closeUserEditBox">Hide Drawer</button>
</div>
<div class="userForm">
  <div class="user-drawer">
    <div class="user-menu">
      <h1>Edit User</h1>
    </div>
  </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

Using PhoneGap to load JSON data with the File API when clicking an event

I have successfully developed code that loads JSON data from an external file on the device. The program is designed to work as follows: Upon a click event, it will first attempt to access the local storage file (course.txt) and if it exists, it will read ...

Changing colors using JavaScript: A step-by-step guide

Hey there! I'm looking to change the color code in this script from $("#Tcounter").css("color","black") which uses the color word "black", to "#317D29". Can someone help me figure out how to do this? <script type="text/javascript"> $(document). ...

Why is the 3D CSS transform (translateZ) feature malfunctioning on Android 4.0.3?

I have this HTML code that is functioning well on Chrome, Safari, and Mobile Safari. However, when testing it on Android 4.0.3, the translateZ property does not seem to have any desired effect on the div element. While everything else works as expected, ...

Using jQuery to select a specific checkbox from a group of checkboxes with identical IDs

There is an issue with multiple checkboxes having the same ID in an asp.net repeater control. Users can select either email or phone against each record in the repeater rows. In the example below, there are two rows. If you select the email icon in the fi ...

Looking for a way to integrate a Facebook feed (RSS) into your webpage? Try using this jQuery

I am looking for a quick and easy way to display a Facebook feed (including posts, comments, and images if possible) on a website without using the standard 'like box' provided by Facebook. After some research, I came across this option: Are th ...

Ways to locate two div class elements that are generated dynamically

I am looking to dynamically create 2 divs in different locations. One for displaying information and another for editing information. I want to be able to delete both divs with the same class when using the delete button. Since they are located in differe ...

Upon redirecting to a new page following the loading of a previous page

At the moment, I have a webpage where filling out a text box and clicking a button redirects you to another page. This page must be loaded because it updates and displays XML data (this aspect cannot be changed). However, my goal is to either redirect th ...

CSS Styled Product Index

Currently, I am working on creating a product catalog using React and CSS. Everything is going smoothly except for the last row of products. The issue with the last row is that it only contains 1 element, and due to the flex-grow: 1 setting, it ends up ta ...

Implementing a PHP button update functionality sans utilizing an HTML form

I need a way to update my database with just a click of a button, without using any forms or POST requests. Most examples I've seen involve updating through forms and the $_POST method. Is there a simpler way to update the database by directly click ...

Utilize jQuery to wrap text within <b> tags and separate them with <br> tags

Upon receiving output in html string format from services, I am presented with the following: "<html>↵<h1>↵Example : ↵<br>Explanation↵</h1>↵<hr>↵<b>key1 : ABCD <br>key2 : 2016-10-18-18-38-29<br> ...

What coding techniques can be used to create dynamic animation of a log stream in javascript/css

When I have a series of events in my browser, I want to display them as a list with a smooth animation effect. As each new event comes in, I'd like the existing items to slide down gracefully, making room for the new event to fade in at the top of the ...

Unable to load content: net::ERR_CONTENT_LENGTH_MISMATCH

Can someone help me understand what this error message means and how to fix it? This error is showing up in the console of Google Chrome version 33.0 on a Windows 7 machine. An error occurred while trying to load a resource: net::ERR_CONTENT_LENGTH_MISM ...

When attempting to POST data in Laravel, a status of 419 (unknown) is returned and the data cannot be posted to the target URL

I am attempting to create a DOM event that triggers when a user clicks on a table row's header (th) cell, deleting the corresponding row from the database that populates the table. Previously, my code worked as expected without any framework. I simpl ...

The appearance of HTML in JSP and CSS output in a Spring application is different from the local environment

For my web application's landing page, I created the design using HTML and CSS. Typically, I first design it on scratchpad.io before implementing it into my STS IDE. However, when I run the application, the output of the page appears different from th ...

What is the best way to align <h2> and <img> elements that are set to display as inline-block?

I've been experimenting with aligning h2 and img (icon) elements and although I added display: inline-block, they ended up shifting to the left of the section. Is there a way I can center them instead? HTML: <h2>Coffee</h2> <img src=&q ...

How can the spacing between Angular Material's mat-card-content and mat-card-actions be adjusted?

I am creating a mat card layout where there are two separate cards within the main card's content section. However, when I add a button for the actions section of the main card, there is no spacing between these two sections. How can I create a vertic ...

When the text starts to overlap the column completely at approximately 700 pixels wide

Previously, everything was working fine until it suddenly broke. The column system switches to a 100% width single column at 1500px, but for some reason, the text overflows off the screen around 700px and I can't figure out why. The text at the bottom ...

Dynamic authentication with Ajax in Laravel 5.2

Looking for a solution to send a form through ajax? Check out this CRUD created with "php artisan make:auth." Here's the Controller: protected function createUser(Request $request){ $user = new User; $user->name = $request[' ...

Chrome: Box-shadow not visible on images with a background present

Recently, I have noticed an issue with the box-shadow not displaying on images with a background on my website. This problem started occurring in Chrome a few months ago, despite working perfectly fine around six months ago. To pinpoint the problem, I cre ...

Having issues with the CSS block in my Jekyll dropdown navbar

I have been exploring resources from both w3schools and another website to create a navigation bar in Jekyll using frontmatter. However, I am running into issues with the block property in CSS. Most of the navbar is functioning properly, except for the dro ...