如何在精灵导航中使用 jQuery 添加淡入淡出效果

发布于 2024-09-27 11:34:24 字数 1478 浏览 2 评论 0原文

我有一个用精灵制作的导航。它几乎是完美的,除了客户想要悬停打开和悬停关闭效果之外。我使用 :hover 来实现这一点,但他也希望对精灵有淡入淡出效果。

这是我当前正在处理的网站。

我尝试了这一点:

  $("nav ul a").hover(function(){
  $(this).stop().fadeTo(300, 0.9);
}, function(){
    $(this).stop().fadeTo(300, 0.1);
}).fadeTo(0, 0.7);

但这使得整个导航透明。那不是我真正想要的。我确实在导航下方添加了深色背景,但看起来仍然不正确。我想不出还有其他方法可以做到这一点。

CSS 如下:

nav ul {  position: relative; z-index: 1000;  margin: -26px 0 0 11px; overflow: hidden; }
nav ul li { display: inline; }
nav ul li a { background: url(../images/nav-sprite.png); display: block; float: left; text-indent: -999px; margin: 0 auto; width: 667px; }    nav ul a#nav-1 { background-position: 0 0;  height: 48px; width: 103px; }
nav ul a#nav-2 { background-position: -103px 0;  height: 48px; width: 129px; }
nav ul a#nav-3 { background-position: -234px 0;  height: 48px; width: 156px; }
nav ul a#nav-4 { background-position: -392px 0;  height: 48px; width: 147px; }
nav ul a#nav-5 { background-position: -541px 0;  height: 48px; width: 124px; }
nav ul a#nav-1:hover { background-position: 0 48px; }
nav ul a#nav-2:hover { background-position: -103px 48px; }
nav ul a#nav-3:hover { background-position: -234px 48px; }
nav ul a#nav-4:hover { background-position: -392px 48px; }
nav ul a#nav-5:hover { background-position: -541px 48px; }

所以现在的默认导航不能具有透明的外观。它需要是正常的。但是当我将鼠标悬停在按钮上时,我希望悬停效果淡入和淡出......可以这样做吗?

I have a navigation that is made with sprites. It's almost perfect except the client wants a hover on and hover off effect on it. I used :hover for that but he would also like to have fadein effect with the sprites.

Here is the site I am currently working on.

I tried this bit:

  $("nav ul a").hover(function(){
  $(this).stop().fadeTo(300, 0.9);
}, function(){
    $(this).stop().fadeTo(300, 0.1);
}).fadeTo(0, 0.7);

But that makes the whole navigation transparent. Thats not really what I want. I did add a dark background underneath the nav but it still didn't look right. I can't think of no other way to do this.

Heres the CSS:

nav ul {  position: relative; z-index: 1000;  margin: -26px 0 0 11px; overflow: hidden; }
nav ul li { display: inline; }
nav ul li a { background: url(../images/nav-sprite.png); display: block; float: left; text-indent: -999px; margin: 0 auto; width: 667px; }    nav ul a#nav-1 { background-position: 0 0;  height: 48px; width: 103px; }
nav ul a#nav-2 { background-position: -103px 0;  height: 48px; width: 129px; }
nav ul a#nav-3 { background-position: -234px 0;  height: 48px; width: 156px; }
nav ul a#nav-4 { background-position: -392px 0;  height: 48px; width: 147px; }
nav ul a#nav-5 { background-position: -541px 0;  height: 48px; width: 124px; }
nav ul a#nav-1:hover { background-position: 0 48px; }
nav ul a#nav-2:hover { background-position: -103px 48px; }
nav ul a#nav-3:hover { background-position: -234px 48px; }
nav ul a#nav-4:hover { background-position: -392px 48px; }
nav ul a#nav-5:hover { background-position: -541px 48px; }

So the default navigation right now can'thave that transparent look. It needs to be normal. But when I hover over a button I want the hover effect to fade in and out.....Can this be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷雾森÷林ヴ 2024-10-04 11:34:24

无法使用精灵实现淡入淡出效果,您必须在 html 中使用图像并使用另一个图像作为背景,因此您只能更改 html 图像的不透明度。

No way to a fade effect with a sprite, you have to use an image in your html and another as background, so you will be able to change opacity only of the html image.

路弥 2024-10-04 11:34:24

听起来您想要的是在淡出后不使其透明,在这种情况下,您应该更改鼠标离开淡入淡出时的不透明度,目前您将其设置为 0.1 或 10%不透明度,但您以 70% 的不透明度开始,所以您似乎希望它回到 70%,如下所示:

$("nav ul a").hover(function(){
  $(this).stop().fadeTo(300, 0.9);
}, function(){
  $(this).stop().fadeTo(300, 0.7); //<--change this to 0.7
}).fadeTo(0, 0.7);

您可以在这里测试

It sounds like what you want is to not make it transparent after in the fadeout, in which case you should change the opacity on the mouse-leaving fade, currently you have it going to 0.1, or 10% opacity, but you're starting it at 70% opacity, so it seems you'd want it to go back to 70%, like this:

$("nav ul a").hover(function(){
  $(this).stop().fadeTo(300, 0.9);
}, function(){
  $(this).stop().fadeTo(300, 0.7); //<--change this to 0.7
}).fadeTo(0, 0.7);

You can test it here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文