Fading and Spinning Icons with CSS3 and jQuery


The post detailed how you could leverage CSS3's transformations and opacity properties, as well as the magical MooTools JavaScript framework, to create spinning, fading, animated icons. Due to popular request, I've duplicated the effect with another popular JavaScript toolkit: jQuery.

The HTML

<div style="padding:20px 0;position:relative;">
<div id="followIcons">
<a style="top: 0.653561px; left: 132.318px; z-index: 1022; opacity: 0.6; -moz-transform: rotate(36.7188deg);" href="http://feeds.feedburner.com/TemplateForYourBlog" rel="nofollow" id="iconRSS">RSS Feed</a>
<a style="top: 38.5985px; left: 200.085px; z-index: 1023; opacity: 0.6; -moz-transform: rotate(74.7156deg);" href="http://twitter.com/bambangwi" rel="nofollow" id="iconTwitter">@Bambang Wicaksono Twitter</a>
<a style="top: 2.87457px; left: 131.284px; z-index: 1012; opacity: 0.6; -moz-transform: rotate(191.92deg);" href="http://www.stumbleupon.com/bambangwi" rel="nofollow" id="iconstumbleupon">@Bambang Wicaksono Stumbleupon</a>
<a style="top: 29.391px; left: 245.218px; z-index: 1000; opacity: 0.6; -moz-transform: rotate(295.304deg);" href="http://www.delicious.com/bambang_wicaksono" rel="nofollow" id="iconDelicious">Bambang Wicaksono de.licio.us</a>
<a style="top: 33.1283px; left: 248.676px; z-index: 1024; opacity: 0.6; -moz-transform: rotate(78.0497deg);" href="http://facebook.com/masbambangwicaksono" rel="nofollow" id="iconFacebook">Bambang Wicaksono Facebook</a>
<a style="top: 15.11px; left: 93.4135px; z-index: 1017; opacity: 0.6; -moz-transform: rotate(346.566deg);" href="http://www.reddit.com/bambangwi" rel="nofollow" id="iconreddit">Bambang Wicaksono Reddit</a>
<a style="top: 28.4499px; left: 47.2333px; z-index: 1020; opacity: 0.6; -moz-transform: rotate(65.6721deg);" href="http://www.digg.com/users/bambangwi" id="icondigg">Bambang Wicaksono Digg</a>
<a style="top: 13.7949px; left: 36.0966px; z-index: 1021; opacity: 0.6; -moz-transform: rotate(210.147deg);" href="mailto:bambang_wicaksono@yahoo.com" id="iconMail">Bambang Wicaksono Email</a>
<a style="top: 24.9191px; left: 393.534px; z-index: 1019; opacity: 0.6; -moz-transform: rotate(264.417deg);" href="http://www.google.com/reader/view/feed/http%3A%2F%2Ffeeds.feedburner.com%2FTemplateForYourBlog" rel="nofollow" id="iconfavorite">Bambang Wicaksono Feed</a>
</div>
<div class="clear"></div>
</div>

The links are as standard as they come. These will be turned into dynamic icons.

The CSS


The first part of the process is using standard CSS to move the text off screen and instead use the icons as background images for the link:
<style type="text/css">
#followIcons a {
display:inline-block;
width:48px;
height:48px;
text-indent:-3000px;
background-position:0 0;
background-repeat:no-repeat;
z-index:2000;
overflow:hidden;
position:absolute;
-webkit-transition-duration: 0.8s;
-moz-transition-duration: 0.8s;
-o-transition-duration: 0.8s;
transition-duration: 0.8s;
-webkit-transition-property: -webkit-transform;
-moz-transition-property: -moz-transform;
-o-transition-property: -o-transform;
transition-property: transform;
}
#iconRSS{ background-image:url(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgV43AiXCgHPUron1dIfNtAykaEw8u7ScuMisja2xgg3k7Mb4irq4SRbSlO1nHAMph-9YGcP_pMdsvT1Pikr7mFMViS_68RjgEaKEPSwfPtok6_nadjzNqBg5QUYylbLtT-zPPL9OU51oyw/s1600/rss.png); }
#iconTwitter{ background-image:url(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiUNCVL4AAj3SBKLbqMyPCT_anqChq0160KyZoJIJMB0ysl6zZO9QpdnCUOhk8NpPwLCpBKLu-zwnA3rbVMI3z2pEmjDZR-jxL2wgFVSUzyscc88-mWxrTRx_I8ecbqzvcG4ve6mB9XtRtW/s1600/twitter_bird.png); }
#iconstumbleupon{ background-image:url(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgt31ieYZ2aRCzP3CeNXOLefCBVSTWGrbXd78UmXT75k2ZkYCpuYHa-Dr2YcX7fRGsfQnAhVgQMlf1nZewhXiGGS74pxUTCxanwx-YrqgiU_-KwqT_YcukPzbYH1Dp4xQ1Zn1D4qMAoBVnx/s1600/stumbleupon.png); }
#iconDelicious{ background-image:url(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjViR57ah8ntkNdeq9jSC3yZaC82Mm50pkV9diain0ajyHqyK7cw85cjtQeZ97YSm1eN7cioS2zMCIBaVgouuYR9liPigqc9wCBQYdaAxkO6_6dgHuMCNde9tr5dpYGyZIPvgZ3ayed9akm/s1600/delicious.png); }
#iconFacebook{ background-image:url(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYcL1zleDqzotmcriOZhyphenhyphentJnORVKtKPfuaAfR1Rfe5QLmce9aL50Y4JBli8Fw_fkVkphg_i2UR2PQQzIJWVsArxArDL1JuWB7QIeSFEGRO3ioW1qc8KpUwB1rbW_RcPy2Hsj2bEzzUd0ev/s1600/facebook.png); }
#iconreddit{ background-image:url(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgSEvbMDUWZ7kIi-yjCaua6m3Zn_ptuKGYQ4ETuM9wlu-8fHjy_GKMvNblMdr8nPqNPCfN6qw-C2oYYedXV6aacwh7poZred9jOV1YXfVwTVRjiUsTuE3pmyHmEQd5uQkVp4R_vz9lIvDRU/s1600/reddit.png); }
#icondigg{ background-image:url(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi9fx2m2nrj4mo5AUYZwqLqbr0LSAfbGjUCXGN6QeOMebdMrX2XdpFe3mGjKAhrDo8-IP9Vd0lEmslvUVKJwVVHkcAXLM7WPs9aKK-z2KKyF9KYB1HBudCr9FDF08u0wc617gfG6vMJIFhY/s1600/digg.png); }
#iconMail{ background-image:url(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgAypSJVxjgzBkJnock9oEKW8dw16uuU6xcDdS3o_4ETbWCFkL9Fc8Xlo7jdCF5Do5YmQ6M8Aog7lfWC6iP7B63fYgEMGIMXNxTqaZtyWM7r_2fO8BdVLn5mzwjNuqZGT7OVuK_HBmHq8Mz/s1600/mail.png); }
#iconfavorite{ background-image:url(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgGzvXdUExYtIcu7VuzLAjGdUKHt3IzYaNGwWEYWdmv0EKW-45d_9aCQtLklgdDdHQ7e29j65bBg8F5qCBJijqnYcPsDA2keOnGqD0k6ottMdkNXM0HNFJbCGO08MhzvIUS6MHUrKAGW-Mf/s1600/favorite.png); }
</style>

The transition duration will be 0.8 seconds and transition property will be a basic transform. You can change the transform duration to any duration you'd like. Too fast or too slow will ruin the effect.

The jQuery JavaScript


The first part is randomly positioning each node/icon within the container. It's important to know the container's width and height, then subtract the icon width and height from that to know the true area you can fit the icon into. Nothing would be more lame than a piece of the icon hidden. The next step of the process is adding mouseenter and mouseleave events to make the images rotate and fade in during each respective event.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>

<script>
jQuery(document).ready(function() {

// "Globals" - Will make things compress mo-betta
var $random = function(x) { return Math.random() * x; };

// Get the proper CSS prefix
if(jQuery.browser.webkit) {
cssPrefix = "webkit";
}
else if(jQuery.browser.mozilla) {
cssPrefix = "moz";
}
else if(jQuery.browser.opera) {
cssPrefix = "o";
}

// Apply opacity
var zIndex = 1000;

// Randomize each link
jQuery.each(jQuery("#followIcons a"),function(index) {
var startDeg = $random(360);
var element = jQuery(this);
var resetPlace = function() {
element.fadeTo(250,0.6).css("-" + cssPrefix + "-transform","rotate(" + startDeg + "deg)");
};
element.attr("style","top:" + $random(availableHeight) + "px; left:" + $random(availableWidth) + "px; z-index:" + zIndex).hover(function() {
element.fadeTo(250,1).css("zIndex",++zIndex).css("-" + cssPrefix + "-transform","rotate(0deg)");
},resetPlace);
resetPlace();
});

});

</script>

When the mouseenter event occurs, the rotation is animated to 0, no rotation. When the mouse leaves the element, the element animates to its initial random rotation. You'll also note that I've used opacity to add to the subtle effect.

source article : davidwalsh.name/fade-spin-css3-jquery 

Related Posts Plugin for WordPress, Blogger...

0 comments:

Post a Comment

Go
to
Top
© 2011 Deviation. WP Themes by Skatter Tech. Bloggerized by Bambang Wicaksono.
Mouse Movement to the Next Widget
 
BLOG MENU