This project has been updated! It now includes more robust JavaScript, icons and more complete XHTML.
Description
A "Mac OS X" style icon dock, built entirely in XHTML/CSS and with smooth animation using jQuery/JavaScript. As from the demo, and unlike
many other icon docks, the navigation works with NO JavaScript. This is very important as the website should still be accessible with JavaScript
disabled. Each icon can have a scalable image and a title. As the image is dynamically resized, it is up to the browser to smoothly render the icon.
Internet Explorer 6 does no longer work fully due to lack of support for vertical align with images. This will be corrected in the next version.
Note: This demonstration uses icons from http://lemex.deviantart.com/art/micr-Os-icon-pack-64-px-v2-135049863, which
are not for commercial use.
Demo
Without JavaScript
With JavaScript
JavaScript
- var HOVER_SCALE = 2.0;
- var PROXIMITY = 200.0;
-
- $(function() {
- $(".icon_enlarge").each(function(i) {
- //private variables
- var dock = $(this);
- var dock_icons = dock.find("li a");
-
- //store the to and from sizes
- $.each(dock_icons, function() {
- var initHeight = parseInt($(this).height());
- var initWidth = parseInt($(this).width());
-
- $(this).data("initWidth", initWidth);
- $(this).data("initHeight", initHeight);
-
- $(this).data("newWidth", initWidth * HOVER_SCALE);
- $(this).data("newHeight", initHeight * HOVER_SCALE);
- });
-
- //event handlers
- dock.bind("mouseleave",function(event) {
- $.each(dock_icons, function() {
- $(this).animate({"height": $(this).data("initHeight") + "px", "width": $(this).data("initWidth") + "px"},"fast");
- });
- });
-
- dock.bind("mousemove", function(event) {
- $.each(dock_icons, function() {
- var newSize = calculateDockIconSize($(this), event.pageX);
-
- $(this).stop();
- $(this).width(newSize[0]);
- $(this).height(newSize[1]);
- });
- });
- });
- });
-
- /**
- * Calculate the height of an icon based upon its position from the mouse
- * @param icon
- * @param mouse position (x only)
- * @return new width/height
- */
- function calculateDockIconSize(icon, mousePosX) {
- //get the distance in x from the mouse to the icon
- var initWidth = parseInt($(icon).data("initWidth"));
- var initHeight = parseInt($(icon).data("initHeight"));
- var newWidth = parseInt($(icon).data("newWidth"));
- var newHeight = parseInt($(icon).data("newHeight"));
-
- var xProximity = Math.abs(mousePosX - $(icon).offset().left - (newWidth/2.0));
-
- //if we need to vary the height because the mouse is within range
- if (xProximity<PROXIMITY) {
- //get the percentage height the icon needs to be
- var newRatio = ((PROXIMITY-xProximity)/PROXIMITY);
-
- var additionalWidth = newRatio * (newWidth-initWidth);
- var additionalHeight = newRatio * (newHeight-initHeight);
-
- //add on the additional percentage to the icon
- return [(initWidth + additionalWidth), (initHeight + additionalHeight)];
- } else {
- //otherwise, return the original icon size
- return [initWidth, initHeight];
- }
- }
CSS
- .navigation {
- text-align: center;
- }
-
- .icon_dock {
- display: inline-block;
- width: auto;
- margin: 0px;
- margin-top: 10px;
- padding: 0px;
- padding-left: 60px;
- padding-right: 60px;
- list-style: none;
- height: 135px;
- overflow: visible;
- vertical-align: bottom;
- line-height: 135px;
- text-align: center;
- background: transparent url("icons/shadow.gif") repeat-x bottom left;
- }
-
- .icon_dock:after {
- clear: both;
- display: block;
- content: ".";
- visibility: hidden;
- height: 0px;
- }
-
- .icon_dock li {
- width: auto;
- height: auto;
- display: inline-block;
- bottom: 0px;
- vertical-align: bottom;
- padding-left: 1px;
- padding-right: 1px;
- }
-
- .icon_dock li.seperator {
- background: transparent url("icons/seperator.png") no-repeat center center;
- }
-
- .icon_dock li.seperator,
- .icon_dock li.seperator:hover {
- width: 6px;
- height: 60px;
- border: none;
- }
-
- .icon_dock li a {
- margin: 0px;
- padding: 0px;
- display: block;
- padding-top: 0px;
- height: 60px;
- width: 60px;
- text-align: center;
- color: black;
- text-decoration: none;
- position: relative;
- outline: none;
- }
-
- .icon_dock li a:hover,
- .icon_dock li a.hover {
- width: 120px;
- height: 120px;
- padding-top: 15px;
- }
-
- .icon_dock li a span {
- width: 100%;
- top: 0px;
- position: absolute;
- text-align: center;
- left: 0px;
- z-index: 100;
- visibility: hidden;
- }
-
- .icon_dock li a:hover span {
- visibility: visible;
- }
-
- .icon_dock li a img {
- width: 100%;
- position: absolute;
- bottom: 0px;
- left: 0px;
- }
-
IE CSS (Conditional)
- .icon_dock li {
- /*IE Hack for inline-block*/
- zoom: 1;
- display: inline;
- }
XHTML
- <div class="navigation">
- <ul class="icon_dock">
- <li><a href="#"><span>Internet</span><img src="icons/world.png" alt="[Internet icon]" title="Internet" /></a></li>
- <li><a href="#"><span>Chat</span><img src="icons/chat.png" alt="[Chat icon]" title="Chat" /></a></li>
- <li><a href="#"><span>Contact</span><img src="icons/contact.png" alt="[Contact icon]" title="Contact" /></a></li>
- <li><a href="#"><span>Notes</span><img src="icons/notes.png" alt="[Notes icon]" title="Notes" /></a></li>
- <li><a href="#"><span>Folder</span><img src="icons/folder.png" alt="[Folder icon]" title="Folder" /></a></li>
- <li><a href="#"><span>Settings</span><img src="icons/lock.png" alt="[Settings icon]" title="Settings" /></a></li>
- <li class="seperator"></li>
- <li><a href="#"><span>Help</span><img src="icons/about.png" alt="[Help icon]" title="Help" /></a></li>
- <li><a href="#"><span>Desktop</span><img src="icons/desktop.png" alt="[Desktop icon]" title="Desktop" /></a></li>
- <li><a href="#"><span>Bin</span><img src="icons/garbage.png" alt="[Bin icon]" title="Bin" /></a></li>
- </ul>
- </div>