Jquery悬停效果消失

发布于 2024-11-04 04:34:27 字数 1856 浏览 0 评论 0原文

我试图实现一种效果,当您将鼠标悬停在图像上时,它会放大、变暗,而另一张图像会向下滑动。我已经成功实现了这个效果,但是有一个小问题。这是我的代码:

HTML

<div class="viewPort">
<a href="#"><img src="<?php echo bloginfo('template_url'); ?>/images/penguin.jpg" alt="penguin">
<span class="dark"></span><span class="slide"></span></a>
</div>

CSS

.viewPort{float:left; position:relative; border:8px solid #60B1D0; height:300px; width:300px; overflow:hidden;}
.viewPort a{display:block;}
.viewPort img{ position:relative; top:-20px; left:-20px; height:330px;}
.viewPort span.dark{background:url(images/trans.png) repeat; width:100%; height:100%; position:absolute; top:0; left:0; z-index:10; display:none;}
.viewPort span.slide{top:-130px; left:84px; position:absolute; z-index:100; width:130px; height:130px; background:url(images/badge.png) no-repeat;}

jQuery

$(".viewPort a").hover(function(){

            $(this).find('img').stop().animate({
                height: '300', left: '0', top: '0', width: '300'
            }, 250);

            $('span.dark').fadeIn(600);
            $('span.slide').stop().animate({
                    top: '80'
                    }, 600);

        }, function(){
            $('span.slide').stop().animate({
                    top: '-130'
                }, 400);
            $('span.dark').stop().fadeOut(400);

            $('.viewPort img').stop().animate({
                height: '330', left: '-20', top: '-20', width: '330'
            }, 250);
        });

这很好用;但是,如果您在图像上来回悬停几次, 似乎不会受到影响。我看着我的萤火虫控制台,悬停时不透明度正在变化;然而,从视觉上看,你并没有看到它变暗。有什么想法为什么会发生这种情况吗?它在正常悬停时效果很好,但如果你稍微使用它,变暗效果就会消失。

http://jsfiddle.net/7zQFf/

I am trying to achieve an effect where when you hover over an image, it zooms in, darkens, and another image slides down. I have achieved this effect successfully, but there is a small problem. Here is my code:

HTML

<div class="viewPort">
<a href="#"><img src="<?php echo bloginfo('template_url'); ?>/images/penguin.jpg" alt="penguin">
<span class="dark"></span><span class="slide"></span></a>
</div>

CSS

.viewPort{float:left; position:relative; border:8px solid #60B1D0; height:300px; width:300px; overflow:hidden;}
.viewPort a{display:block;}
.viewPort img{ position:relative; top:-20px; left:-20px; height:330px;}
.viewPort span.dark{background:url(images/trans.png) repeat; width:100%; height:100%; position:absolute; top:0; left:0; z-index:10; display:none;}
.viewPort span.slide{top:-130px; left:84px; position:absolute; z-index:100; width:130px; height:130px; background:url(images/badge.png) no-repeat;}

jQuery

$(".viewPort a").hover(function(){

            $(this).find('img').stop().animate({
                height: '300', left: '0', top: '0', width: '300'
            }, 250);

            $('span.dark').fadeIn(600);
            $('span.slide').stop().animate({
                    top: '80'
                    }, 600);

        }, function(){
            $('span.slide').stop().animate({
                    top: '-130'
                }, 400);
            $('span.dark').stop().fadeOut(400);

            $('.viewPort img').stop().animate({
                height: '330', left: '-20', top: '-20', width: '330'
            }, 250);
        });

This works great; however, if you hover back and forth over the image a few times, the <span class="dark"></span> doesn't seem to be affected. I looked at my firebug console, and the opacity is changing on hover; however, visually, you do not see it darkening. Any ideas why this is happening? It works great on normal hovers, but if you play with it for a little bit, the darkening effect disappears.

http://jsfiddle.net/7zQFf/

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

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

发布评论

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

评论(2

一影成城 2024-11-11 04:34:27

尝试将 jQuery 更改为:

$(".viewPort a").hover(function(){

        $(this).find('img').stop().animate({
            height: '300', left: '0', top: '0', width: '300'
        }, 250);

        $('span.dark').stop().fadeTo(600, 1); // .stop().fadeTo()
        $('span.slide').stop().animate({
                top: '80'
                }, 600);

    }, function(){
        $('span.slide').stop().animate({
                top: '-130'
            }, 400);
        $('span.dark').stop().fadeTo(400, 0); // .stop().fadeTo()

        $('.viewPort img').stop().animate({
            height: '330', left: '-20', top: '-20', width: '330'
        }, 250);
    });

在 JSFiddle 上为我工作: http://jsfiddle.net/bFUSX/ (FF4)

Try changing the jQuery to this:

$(".viewPort a").hover(function(){

        $(this).find('img').stop().animate({
            height: '300', left: '0', top: '0', width: '300'
        }, 250);

        $('span.dark').stop().fadeTo(600, 1); // .stop().fadeTo()
        $('span.slide').stop().animate({
                top: '80'
                }, 600);

    }, function(){
        $('span.slide').stop().animate({
                top: '-130'
            }, 400);
        $('span.dark').stop().fadeTo(400, 0); // .stop().fadeTo()

        $('.viewPort img').stop().animate({
            height: '330', left: '-20', top: '-20', width: '330'
        }, 250);
    });

Worked for me on the JSFiddle: http://jsfiddle.net/bFUSX/ (FF4)

妥活 2024-11-11 04:34:27

您应该将 jumpToEnd 参数传递为 true(这样动画就会很快完成),以便在黑暗跨度上调用 stop 时起作用。
这是演示:

http://jsfiddle.net/7zQFf/4/

You should pass the jumpToEnd argument as true (so the animation will be completed quickly) for this to work while calling stop on the dark span.
Here is the demo:

http://jsfiddle.net/7zQFf/4/

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