Semi-Transparent Mockup Overlays with CSS / jQuery UI

I recently needed to convert some PSDs to HTML/CSS with pretty high accuracy, that is, the finished webpages needed to look very close to their original mockups. As is customary nerd style, I wrote some code to help me do that.

(These demos and pictures use my Movie Monster gig, because the aforementioned project isn’t ready to be talked about.)

Keeping It Simple with CSS

Semi-Transparent Mockup Overlays with CSS Demo

Demo!

<div id="mockupoverlay_original">original</div>

<style type="text/css">
#mockupoverlay_original {
	background-color: rgba(0,0,0,.5);
	font-size: 12px;
	color: white;
	position: absolute;
	top: 5px;
	right: 5px;
	height: auto;
	font-weight: bold;
	padding: 5px 10px;
	cursor: pointer;
	z-index: 1000 !important;
}
#mockupoverlay_original:active {
	background: url("mockupoverlay_original.jpg") top center no-repeat;
	padding: 0;
	top: 0;
	left: 0;
	width: 100%;
	height: 100%;
	text-indent: -5000px;
}
</style>

This affixes an “Original” button to the top right of the page that, when clicked, overlays the original mockup on top of everything, centered and at the top. Sweet and simple, it’s just a line of HTML and some CSS. Wowza!

Tip: If you realtime edit your CSS with Web Developer Extension like I do, you can change that :active pseudo-class to :hover and add something like opacity: .5; to it. That’s called onion skinning, an animation technique.

Making It Hot with jQuery UI

Semi-Transparent Mockup Overlays with CSS and jQuery UI Demo

Demo!

<div id="mockupoverlay_slider"></div>
<div id="mockupoverlay_original"></div>

<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.0/themes/base/jquery-ui.css" type="text/css" rel="stylesheet" media="screen" />
<style type="text/css">
#mockupoverlay_slider {
	z-index: 999;
	font-size: .75em;
	position: fixed;
	width: 50px;
	top: 15px;
	right: 15px;
	opacity: .33;
}
#mockupoverlay_slider:hover { opacity: .66; }
#mockupoverlay_original {
	z-index: 900;
	position: absolute;
	top: 0;
	left: 0;
	width: 100%;
	height: 100%;
	background: url("mockupoverlay_original.jpg") top center no-repeat;
	opacity: 1;
	display: none;
}
</style>
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript">google.load("jquery", "1");  google.load("jqueryui", "1");</script>
<script type="text/javascript">
$(function() {
	$("#mockupoverlay_slider").slider({ step: 10, change: function(event, ui) {
		$("#mockupoverlay_original").css("height", $(document).height());
		mockup_opacity = $('#mockupoverlay_slider').slider('value')/100;
		if (mockup_opacity == 0) {
			$("#mockupoverlay_original").hide();
		} else {
			$("#mockupoverlay_original").show().css("opacity", mockup_opacity);
		}
	} });
});
</script>

Here, instead of an “Original” button, there’s a slider, courtesy of jQuery UI, that controls the opacity of the overlay. All the way to the left, it’s hidden and you can interact with the page normally. All the way to the right, it’s fully opaque. Anywhere in the middle, it’s some fractional semi-transparency. The overlay also stretches to the entire height of the document, not just the first 100%, so you can scroll vertically.

You’ll notice that everything that can be is pulled in from Google’s CDN: jQuery, jQuery UI, and the base jQuery UI theme. Because why not?

Use It or Lose It

To use either of these approaches, copy its code and place it at the bottom of your HTML just before the closing </html> tag. Then either name your mockup image “mockupoverlay_original.jpg” and keep it in the same folder or modify the CSS to point to wherever/whatever it may be.

Make sense? Let me know if you find good use for it.