Guide to concealing pop-up with animation when clicking outside

My question is:

  1. Clicking on OPEN should open the popup.
  2. Clicking on BACK should close the popup.
  3. Clicking outside the popup should also close it.

Note: Clicking inside the popup when it is open should not close it. The popup should only close on an outside click or by clicking on BACK.

The current behavior is that clicking anywhere opens and closes the popup. How can I achieve the desired behavior? I have prepared a JSFiddle and Code Snippet.

JSFiddle

$('#open-1').on('click touch', function() {
  $("#card-1").toggleClass("flip")
});

$(document).on('click touch', function(event) {
  if (!$(event.target).parents().addBack().is('#open-1')) {
    $("#card-1").toggleClass("flip");
  }
});

$('#open-1').on('click touch', function(event) {
  event.stopPropagation();
});
.panel {
  width: 45%;
  display: inline-block;
  margin-bottom: 20px;
  background-color: #fff;
  margin: 14px;
  border-bottom-right-radius: 5px;
  border-bottom-left-radius: 5px;
  box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 3px 1px -2px rgba(0, 0, 0, 0.12), 0 1px 5px 0 rgba(0, 0, 0, 0.2);
}

.est {
  box-shadow: 0px 0px 0px #333;
  background-color: transparent;
}

div {
  display: block;
}

#card-1 {
  height: 300px;
  width: 100%;
  margin: 0 auto;
  z-index: 1;
  display: inline-block;
  perspective: 700px;
}

.flip .front {
  transform: rotateY(180deg);
}

.front {
  transform: rotateY(0deg);
  overflow: hidden;
  z-index: 1;
}

.front,
.back {
  border: 1px solid #ddd;
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  transform-style: preserve-3d;
  backface-visibility: hidden;
}

.ease {
  -webkit-transition: all .45s ease-out 0s;
  -moz-transition: all .45s ease-out 0s;
  -ms-transition: all .45s ease-out 0s;
  -o-transition: all .45s ease-out 0s;
  transition: all .45s ease-out 0s;
}

.open-1,
.open-2,
.open-3,
.open-4,
.open-5 {
  font-size: 14px;
  font-weight: 500;
  text-transform: uppercase;
  line-height: 50px;
}

.back {
  transform: rotateY(180deg);
  background-color: #ddd;
  display: table;
  z-index: 2;
  font-size: 13px;
  line-height: 23px;
  padding: 10px 20px;
  height: 320px;
}

.info {
  color: #333;
  font-size: 20px;
  z-index: 9999;
}

.flip .back {
    transform: rotateY(360deg);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<div class="panel e-b est">   <div class="wrapper">
    <div id="card-1">
      <div class="front ease">
        <a href="javascript:void" class="open-1" id="open-1">open</a>
        <p>
          test content
        </p>
      </div>
      <div class="back ease">
        <div class="back-info">
          <div class="info">
            test content
          </div>
        </div>
        <div class="social-bar"><a href="javascript:void" class="open-1">Back</a></div>
      </div>
    </div>   </div> </div>

JSFiddle

Answer №1

$(document).on('click touch', function(event) {
  if (!$(event.target).parents().addBack().is('#open-1')) {
    $("#card-1").toggleClass("flip");
  }
});

Instead of using toggle(), try using removeClass():

$(document).on('click touch', function(event) {
  if (!$(event.target).parents().addBack().is('#open-1')) {
    $("#card-1").removeClass("flip");
  }
});

To prevent toggling when clicking on the flipped div, target the flipped div:

$('.back.ease').on('click touch', function(event) {
  event.stopPropagation();
});

Add a class (e.g., close-1) to the back button:

<a href="#" class="open-1 close-1">Back</a>

Finally, create a function to remove the flip class when the back button is clicked:

$('.close-1').on('click touch', function() {
    $("#card-1").removeClass("flip");
});

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

What is the most efficient method for designing this jQuery code to be reusable?

I am currently using dynamic Bootstrap popovers that are populated with content from my database. In PHP, it generates unique classes for each popover. My question is, when using jQuery, do I need to trigger the popovers individually? This is how I am cur ...

Dynamically binding image URLs in VUEJS

Below is the JSON data containing button names and their corresponding image URLs: buttonDetails= [ { "name": "button1", "images": [{ "url": "https://localhost:8080/asset/d304904a-1bbd-11e6-90b9-55ea1f18bb ...

What is the most efficient way to use map-reduce in TypeScript to filter a list based on the maximum value of an attribute?

Recently, I came across a list that looked something like this: let scores = [{name: "A", skills: 50, result: 80}, {name: "B", skills: 40, result: 90}, {name: "C", skills: 60, result: 60}, {name: "D", skills: 60, ...

Using Flexbox CSS to Expand a Box According to its Content Size, Without Impacting Adjacent Rows

Is there a way to make the bottom-most row (highlighted in yellow) expand downward to fill available space? Check out this link for reference: https://jsfiddle.net/darrengates/oucvnfke/ I initially thought using: flex: auto; would solve the issue. Whil ...

Overwriting the responseText from a previous XMLHttpRequest in JavaScript

I am working on updating two different JavaScript charts displayed on a PHP page by making sequential XMLHttpRequests using JavaScript. The goal is to update the data on these charts simultaneously. <!DOCTYPE HTML> <html> <head> <scri ...

Utilizing HTML and CSS allows for creating a responsive layout with three elements such as this

Need help with CSS to align three elements in a row, where the first two are on the left and the third is on the right. The layout should adjust so that when the window size is smaller, the second element moves below the first while the third element stays ...

Warning from Material-UI Autocomplete: Always remember to use the `getOptionSelected` prop in order to tailor the equality test

I am currently working on implementing an autocomplete textfield that allows for multiple values with dynamically created options. However, I keep encountering the error message "None of the options match with ["prova1","prova2","p ...

Tips for verifying a list of objects within a request body with JOI

I'm in the process of validating the request body for an order placement. The request body contains an array of JSON objects that I need to validate, but I keep encountering the error message "productId" is required. Below is the structure of my requ ...

Loading texts with the same color code via ajax will reveal there are differences among them

I am currently designing a webshop with green as the primary color scheme. Everything is functioning perfectly, but I have noticed that the text within an ajax loaded div appears brighter than it should be. The text that loads traditionally is noticeably d ...

Auto-fill Textbox from a different Textbox in MVC 4

I am attempting to automatically fill in a textbox based on the value of another textbox, but I am struggling with getting the other textbox to populate. Below is my code - any advice on the best solution would be greatly appreciated. Action Method: [Htt ...

Struggling to extract the values from a KendoDropDown using Selenium

When using the selenium web driver, we are trying to retrieve a set of option values such as Last 30 days, Last 60 days, etc. Attempts have been made to locate these elements using a CSS selector. var timeperiodcss = "span.k-widget.k-dropdown.k-header se ...

Set a delay for an AJAX request using jQuery

Is it possible to incorporate a setTimeout function into this ajax call? Here's the code snippet: jQuery.ajax({ type : "POST", url : dir+"all/money/myFile.php", data : "page="+data.replace(/\&/g, '^'), suc ...

Two media queries are combining the same declaration, with one targeting iPads and the other targeting desktop devices

In my code, I am utilizing two media queries within a bulleted list li tag. The issue arises when both queries, clearing every nth-child(2n+1) and nth-child(3n+1), are active on the same page in Chrome's device views. This causes the grid layout to ap ...

Firestore query failing to retrieve most recent data

I have implemented a route guard in Angular that validates a specific value in firestore before granting access to the designated route. The component is accessed only after an HTTP cloud function has finished executing. This cloud function generates an o ...

What are some ways to utilize .styl stylesheets instead of traditional .css files?

I really hope this isn't a silly question, but I've been searching Google and forums for 30 minutes and can't find anything. I downloaded this npm package (ag-grid) and all their documentation talks about .css files, but the package only ha ...

How can I convert Double Quotes from (") to (&quot;) in TinyMce Editor?

While using TinyMce Editor, I have encountered a problem with double quotes breaking my code. In the HTML source of TinyMce, it displays " instead of &quot, causing issues in conversion. It seems that it is not converting " to " as it should, simi ...

Error: The DOM is throwing an uncaught TypeError because it cannot read the property 'children' of an undefined value

After delving into the depths of the DOM, I zeroed in on a specific element that I was eager to access. To reach this elusive element, I meticulously navigated through the DOM using the following code snippet: var body = document.body; var bodych ...

What is the most effective method for incorporating multi-line breadcrumb links in a React application?

I am currently working on implementing a multiline breadcrumb link feature for mobile and tablet devices. As users navigate through multiple folders, I need to handle scenarios where the number of links exceeds the maximum allowed in the breadcrumb contain ...

Express Producing Empty Axios Post Request Body

I am facing an issue with sending two text data pieces from my React frontend to an Express backend. Whenever I use the post command with Axios, the body appears as {} in the backend and becomes unusable. Below is the code that I am using. Client (App.js) ...

Binding Data to Arrays in Polymer

I'm currently diving into the world of Polymer. My goal is to connect an array with my user interface in a way that allows for dynamic updates. Each object within the array contains a changing property, and I want my UI to reflect these changes accord ...