You all know Hakim El Hattab right? He creates some super crazy progressive demos over on his blog. His CodePen profile is full of amazing too.
One recent creation of his is Avgrund. It's a design pattern for dialog boxes in which the main page fades away and the modal box flies down from above (or up from below). The main page becomes smaller and blurry, making it seem further away ala depth of field in photography. The modal box sits on top, making it seem closer to you and clearly demand your attention. That's good, because the very purpose of modal boxes is to require a user to give you some input before they can do anything else.
It feels pretty magical when you see and use it. Kinda makes you want to right-click and see if it's Flash. But it's not, and like many things on the web when you start digging in, the magic is just a nice combination of simple effects.
Let's look at them in order. Note: this isn't exactly how Avgrund works, it's just me reverse engineering it.
Step 1) Separate Page Markup and Modal Markup
All the content on the entire page should be contained within a wrapper div. The modal is outside of that wrapper.
<body>
<div id="page-wrap">
<!-- all page content -->
</div>
<div id="modal">
<!-- modal box content -->
</div>
</body>
How that markup gets there is up to you. If I was using this for real, I'd probably inject it dynamically when needed through a JavaScript thingy I create just for handling dialogs.
Step 2) State Based CSS
No need to get too fancy with JavaScript. If we think "state based", all we need is a class name on the body
element and we can adjust all visual design as needed with that class. This is a larger concept that is useful in big ways and warrants further discussion (like how/where/why to trigger states), but let's just keep it simple here with a bit of jQuery:
// Something happens
$("button").on("click", function() {
// State changes
$("body").toggleClass("dialogIsOpen");
});
Step 3) Default State for Modal
The modal will be a fixed position box right in the middle of the screen. By default, it will be hidden (zero opacity) and unclickable (pointer-events). Let's just ignore browser support on that. If it's a big deal to you, you can hide it in any number of different other ways like positioning it off screen.
#modal {
background: white;
position: fixed;
width: 50%;
top: 50%;
left: 50%;
margin: -25% 0 0 -25%;
/* Embiggen */
transform: scale(1.5); /* prefix me */
/* Hidden */
opacity: 0;
pointer-events: none;
}
Step 4) Active Modal State (The Magic!)
Now we have all we need to "recede" the page when the modal is open. Let's target the #page-wrap when the state is active and do the magic.
The magic is simply: transform scale the #page-wrap to make it smaller and filter the #page-wrap to make it blurry and less colorful.
.dialogIsOpen #page-wrap {
/* Blur and de-color */
-webkit-filter: blur(5px) grayscale(50%);
/* Recede */
-webkit-transform: scale(0.9);
}
WebKit only? Well... the filters are only in WebKit for the time being. Your call if you want to load up the vendor prefixes or not. If I was going to use this for real on a site, I'd spend a little time making sure this effect had a fallback, which shouldn't be too hard. Perhaps just an emphatic box-shadow
would do.
Then: Make the dialog appear from above, enforcing the depth of field effect. Opacity makes it appear; transform scale makes it appear from above.
.dialogIsOpen #modal {
/* Regular size and visible */
transform: scale(1); /* prefix me */
opacity: 1;
/* Clickable */
pointer-events: auto;
}
Step 5) Transitions
To make it feel natural and magical, toss in some transitions on both of the players involved.
#page-wrap, #modal {
transition: all 0.4s ease; /* prefix me */
}
Of course Sass/Compass makes all this a bunch easier since it has @mixins for all this stuff. e.g.
@include transition(all 0.4s ease);
@include filter(blur(5px) grayscale(50%));
@include transform(scale(0.9));
Fair warning, this stuff is fairly memory/processing intensive. Sometimes little hacks like triggering 3D transforms helps in WebKit, at the risk of nasty looking text.
body {
/* Use at your own discretion */
-webkit-transform: translateZ(0);
}
Wrapup
A video, if you don't have access to a supported browser or whatever:
I put my reverse engineered demo from this article on CodePen, but you should really just go look at Hakim's demo, which also on CodePen.
Receding Background Modal Boxes is a post from CSS-Tricks
via CSS-Tricks http://css-tricks.com/receding-background-modal-boxes/
Comentaris
Publica un comentari a l'entrada