Skip to content

Instantly share code, notes, and snippets.

@khopkins218
Created May 30, 2012 16:50
Show Gist options
  • Save khopkins218/2837567 to your computer and use it in GitHub Desktop.
Save khopkins218/2837567 to your computer and use it in GitHub Desktop.
Custom Image Sprite navigation CSS generator
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<title>Nav CSS Generator of Awesomeness</title>
</head>
<body>
<!-- Developed by Kevin L. Hopkins (http://kevin.h-pk-ns.com)
You may borrow, steal, use this in any way you feel necessary but please
leave attribution to me as the source. If you feel especially grateful,
give me a linkback from your blog, a shoutout @Devneck on Twitter, or
my company profile @ http://wearefound.com. -->
<!-- Licensing:
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->
<div>
<h2>Nav CSS Generator of Awesomeness</h2>
<p>This tool will generate the CSS you need to implement custom image-sprite navigation.</p>
<div>
<p>
Enter the # of navigation elements:<br />
<input type="text" id="qty" />
</p>
<p>
Enter the height of the entire nav:<br />
<input type="text" id="height" class="nav-trigger" />
</p>
<p id="nav-list">
</p>
</div>
<div>
<p>
<input type="button" id="go" value="Go!" />
</p>
</div>
</div>
<div>
<p id="output">
</p>
</div>
<script type="text/javascript">
function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
function calculateNav() {
var qty = Number($("#qty").val());
var height = Number($("#height").val());
if (!isNumber(qty) && !isNumber(height)) {
return false
}
var navItemsOn = new Array();
for (var i = 0; i < qty; i++) {
var item = new Object();
if (i < 9) {
item.elementId = "nav-0" + (i + 1);
} else {
item.elementId = "nav-" + (i + 1);
}
var x = 0;
if (Number($("#" + item.elementId).val()) == 0) {
alert(item.elementId + " needs a width value.")
return
}
var y = height * -1;
item.valWidth = Number($("#" + item.elementId).val());
for (var j = 0; j < navItemsOn.length; j++) {
x += navItemsOn[j].valWidth;
}
item.valX = x;
item.valY = y;
navItemsOn.push(item);
}
var totalWidth = Number(navItemsOn[navItemsOn.length - 1].valX) + Number(navItemsOn[navItemsOn.length - 1].valWidth);
var cssOutput = "#nav {<br />&nbsp;&nbsp;background:url(image-sprite-path); no-repeat;<br />&nbsp;&nbsp;width: " + totalWidth + "px;<br />&nbsp;&nbsp;height: "+ height + "px;<br />&nbsp;&nbsp;margin: 0;<br />&nbsp;&nbsp;padding: 0;<br />&nbsp;&nbsp;display:block;<br />&nbsp;&nbsp;margin:0px auto;<br />}<br /><br />#nav li, #nav a {<br />&nbsp;&nbsp;height: " + height + "px;<br />&nbsp;&nbsp;display: block;<br />}<br /><br />#nav li {<br />&nbsp;&nbsp;float: left;<br />&nbsp;&nbsp;list-style: none;<br />&nbsp;&nbsp;display: inline;<br />&nbsp;&nbsp;text-indent: -9999em;<br />&nbsp;&nbsp;position:relative;<br />}<br /><br />";
for (i = 0; i < navItemsOn.length; i++) {
cssOutput += "#" + navItemsOn[i].elementId + " { width: " + navItemsOn[i].valWidth + "px; }<br />";
}
cssOutput += "<br /><br />";
for (i = 0; i < navItemsOn.length; i++) {
cssOutput += "#" + navItemsOn[i].elementId + " a:hover { background: url(image-sprite-path) " + (navItemsOn[i].valX * -1) + " " + navItemsOn[i].valY + " no-repeat; }<br />"
}
$("#output").html(cssOutput);
}
function populateLiObjs() {
var qty = Number($("#qty").val());
var height = Number($("#height").val());
var list = "<p>Enter the widths of each nav li element:<br />";
for (var i = 0; i < qty; i++) {
var eleId = ""
if (i < 9) {
eleId = "nav-0" + (i + 1);
} else {
eleId = "nav-" + (i + 1);
}
list += "<span>" + eleId + ": <input type='text' id='" + eleId + "' /></span><br />";
}
list += "<br />";
$("#nav-list").html(list);
}
$(document).ready(function() {
$("#qty").change(function() {
populateLiObjs();
})
$("#go").click(function() {
calculateNav();
})
})
</script>
<p>Concept by <a href="http://joelwe.st">Joel West</a>, Development by <a href="http://kevin.h-pk-ns.com">Kevin Hopkins</a></p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment