jquery - 使用 jQuery 改变不透明度

标签 jquery css mouseover opacity mouseout

我在一个网格上有 9 个元素,我希望所有元素的每个元素都具有 0.5 的不透明度,并且只有当鼠标悬停在上面时,div/元素和里面的所有内容都应该具有 1.0 的不透明度。

这是JS

$('.gallery-single').css({ opacity: 0.5 });

$('.gallery-single a').mouseover(function(){
    $('.gallery-single-title', this).css('display', 'block');
        $('.gallery-single', this).css({ opacity: 1 });
});
$('.gallery-single a').mouseout(function(){
    $('.gallery-single-title', this).css('display', 'none');
        $('.gallery-single', this).css({ opacity: 0.5 });
}); 

HTML

<div class="gallery-single">
<a href="#" title="">
<div class="gallery-single-title hide">Some text goes here</div>
<div class="gallery-single-img"><img src="http://img.youtube.com/vi/code/0.jpg" width="300" height="200" /></div>
</a>
</div>

加载时所有元素的不透明度均为 0.5,但聚焦时不透明度不会改变。 我在这里做错了什么?

最佳答案

试试这个:

$('.gallery-single a').hover(function(){
    $(this).closest('.gallery-single-title').css('display', 'block');
        $(this).closest('.gallery-single').css({ opacity: 1 });
},
function(){
    $(this).closest('.gallery-single-title').css('display', 'none');
        $(this).closest('.gallery-single').css({ opacity: 0.5 });
}); 

Working example .

关于jquery - 使用 jQuery 改变不透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6274495/

相关文章:

用于 WP8 (Lumia 920) 设备的 jQuery/HTML5/gwt 应用程序 : vertical css scroll fix

jquery 将鼠标悬停在我的子类别上消失

javascript - 如何使用CSS和Javascript在鼠标悬停时上下滑动?

get - Prototype - 如何获取原型(prototype) 1.6.0.3 中的子元素

javascript - JQuery 保存和读取 $.cookie 存储对象

jQuery 属性选择器 : anything but not ending with specified string?

jquery - 更改 jQuery 下拉菜单的样式

javascript - 如何以编程方式从 Materialise CSS select 中选择一个选项?

jquery - 使用 jquery 更改背景图像

javascript - 在新页面上加载 ajax 成功数据