{"id":2117,"date":"2018-07-09T13:18:58","date_gmt":"2018-07-09T18:18:58","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=2117"},"modified":"2018-07-09T13:18:58","modified_gmt":"2018-07-09T18:18:58","slug":"jquery-create-animated-alert-box","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2018\/07\/09\/jquery-create-animated-alert-box\/","title":{"rendered":"jQuery: How to create an animated alert box"},"content":{"rendered":"<p>Let&#8217;s say you&#8217;ve made a login form and you want to display a placard whenever the login information is incorrect to alert the user. For example, when the user types an incorrect email address or password, they see the placard above the form, with a white background and red text:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2118\" src=\"http:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/07\/incorrect-login-placard.png\" alt=\"\" width=\"487\" height=\"471\" srcset=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/07\/incorrect-login-placard.png 487w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/07\/incorrect-login-placard-300x290.png 300w\" sizes=\"auto, (max-width: 487px) 100vw, 487px\" \/><\/p>\n<p>This is easy enough to accomplish with CSS and a little bit of JavaScript. However, let&#8217;s say we want to animate the placard so that every time it appears on the screen, it flips to the right, as if laying a card down to the right. This can be accomplished with some jQuery. For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">var card = document.querySelector(\"#errorMessage\");\r\nvar container = document.querySelector('.flip-container');\r\nvar isOpen = false;\r\n\r\nfunction showCard() {\r\n  if (!isOpen) {\r\n    container.style.visibility = \"visible\";\r\n    card.classList.add(\"flip\");\r\n    \/\/ document.querySelector(\".flipper\").classList.toggle(\"flip\");\r\n    isOpen = true;\r\n    console.log(\"isOpen1\", isOpen)\r\n  } else  { \r\n    card.classList.toggle(\"flip\");\r\n    isOpen = false;\r\n    console.log(\"isOpen2\", isOpen)\r\n    clickAgain();\r\n  }\r\n}\r\n\r\nfunction clickAgain(){\r\n  setTimeout(function() {\r\n    $(document).ready(function(){\r\n      $(\"#b1\").click()\r\n    });\r\n  }, 350);\r\n}<\/pre>\n<p>Here is the HTML:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">&lt;h1&gt;Click the button below to see the &lt;br&gt;animated alert.&lt;\/h1&gt;\r\n&lt;div class=\"flip-container\" id=\"errorMessage\" &gt;\r\n  &lt;div class=\"flipper\"&gt;\r\n   &lt;!-- text here will rotate --&gt; \r\n      &lt;div class=\"front\"&gt;\r\n         &lt;!-- front content --&gt; \r\n         &lt;div class=\"error-box\"&gt;\r\n            Email address or password &lt;BR&gt;\r\n            not found. &lt;BR&gt;\r\n            Please Try Again.\r\n         &lt;\/div&gt;      \r\n      &lt;\/div&gt;\r\n      &lt;div class=\"back\"&gt;\r\n\t&lt;!-- back content --&gt;\r\n      &lt;\/div&gt;\r\n   &lt;\/div&gt;      \r\n&lt;\/div&gt;\r\n&lt;input type=\"button\" id=\"b1\" value=\"Show card\" onClick=\"showCard();\"&gt;<\/pre>\n<p>And here is the CSS used for styling:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"css\">body {\r\n  background: #575955;\r\n  color: white;\r\n}\r\n.error-box {\r\n  width: 380px;\r\n  height: 110px;\r\n  background: #fff;\r\n  border: solid 1px #B71C1C;\r\n  border-radius: 9px;\r\n  font-family: 'Raleway', sans-serif;\r\n  font-size: 1.6rem;\r\n  color: #B71C1C;\r\n  text-align: center;\r\n  padding: 30px;\r\n}\r\n\r\n\/* Hide the flip container to start *\/\r\n.flip-container {\r\n  perspective: 1000px;\r\n\u00a0\u00a0visibility: hidden;\r\n}\r\n.flip-container.flip .flipper {\r\n  transform: rotateY(90deg);\r\n  transition: 0.35s;\r\n}\r\n\r\n.flip-container, .front, .back {\r\n  width: 320px;\r\n  height: 200px;\r\n}\r\n\r\n\/* flip speed goes here *\/\r\n.flipper {\r\n  transition: 0s;\r\n  transform-style: preserve-3d;\r\n  position: relative;\r\n}\r\n\r\n\/* hide back of pane during swap *\/\r\n.front, .back {\r\n  backface-visibility: hidden;\r\n  position: absolute;\r\n  top: 0;\r\n  left: 0;\r\n}\r\n\r\n\/* front pane, placed above back *\/\r\n.front {\r\n  z-index: 2;\r\n  \/* for firefox 31 *\/\r\n  transform: rotateY(-90deg);\r\n}\r\n\r\n\/* back, initially hidden pane *\/\r\n.back {\r\n  transform: rotateY(90deg);\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>The CSS for this technique is even more important than the jQuery. It uitlizes some rare features such as:<\/p>\n<ul>\n<li class=\" even\"><span class=\"kw3\">transform<\/span><span class=\"sy0\">:<\/span><span class=\"\"> rotateY<\/span><span class=\"br0\">(<\/span><span class=\"nu0\">90deg<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span><\/li>\n<li class=\" odd\"><span class=\"kw3\">transition<\/span><span class=\"sy0\">:<\/span> <span class=\"nu0\">0.<\/span><span class=\"nu0\">35s<\/span><span class=\"sy0\">;<\/span><\/li>\n<li><span class=\"kw3\">backface-visibility<\/span><span class=\"sy0\">:<\/span><span class=\"\"> hidden<\/span><span class=\"sy0\">;<\/span><\/li>\n<li><span class=\"kw3\">transform-style<\/span><span class=\"sy0\">:<\/span><span class=\"\"> preserve-<\/span><span class=\"nu0\">3d<\/span><span class=\"sy0\">;<\/span><\/li>\n<\/ul>\n<p>The jQuery toggles visibilty and classes that have animations attached to them, but does so in such a way that every time the button is clicked, the card starts over from the same hidden position. Here is an embedded Codepen that shows this technique in action:<\/p>\n<p class='codepen'  data-height='420' data-theme-id='0' data-slug-hash='LdWPPG' data-default-tab='css,result' data-animations='run' data-editable='' data-embed-version='2'>\nSee the Pen Animated altert box 4 (CSS transition + jQuery) \u2013 2 by Chris Nielsen (@Chris_Nielsen) on CodePen.<\/p>\n\n<p>&nbsp;<\/p>\n<p>For more information about these CSS techniques, see:<br \/>\n<a href=\"https:\/\/www.w3schools.com\/cssref\/css3_pr_transform.asp\">https:\/\/www.w3schools.com\/cssref\/css3_pr_transform.asp<\/a><br \/>\n<a href=\"https:\/\/www.w3schools.com\/css\/css3_transitions.asp\">https:\/\/www.w3schools.com\/css\/css3_transitions.asp<\/a><br \/>\n<a href=\"https:\/\/www.w3schools.com\/cssref\/css3_pr_backface-visibility.asp\">https:\/\/www.w3schools.com\/cssref\/css3_pr_backface-visibility.asp<\/a><br \/>\n<a href=\"https:\/\/www.w3schools.com\/cssref\/css3_pr_transform-style.asp\">https:\/\/www.w3schools.com\/cssref\/css3_pr_transform-style.asp<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s say you&#8217;ve made a login form and you want to display a placard whenever the login information is incorrect to alert the user. For example, when the user types an incorrect email address or password, they see the placard above the form, with a white background and red text: This is easy enough to &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2018\/07\/09\/jquery-create-animated-alert-box\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">jQuery: How to create an animated alert box<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44,128],"tags":[142,10,105,45,123,144,141,143],"class_list":["post-2117","post","type-post","status-publish","format-standard","hentry","category-javascript-language","category-jquery","tag-backface-visibility","tag-css","tag-html","tag-javascript","tag-jquery","tag-transform","tag-transform-style","tag-transition"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2117","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=2117"}],"version-history":[{"count":9,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2117\/revisions"}],"predecessor-version":[{"id":2127,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2117\/revisions\/2127"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=2117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=2117"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=2117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}