Guest User

Untitled

a guest
Oct 17th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.57 KB | None | 0 0
  1. Cool notification messages with CSS3 & Jquery
  2. 5digg
  3. Nowadays, UX is a key factor when it comes about creating/designing a product or system. To keep users happy, developers struggle to create a good experience and a better interactivity.
  4. UX is a term used to describe the overall experience and satisfaction a user has when using a product or system. So, a good UX will always make users happy and businesses more successful.
  5. Notification messages are an important part of the user experience and you can’t afford to omit them. A notification alert message should appear every time the user perform important tasks.
  6. In this article, you’ll learn how to create some alert messages with CSS3 and Jquery.
  7.  
  8.  
  9. View demo
  10.  
  11. Message Types
  12. Bellow is a list with common notification messages:
  13. Info
  14. Error
  15. Warning
  16. Success
  17. Info
  18. Its purpose is to inform user regarding a relevant matter.
  19.  
  20.  
  21. Error
  22. When an operation has failed, the user must be notified. For example: “The account couldn’t be deleted.” or “Your settings weren’t saved” etc.
  23.  
  24.  
  25. Warning
  26. This type of message notify the user of a condition that might cause a problem in the future.
  27.  
  28.  
  29. Success
  30. The success message should be displayed after user successfully performs an action.
  31.  
  32.  
  33. The HTML
  34. <div class="info message">
  35.                  <h3>FYI, something just happened!</h3>
  36.                  <p>This is just an info notification message.</p>
  37. </div>
  38.  
  39. <div class="error message">
  40.                  <h3>Ups, an error ocurred</h3>
  41.                  <p>This is just an error notification message.</p>
  42. </div>
  43.  
  44. <div class="warning message">
  45.                  <h3>Wait, I must warn you!</h3>
  46.                  <p>This is just a warning notification message.</p>
  47. </div>
  48.  
  49. <div class="success message">
  50.                  <h3>Congrats, you did it!</h3>
  51.                  <p>This is just a success notification message.</p>
  52. </div>
  53. The CSS
  54. .message
  55. {
  56.                 -webkit-background-size: 40px 40px;
  57.                 -moz-background-size: 40px 40px;
  58.                 background-size: 40px 40px;
  59.                 background-image: -webkit-gradient(linear, left top, right bottom,
  60.                                                         color-stop(.25, rgba(255, 255, 255, .05)), color-stop(.25, transparent),
  61.                                                         color-stop(.5, transparent), color-stop(.5, rgba(255, 255, 255, .05)),
  62.                                                         color-stop(.75, rgba(255, 255, 255, .05)), color-stop(.75, transparent),
  63.                                                         to(transparent));
  64.                 background-image: -webkit-linear-gradient(135deg, rgba(255, 255, 255, .05) 25%, transparent 25%,
  65.                                                         transparent 50%, rgba(255, 255, 255, .05) 50%, rgba(255, 255, 255, .05) 75%,
  66.                                                         transparent 75%, transparent);
  67.                 background-image: -moz-linear-gradient(135deg, rgba(255, 255, 255, .05) 25%, transparent 25%,
  68.                                                         transparent 50%, rgba(255, 255, 255, .05) 50%, rgba(255, 255, 255, .05) 75%,
  69.                                                         transparent 75%, transparent);
  70.                 background-image: -ms-linear-gradient(135deg, rgba(255, 255, 255, .05) 25%, transparent 25%,
  71.                                                         transparent 50%, rgba(255, 255, 255, .05) 50%, rgba(255, 255, 255, .05) 75%,
  72.                                                         transparent 75%, transparent);
  73.                 background-image: -o-linear-gradient(135deg, rgba(255, 255, 255, .05) 25%, transparent 25%,
  74.                                                         transparent 50%, rgba(255, 255, 255, .05) 50%, rgba(255, 255, 255, .05) 75%,
  75.                                                         transparent 75%, transparent);
  76.                 background-image: linear-gradient(135deg, rgba(255, 255, 255, .05) 25%, transparent 25%,
  77.                                                         transparent 50%, rgba(255, 255, 255, .05) 50%, rgba(255, 255, 255, .05) 75%,
  78.                                                         transparent 75%, transparent);
  79.                  -moz-box-shadow: inset 0 -1px 0 rgba(255,255,255,.4);
  80.                  -webkit-box-shadow: inset 0 -1px 0 rgba(255,255,255,.4);
  81.                  box-shadow: inset 0 -1px 0 rgba(255,255,255,.4);
  82.                  width: 100%;
  83.                  border: 1px solid;
  84.                  color: #fff;
  85.                  padding: 15px;
  86.                  position: fixed;
  87.                  _position: absolute;
  88.                  text-shadow: 0 1px 0 rgba(0,0,0,.5);
  89.                  -webkit-animation: animate-bg 5s linear infinite;
  90.                  -moz-animation: animate-bg 5s linear infinite;
  91. }
  92.  
  93. .info
  94. {
  95.                  background-color: #4ea5cd;
  96.                  border-color: #3b8eb5;
  97. }
  98.  
  99. .error
  100. {
  101.                  background-color: #de4343;
  102.                  border-color: #c43d3d;
  103. }
  104.  
  105. .warning
  106. {
  107.                  background-color: #eaaf51;
  108.                  border-color: #d99a36;
  109. }
  110.  
  111. .success
  112. {
  113.                  background-color: #61b832;
  114.                  border-color: #55a12c;
  115. }
  116.  
  117. .message h3
  118. {
  119.                  margin: 0 0 5px 0;
  120. }
  121.  
  122. .message p
  123. {
  124.                  margin: 0;
  125. }
  126.  
  127. @-webkit-keyframes animate-bg
  128. {
  129.     from {
  130.         background-position: 0 0;
  131.     }
  132.     to {
  133.        background-position: -80px 0;
  134.     }
  135. }
  136.  
  137. @-moz-keyframes animate-bg
  138. {
  139.     from {
  140.         background-position: 0 0;
  141.     }
  142.     to {
  143.        background-position: -80px 0;
  144.     }
  145. }
  146. Note the animate-bg, which animate the background diagonal stripes. Of course, to see this effect, you should use latest Webkit browsers like Chrome/Safari or Mozilla 5+.
  147. The notification messages will be hidden initially. For that we’ll use fixed positioning (absolute value just for IE6 – as there is no position:fixed support).
  148.     position: fixed;
  149.      _position: absolute; /* IE6 only */
  150. The Jquery
  151. Define the messages types using an array:
  152. var myMessages = ['info','warning','error','success'];
  153. The below function hides the notification messages. Messages could have dynamic heights and for that, each message’s height is calculated in order to hide it properly.
  154. function hideAllMessages()
  155. {
  156.                  var messagesHeights = new Array(); // this array will store height for each
  157.  
  158.                  for (i=0; i<myMessages.length; i++)
  159.                  {
  160.                                   messagesHeights[i] = $('.' + myMessages[i]).outerHeight(); // fill array
  161.                                   $('.' + myMessages[i]).css('top', -messagesHeights[i]); //move element outside viewport
  162.                  }
  163. }
  164. The showMessage function is called when document ready.
  165. function showMessage(type)
  166. {
  167.         $('.'+ type +'-trigger').click(function(){
  168.                   hideAllMessages();
  169.                   $('.'+type).animate({top:"0"}, 500);
  170.         });
  171. }
  172. On page load, first of all we’ll hide all the messages: hideAllMessages(). Then, for each trigger, show the equivalent message:
  173. $(document).ready(function(){
  174.  
  175.                  // Initially, hide them all
  176.                  hideAllMessages();
  177.  
  178.                  // Show message
  179.                  for(var i=0;i<myMessages.length;i++)
  180.                  {
  181.                         showMessage(myMessages[i]);
  182.                  }
  183.  
  184.                  // When message is clicked, hide it
  185.                  $('.message').click(function(){
  186.                                   $(this).animate({top: -$(this).outerHeight()}, 500);
  187.                   });            
  188.  
  189. });
  190. View demo
  191.  
  192. Conclusion
  193. Using CSS3, I think you can find endless possibilities to design notification messages. The above is just a minimal example, with no images (for simplicity’s sake).
  194. But, the design is not everything, as functionality and interactivity have a very important role here.
  195. Updates
  196. Added CSS3 animation support for Mozilla 5+ (thanks Magne). I also fixed the proper CSS3 animation (thanks Glukki )
  197. CSS3 gradients syntax updated. Now, Safari also renders background stripes.
  198. Related content
  199. CSS3 dropdown menu
  200. How to create a cool and usable CSS3 search box
  201. Sexy CSS3 menu
  202. Pure CSS3 accordion
  203. HTML5 & CSS3 envelope contact form
  204.  
  205. Written by Red
  206. Catalin Rosu, a.k.a Red, is a professional web designer and developer who loves to be creative and enjoys CSS techniques. Stay tuned for latest updates, subscribe to RSS and follow him on Twitter.
  207.  
  208.  
  209.  
  210. 13 Responses to “Cool notification messages with CSS3 & Jquery”
  211. GLuKKi says:
  212. July 5, 2011 at 16:35
  213. There’s a glitch in example animation due to wrong background position shifting number. The result is the jumping background every 5 seconds.
  214. Fix animate-bg background-position to the value ’40px * N’
  215.  
  216. @-webkit-keyframes animate-bg
  217. {
  218. from {
  219. background-position: 0 0;
  220. }
  221. to {
  222. background-position: -80px 0;
  223. }
  224. }
  225.  
  226. REPLY
  227. Red says:
  228. July 5, 2011 at 17:19
  229. Thanks for noticing, I’ll fix that soon!
  230.  
  231. REPLY
  232. Magne Andersson says:
  233. July 5, 2011 at 19:37
  234. You may want to know that support for CSS-animations is available in Firefox starting with (current) version 5. Seeing as you’ve added prefixes for other properties, you may want to do that for animations in Gecko-based browsers as well
  235.  
  236. REPLY
  237. Red says:
  238. July 5, 2011 at 21:26
  239. Updates were made, thanks for your valuable feedback!
  240.  
  241. REPLY
  242. Paul Crowe says:
  243. July 5, 2011 at 21:28
  244. I think this could be used for Email subscription.Click on an Email icon and the form slides down.Might try it…
  245.  
  246. REPLY
  247. Red says:
  248. July 5, 2011 at 23:31
  249. Paul, with some modifications, you could use the above also for other purposes. Good luck!
  250.  
  251. REPLY
  252. Subhsh says:
  253. July 6, 2011 at 07:26
  254. An auto-close after x seconds will be a great help. Please consider adding it.
  255.  
  256. REPLY
  257. Red says:
  258. July 6, 2011 at 10:14
  259. Subhsh,
  260. thanks for your suggestion.
  261.  
  262. This can be done calling hideAllMessages(); function using a setTimeout() method.
  263.  
  264. Though, auto hiding could be an important drawback. The user may not have time to actually read the notification message.
  265.  
  266. Catalin
  267.  
  268. REPLY
  269. infocyde says:
  270. July 8, 2011 at 19:24
  271. Thanks for posting your demo, I like the technique. How about message blocks that pop up from the bottom of the view port? I’ll take your example and see what I can come up with.
  272.  
  273. Thanks again for sharing, this example has been very helpful.
  274.  
  275. REPLY
  276. Red says:
  277. July 9, 2011 at 09:35
  278. infocyde, nice to hear you like this technique.
  279.  
  280. Of course, messages that pop out from the bottom are another option when it comes to notification messages.
  281.  
  282. Thanks for your comment!
  283.  
  284. REPLY
  285. Tim says:
  286. July 9, 2011 at 02:53
  287. Great examples! I’m a backend guy and don’t know much about js. How would you go about showing a message after a form submission?
  288.  
  289. REPLY
  290. Red says:
  291. July 9, 2011 at 09:56
  292. Thanks Tim!
  293.  
  294. If you’re submitting form without page refresh, using jQuery’s Ajax function, then you need to call the showMessage('success'); function within .success() method and showMessage('error'); function within .error() method.
  295.  
  296. REPLY
  297. Tim says:
  298. July 9, 2011 at 17:04
  299. Thank you. Yeah, I should have specified there’s a page refresh.
  300.  
  301. REPLY
  302. Leave a Reply
  303. Name (required)
  304. Mail (will not be published) (required)
  305. Website
  306.  
  307.  
  308. Notify me of follow-up comments via e-mail
  309. Subscribe to the Newsletter:
  310.  
  311.  
  312. DesignerThemes.com
  313. We're teaming up with awesome designers to build some amazing WordPress themes!
  314. via Ad Packs
  315. Categories
  316. CSS
  317. Graphic Design
  318. Tools
  319. Tutorials
  320. Web Design
  321. Friends
  322. Accesorii iPhone
  323. Crazy cool gadgets
  324. Science and technology blog
  325. Six Revisions
  326. StudioPress Genesis
  327. Total Computers
  328. Webtide
Add Comment
Please, Sign In to add comment