更改悬停时 2 个项目的不透明度

发布于 2024-11-15 12:21:20 字数 529 浏览 3 评论 0原文

我的页面上有 2 张图片。当我将鼠标悬停在 img.a 上时,它会将不透明度更改为 0 并且工作正常。不过,当将鼠标悬停在 img.a 上时,我希望 img.c 也将不透明度更改为 0。我的代码适用于 img.a,但不适用于 img.c

<script type='text/javascript'>
$(document).ready(function(){
$("img.a").hover(
function() {
$(this).stop().animate({"opacity": "0"}, "slow");
},
function() {
$(this).stop().animate({"opacity": "1"}, "slow");
},
function() {
$("img.c").stop().animate({"opacity": "0"}, "slow");
},
function() {
$("img.c").stop().animate({"opacity": "1"}, "slow");
});

});
</script>

I have 2 images on my page. when I hover over img.a, it changes opacity to 0 and works perfectly. However I would like img.c to also change opacity to 0 when img.a is being hovered over. My code works for img.a but nothing for img.c

<script type='text/javascript'>
$(document).ready(function(){
$("img.a").hover(
function() {
$(this).stop().animate({"opacity": "0"}, "slow");
},
function() {
$(this).stop().animate({"opacity": "1"}, "slow");
},
function() {
$("img.c").stop().animate({"opacity": "0"}, "slow");
},
function() {
$("img.c").stop().animate({"opacity": "1"}, "slow");
});

});
</script>

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

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

发布评论

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

评论(4

记忆之渊 2024-11-22 12:21:20

问题是你的语法。

jQuery的hover()函数只有两个参数——两个函数。第一个是当鼠标悬停在元素上时调用的函数,另一个是当鼠标移出元素时调用的函数。

你已经快完成了,现在你需要做的就是将 4 个函数合并为两个函数,它就可以工作了:

<script type='text/javascript'>
$(document).ready(function(){

  $("img.a").hover(

    function() { // this function is called on mouseover img.a
        $(this).stop().animate({"opacity": "0"}, "slow");
        $("img.c").stop().animate({"opacity": "0"}, "slow");
    },
    function() { // this function is called on mouseout img.a
        $(this).stop().animate({"opacity": "1"}, "slow");
        $("img.c").stop().animate({"opacity": "1"}, "slow");
    }

  });

});
</script>

The problem is your syntax.

jQuery's hover() function only has two arguments - the two functions. The first one is the one which is called when you mouseover the element, and the other one is called when you mouse out.

You're almost there, now all you need to do is combine the 4 functions into two functions and it will work:

<script type='text/javascript'>
$(document).ready(function(){

  $("img.a").hover(

    function() { // this function is called on mouseover img.a
        $(this).stop().animate({"opacity": "0"}, "slow");
        $("img.c").stop().animate({"opacity": "0"}, "slow");
    },
    function() { // this function is called on mouseout img.a
        $(this).stop().animate({"opacity": "1"}, "slow");
        $("img.c").stop().animate({"opacity": "1"}, "slow");
    }

  });

});
</script>
忘东忘西忘不掉你 2024-11-22 12:21:20

您可以使用 $("img.a, img.c") 作为悬停函数中的选择器,而不是使用 $(this)

有关基本示例,请参阅此小提琴

Instead of using $(this), you could use $("img.a, img.c") as your selector in the hover function.

See this fiddle for a basic example.

燕归巢 2024-11-22 12:21:20

将所有应该褪色的图像指定为同一类。
然后为所有应该一起淡入淡出的图像提供相同的数据组

<img class="fade" data-group="a" />
<img class="fade" data-group="b" />
<img class="fade" data-group="a" />

<script type="text/javascript">
$(function(){ /* shorthand for $(document).ready(function(){ */

    $('img.fade').hover(function(){

        $('img.fade[data-group="'+$(this).data('group')+'"]').stop().animate({"opacity": "0"},"slow");

    },function(){

        $('img.fade[data-group="'+$(this).data('group')+'"]').stop().animate({"opacity": "1"},"slow");

    });    

});
</script>

现在,当您将鼠标悬停在其中一张图像上时,同一组中的所有图像都会淡出。

这是我在 jsFiddle 上的示例: http://jsfiddle.net/Rv9jU/

Give all the images that should be faded the same class.
Then give all the images that should be faded together the same data-group.

<img class="fade" data-group="a" />
<img class="fade" data-group="b" />
<img class="fade" data-group="a" />

<script type="text/javascript">
$(function(){ /* shorthand for $(document).ready(function(){ */

    $('img.fade').hover(function(){

        $('img.fade[data-group="'+$(this).data('group')+'"]').stop().animate({"opacity": "0"},"slow");

    },function(){

        $('img.fade[data-group="'+$(this).data('group')+'"]').stop().animate({"opacity": "1"},"slow");

    });    

});
</script>

Now when you hover over one of the images, all the images from the same group will be faded out.

Here is my example on jsFiddle: http://jsfiddle.net/Rv9jU/

做个少女永远怀春 2024-11-22 12:21:20
$(function () {
    $("#image1").css("opacity", 0.3);
    $("#image1").hover(function () {
        $(this).animate({ opacity: 1.0 }, 500);
    }, function () {
        $(this).animate({ opacity: 0.3 }, 500);
    });
})

在 html 页面头部的 script 标签内使用此函数。

$(function () {
    $("#image1").css("opacity", 0.3);
    $("#image1").hover(function () {
        $(this).animate({ opacity: 1.0 }, 500);
    }, function () {
        $(this).animate({ opacity: 0.3 }, 500);
    });
})

Use this function within script tag in html page head section.

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