六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 293|回复: 0

CSS精髓2:JS的回归

[复制链接]

升级  88%

12

主题

12

主题

12

主题

童生

Rank: 1

积分
44
 楼主| 发表于 2013-2-8 00:55:58 | 显示全部楼层 |阅读模式
 
     来源: OurJS.cn   原文:http://www.ourjs.cn/show.php?id=449
 
   2004年3月,Dave Shea写了一篇经典文章《CSS Sprites》,现在他使用JavaScript同样能创造出完美的视角效果。
(要了解更多Dave Shea,可以点击这里)
 
 
JAVASCRIPT:

$(document).ready(function(){

    // remove link background images since we're re-doing the hover interaction below

    // (doing it this way retains the CSS default hover states for non-javascript-enabled browsers)

    // we also want to only remove the image on non-selected nav items, so this is a bit more complicated

    $(".nav").children("li").each(function() {

       var current = "nav current-" + ($(this).attr("class"));

       var parentClass = $(".nav").attr("class");

       if (parentClass != current) {

          $(this).children("a").css({backgroundImage:"none"});

       }

    });    

    // create events for each nav item

    attachNavEvents(".nav", "home");

    attachNavEvents(".nav", "about");

    attachNavEvents(".nav", "services");

    attachNavEvents(".nav", "contact");

    function attachNavEvents(parent, myClass) {

       $(parent + " ." + myClass).mouseover(function() {

          $(this).append('<div class="nav-' + myClass + '"></div>');

          $("div.nav-" + myClass).css({display:"none"}).fadeIn(200);

       }).mouseout(function() {

          $("div.nav-" + myClass).fadeOut(200, function() {

              $(this).remove();

          });

       }).mousedown(function() {

          $("div.nav-" + myClass).attr("class", "nav-" + myClass + "-click");

       }).mouseup(function() {

          $("div.nav-" + myClass + "-click").attr("class", "nav-" + myClass);

       });

    }

});

 
来源: OurJS.cn   原文:http://www.ourjs.cn/show.php?id=449
 
 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表