What is the best way to slide a Bootstrap 4 modal dialog to the right when closing, utilizing CSS or JavaScript

I have successfully added a CSS animation to my Bootstrap modal that slides in from the right when opening, which looks great. However, I am looking for a way to slide it back to the right when a custom close button is clicked, instead of just hiding it without any animation.

If anyone has an idea on how I can achieve this, please let me know. I have searched online but all I could find was information on applying animations on open only.

function customOpen(element){
  $(element).modal({
    backdrop: false,
    keyboard: false
  });
}
function customClose(element){
  $(element).modal('hide');
}
$(document).on("click", "#open", function() {
  var elem = $(this).attr("data-element");
  customOpen(elem);
});
$(document).on("click", "#close", function() {
  var elem = $(this).attr("data-element");
  customClose(elem);
});
.modal.fade {
    transition: opacity .0s linear !important;
}
.modal.fade:not(.show).left .modal-dialog {
-webkit-transform: translate3d(-125%, 0, 0);
-ms-transform: translate3d(-125%, 0, 0);
-o-transform: translate3d(-125%, 0, 0);
transform: translate3d(-125%, 0, 0);
}
.modal.fade:not(.show).right .modal-dialog {
-webkit-transform: translate3d(125%, 0, 0);
-ms-transform: translate3d(125%, 0, 0);
-o-transform: translate3d(125%, 0, 0);
transform: translate3d(125%, 0, 0);
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

<div class="modal modal-full fade right" id="newPage">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title">Modal title</h4>
      </div>
      <div class="modal-body">
        <p>One fine body&hellip;</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" id="close" data-element='#newPage'>Close</button>
      </div>
    </div>
  </div>
</div>

<a class='btn btn-primary btn' id="open" data-element='#newPage'>Open Right</a>

Answer №1

To implement a custom layout, simply utilize the modal-right class and adjust the transform property of the modal-dialog element as needed. By default, Bootstrap applies alignment from the top.

.modal.fade.modal-right .modal-dialog {
  transform: translate(125%, 0px);
}

.modal.show.modal-right .modal-dialog {
  transform: none;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous></script>

<div class="modal fade modal-right" id="newPage" tabindex="-1" role="dialog" aria-labelledby="newPage" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title">Modal title</h4>
      </div>
      <div class="modal-body">
        <p>One fine body&hellip;</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">Close</button>
      </div>
    </div>
  </div>
</div>

<a class="btn btn-primary" data-toggle="modal" data-target="#newPage">Open Right</a>

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

Strategies for managing JSON data with numeric strings

I have implemented a PHP code to fetch JSON data from a web server. Here is the code snippet: $result = mysql_query("select lat,lng from gpsdata limit 10"); $rows = array(); while($r = mysql_fetch_assoc($result)) { $rows[] = $r; } print json_encod ...

Obtaining Compiled HTML Output Using AngularJS

I'm running into an issue with obtaining the compiled html of a page in AngularJS. Below is the code snippet that demonstrates this problem: JavaScript: <script src="http://code.angularjs.org/1.2.0rc1/angular.min.js"></script> &l ...

JavaScript - Executing the change event multiple times

Below is the table I am working with: <table class="table invoice-items-table"> <thead> <tr> <th>Item</th> <th>Quantity</th> <th>Price</th> & ...

Navigate to a designated tab by selecting a designated button in HTML

In my modal, there are two tabs called Login and Registration, along with buttons named Login_Button and Registration_Button. When I click on the Login_Button, I want to display the Login tab. Similarly, when I click on the Registration_Button, I want to ...

What is the best way to input variables into json values? [JavaScript]

let countryCode; let countryName; $(window).on("load", function () { $.ajax({ url: "URL_THAT_RETURNS_JSON" }).done(function (json) { countryCode = json.country_code; $(function () { $.ajax({ url: "URL_THAT_RETURNS_JSON" ...

Guide to linking NodeJs with an Atlas mongodb Cluster

I am currently in the process of setting up a new application that utilizes an Atlas Database with node. Unfortunately, I keep encountering an error message that reads: "MongoError: MongoClient must be connected before calling MongoClient.prototype.db". ...

Showing a picture in Angular from a stored image on a node.js server

I'm curious about security measures. Let's say I've uploaded an image of an animal to the server, such as 1545419953137bear.jpg, which is stored in my backend uploads folder along with other images. On the frontend, I use an img element wit ...

Instructions for correctly integrating the ng2-datepicker module into an Angular 2 application

Ng2-datepicker (1.0.6) Angular2 (2.0.0-rc.5) As I attempted to implement it in my HTML following the documentation, I encountered the following error: zone.js:484 Unhandled Promise rejection: Template parse errors: Can't bind to 'ngMode ...

Is it possible for a nodejs server to handle both grpc and express servers simultaneously, or do they need to be separate servers?

I'm currently in the process of building a REST server using Node/Express. I'm curious about how to incorporate a GRPC server within the same setup. Would it be possible to run both servers on the same NodeJS instance, or is it recommended to hav ...

The Navbar Items Background Color is not functioning as anticipated

When the hamburger is clicked on mobile devices, I want my navbar items to have a full background-color display that only shows the logo, the hamburger icon, and the navigation items. The main content of the landing page should be hidden unless the hamburg ...

Back from where it came, the return function appears unable to escape the confines of the code

Currently, I am working on solving some leetcode problems and stumbled upon an interesting issue in my code for the problem at . However, I am having trouble understanding how it actually functions. The problem with this code lies in the fact that the ret ...

A guide on changing the background color to complement the light/dark theme in Bootstrap 5.3

In the latest version of Bootstrap, 5.3, users have the ability to select a theme for their websites. The built-in options include a Dark theme and a Light theme. For instance, by setting <html data-bs-theme="dark">, you can specify that t ...

Tips on displaying a div using javascript

I have a minor issue that I need assistance with. I am working on a PHP project where I need to dynamically generate product information on a webpage. Specifically, when a user clicks on the "Quick Look" option, a pop-up window should appear displaying rel ...

A guide on transferring information to a database through a WYSIWYG HTML JavaScript editor in conjunction with Django

This morning, I dedicated my time to going through numerous tutorials on YouTube that explain how to build your own WYSIWYG editor. After testing the code in a controlled environment, it seems to function correctly and delivers the customization promised. ...

What is the most effective approach - should we use nested functions or is there a better

Do both of my examples have the same functionality, considering that I'm handling errors by terminating with res.json(400, err)? Also, I want to confirm that in my second example, the second async.each always runs after the first one, so is it safe to ...

How about "Can I position two divs on the right side of the page

After browsing through some StackOverflow threads and other sources, I'm still struggling to figure out how to float two divs to the right while aligning them vertically (with the "sign in" div on top). Based on my findings, here are the key points: ...

What is the best way to showcase two distinct timeseries datasets using ChartJS?

I want to create a single chart using ChartJS that displays two different timeseries datasets - the past 6 months of a stock's price and the forecast for the next month. Although I have stored both the historical data and the forecast in separate tab ...

Mastering Meteor: Techniques for Manipulating Mongodb Data in Real Time Display

Within my Meteor application, I have accomplished the successful publication of data from the server and its subscription on the client side. Instead of directly displaying raw data on the client's screen, I am interested in performing some calculatio ...

Incorporating Information into an Array as a State Object within React.js

Hey everyone, I've hit a bit of a roadblock and I could really use some assistance. I'm trying to figure out why I keep getting an error when attempting to push user input into my array. Can someone take a look at my code and offer some insight? ...

In JavaScript, an HTTP request file includes a JavaScript variable

I am currently working on a Node.js project that involves making an HTTP request to fetch a JavaScript file. For example, let's say we have a file named a.js: var a = 'i am a.js'; var b = 'please convert me to js'; Here is the a ...