Animated CSS sidemenu (utilized as a filtering panel for a table)

Hi there, I'm having some trouble with CSS Animation. I recently started developing websites and am using Bootstrap 4 along with Animate.css for animations. My goal is to have an icon button expand sideways to reveal a div containing select elements for table filtering (I am using datatables for this). However, I can't seem to figure out how to trigger the animation from the icon button itself. I also want the select buttons to be evenly spaced and vertically centered, with the button on the right side. I tried using display:flex for this layout, but float-right doesn't work as expected.

Unfortunately, I couldn't get Animate.css to work properly in my jsfiddle example. Here's the link: https://jsfiddle.net/z4yvp0gL/8/

HTML:

<div>
  <div class="row button-container">
    <div class="col" style="display: flex;">
      <div id="sideExpand"><select>
          <optgroup label="This is a group">
            <option value="12" selected>This is item 1</option>
            <option value="13">This is item 2</option>
            <option value="14">This is item 3</option>
          </optgroup>
        </select><select>
          <optgroup label="This is a group">
            <option value="12" selected>This is item 1</option>
            <option value="13">This is item 2</option>
            <option value="14">This is item 3</option>
          </optgroup>
        </select><select>
          <optgroup label="This is a group">
            <option value="12" selected>This is item 1</option>
            <option value="13">This is item 2</option>
            <option value="14">This is item 3</option>
          </optgroup>
        </select></div>
      <button class="btn btn btn-transparent btn-icon" id="filterToggle" onclick="SetActiveDiv('#sideExpand',this)"><i class="fa fa-filter" style="position: absolute;font-size: 150%;"></i></button>
    </div>
  </div>
</div>

CSS

#sideExpand {
  display: none;
  width: 100%;
  border-radius: 35px;
  background-color: rgba(31, 45, 65, 0.125);
}
select {
  border: 0;
  background-color: initial;
  font-size: 100%;
  display: block;
  margin: auto;
  word-wrap: normal;
}
.btn-icon {
  padding: 0;
  display: -webkit-inline-box;
  display: inline-flex;
  -webkit-box-align: center;
  align-items: center;
  -webkit-box-pack: center;
  justify-content: center;
  overflow: hidden;
  border-radius: 100%;
  flex-shrink: 0;
  height: calc( (1rem * 1.5) + (0.5rem * 2) + (2px) ) !important;
  width: calc( (1rem * 1.5) + (0.5rem * 2) + (2px) ) !important;
}

Javascript

function SetActiveDiv(ID, bt) {
  var element = document.querySelector(ID);
  if (element !== null) {
    if (element.classList.contains("show")) {
      element.classList.remove("show");
      animateCSS(element, "fadeOutLeftBig", "2s")
      animateCSS(bt, "fadeOutLeftBig", "2s")
      return setTimeout(() => {
        element.style.display = "none";
      }, 2000);
    } else {
      element.style.display = "flex";
      element.classList.add("show");
      animateCSS(element, "fadeInLeftBig", "2s").then((message) => {});
      return animateCSS(bt, "fadeInLeftBig", "2s").then((message) => {});
    }
  }
  return console.log(ID, " not found!");
}

Answer №1

Creating Smooth Transition Animations Using CSS

Allow me to walk you through the process of adding transition animations with CSS. By understanding this example, you can kickstart your learning journey or deepen your existing knowledge.

When it comes to animating in CSS, there are two main methods: using the Animate property or the Transition property. In this demonstration, I opted for the transition property due to its simplicity and performance advantages.

Transition: Specify the property to be transitioned, duration, and an animation timing curve (Bézier curve).

The code includes detailed comments explaining each part's purpose.

Demo: https://jsfiddle.net/hexzero/9da0q3eh/

function ToggleActiveElement(ID, btn) {
  var element = document.getElementById(ID);
  if (element) {
    element.classList.toggle("slide"); // Toggling classes triggers the animation
    return true;
  }
  return console.log(ID, " not found!");
}
#sideExpand {
  display:flex;
  width: 100%;
  height: calc((1rem * 1.5) + (0.5rem * 2) + (2px));
  border-radius: 35px;
  background-color: rgba(31, 45, 65, 0.125);
  transition: transform 1s ease-in; // Define animated property and transition settings
  transform: translateX(100%); // Initial element position

Note: Initially, the element is positioned 100% outwards, towards the right side as per document flow.

z-index: -1; // Lower z-index places elements at the bottom of the stack

#sideExpand.slide { 
  transform: translateX(0%); // Target position after animation
}

.filter-container {
  padding: .3rem;
  display: flex;
  justify-content: flex-end;
  overflow: hidden; // Hides overflowing content, currently at 100% offset to the right
}

#filterToggle {
  background: white;
  font-size: 150%;
  border: .2rem solid #c1c1c1;
}

#filterToggle::after {
  position:absolute;
  content: "";
  height: calc((1rem * 1.5) + (0.5rem * 2) + (2px));
  width: 4rem;
  background:white;
  right: -2rem ;
  z-index: -1;
}

If you discover any discrepancies or have suggestions for enhancing these explanations, please don't hesitate to share your feedback. Thank you!

Answer №2

To incorporate CSS animations, you can follow these steps:

LATEST UPDATE: The button has been adjusted to the left side.

function ToggleDivVisibility() {
  var targetElement = document.getElementById("sideExpand");
  targetElement.classList.toggle("show");
}
#sideExpand {
  display: flex;
  width: 100%;
  border-radius: 35px;
  padding: 10px 10px 10px 48px;
  justify-content: space-between;
  transition: transform .3s ease;
  transform: translateX(-105%);
  background-color: rgba(31, 45, 65, 0.125);
}

#sideExpand.show {
  transform: translateX(0) !important;
}

#sideExpand select {
  border: 0;
  background-color: initial;
  font-size: 100%;
  word-wrap: normal;
}

.btn-icon {
  padding: 0;
  outline: none;
  cursor: pointer;
      position: fixed;
    left: 6px;
    top: 6px;
  display: -webkit-inline-box;
  display: inline-flex;
  -webkit-box-align: center;
  align-items: center;
  -webkit-box-pack: center;
  justify-content: center;
  overflow: hidden;
  border-radius: 100%;
  flex-shrink: 0;
  height: calc( (1rem * 1.5) + (0.5rem * 2) + (2px)) !important;
  width: calc( (1rem * 1.5) + (0.5rem * 2) + (2px)) !important;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.min.css" integrity="sha512-1PKOgIY59xJ8Co8+NE6FZ+LOAZKjy+KY8iq0G4B3CyeY6wYHN3yt9PW0XpSriVlkMXe40PTKnXrLnZ9+fkDaog==" crossorigin="anonymous" />
<div>
  <div class="row button-container">
    <div class="col" style="display: flex;">
      <div id="sideExpand">
        <select>
          <optgroup label="This is a group">
            <option value="12" selected>This is item 1</option>
            <option value="13">This is item 2</option>
            <option value="14">This is item 3</option>
          </optgroup>
        </select>
        <select>
          <optgroup label="This is a group">
            <option value="12" selected>This is item 1</option>
            <option value="13">This is item 2</option>
            <option value="14">This is item 3</option>
          </optgroup>
        </select>
        <select>
          <optgroup label="This is a group">
            <option value="12" selected>This is item 1</option>
            <option value="13">This is item 2</option>
            <option value="14">This is item 3</option>
          </optgroup>
        </select>
      </div>
      <button class="btn btn btn-transparent btn-icon" id="filterToggle" onclick="ToggleDivVisibility()"><i class="fa fa-filter" style="position: absolute;font-size: 150%;"></i></button>
    </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

Issue with Material UI components: The Select component is collapsed and the autoWidth functionality is not

The Material UI (React) Select component is not expanding in width as expected, even with the autoWidth property. <FormControl margin="dense"> <InputLabel id="prefix-label">Prefix</InputLabel> ...

The deletion function necessitates a switch from a server component to a client component

I built an app using Next.js v13.4. Within the app, there is a server component where I fetch all users from the database and display them in individual cards. My goal is to add a delete button to each card, but whenever I try to attach an event listener t ...

Quickest method for sorting an array of objects based on the property of another object's array

Within my code, I have two arrays of objects, both containing a "columnId" property. My goal is to rearrange the first array to match the order of the second. I attempted the following method: filtered = visibleColumns.filter(function(v) { re ...

The issue of HTML email background color not showing up in Gmail, mobile, iOS, or dark mode

Introduction I created the email using MJML code and then converted it to HTML. Everything is functioning correctly, except for the background color not displaying under specific conditions: If the email client is Gmail If the mobile version is being use ...

What is the best way to use jQuery to display the character represented by an ASCII code &###?

Struggling with printing text? I am trying to append some content to an array, but it's showing special characters like &aacute; instead of the actual accented letters. Any suggestions on how to fix this issue? This is for a select tag, so I&apos ...

Remove dynamically inserted list item using a button

I have dynamically created an unordered list (<ul>) that adds list items (<li>) when a button is clicked, along with a remove button. Initially, the default list item should not contain a remove button so I added it in the script. SCRIPT $(d ...

Delightful Popup for Successful Woocommerce Additions

I have created a plugin that transforms Wordpress Woocommerce variations into a table layout. I made significant changes to the code and now I'm attempting to integrate Sweet Alerts 2 in place of the current alerts displayed when a user adds a product ...

Angular-ui typeahead feature allows users to search through a predefined

I recently developed a typeahead feature using the angular-ui library. Here is the current HTML for my typeahead: <input name="customers" id="customers" type="text" placeholder="enter a customer" ng-model="selectedCustomer" uib-typeahead="customer.fir ...

When attempting to view an .html file, SVG images may not appear on

I've encountered a common issue that I can't seem to solve. After creating a website using HTML, CSS, and JS, I uploaded it to the hosting server via FileZilla. Everything is working perfectly on the live site. However, when I downloaded the fi ...

Is it possible to view the CSS of a PDF file in print template mode using Chrome?

https://i.stack.imgur.com/XlcWm.png Currently facing an issue with debugging CSS on my PDF template. I am unable to inspect the styles directly in the CSS due to limitations. When using inspect element in Chrome, only the styles for text and elements are ...

Discover the secret to easily displaying or concealing the form of your choice with the perfect fields

Greetings! I require assistance in understanding how to hide or display a form. Specifically, I have two forms - studentForm and EmployeeForm. When selected from the dropdown list profileType, I want the corresponding form to be displayed. I am facing an ...

Can the image upload file size be customized or adjusted?

Recently, I've come across a standard input file code that looks like this: <Label class="custom-file-upload"> <input type="file" onChange={onDrop} /> Upload Photo </Label> I have been thinking about limiting the size of the ...

Tips on preventing repeated data fetching logic in Next.js App Routes

I'm currently developing a project with Next.js 13's latest App Routes feature and I'm trying to figure out how to prevent repeating data fetching logic in my metadata generation function and the actual page component. /[slug]/page.tsx expo ...

Switch between adding elements to various div containers

Can we implement a functionality where clicking an anchor tag moves the .item from .region1 to .region2, and then moving it back to .region1 when clicked again? Essentially creating a toggle behavior? <a href="#" class="btn">Click</div> &l ...

JavaScript utilizes regex patterns to modify the value located between the characters within an input's name attribute

Can anyone assist me in creating a unique regex pattern to extract specific characters from the attribute values of HTML inputs? I'm dynamically cloning select elements and input text with button clicks, so I need to maintain the attribute name synta ...

Retrieving Data from Database on the Current Page

Struggling with fetching data from a MySQL database using PHP search methods and displaying the results on the same page? Here's how you can achieve it. This HTML Script: <html> <head> <title>Digital Library</title> </head ...

Creating a new form upon clicking

By clicking the "Add New Job" link, a new form with three fields will appear. This is the primary form: <h2>JOB EXPERIENCE 1</h2> <div class="job-content"> <input type="text" value="Company" name="company" /> <input type="te ...

modifying the identification value in HTML and then reverting it

Although this may seem like messy code, I'm really in need of assistance with a problem that has stumped me. Currently, I am working on an E-shop project where I have modals for displaying products. Within these modals, there is a button that allows u ...

Is there a way to upload several documents in just one writing?

Can anyone advise me on how to add multiple documents to my Firestore collection using just one write operation? I have over 20,000 records and I'm looking for a cost-effective solution. Is there a way to add multiple docs in a single write? Apprecia ...

My Vue frontend project is encountering an error during compilation that states "this relative module module was not found."

I have created a vue frontend to interact with my spring backend, which is working well. However, when I compile the frontend, it compiles to 98% and shows an error message: ERROR Failed to compile with 1 error 11:24:51 The relative module was not foun ...