简体   繁体   English

除了在jQuery中悬停的元素之外,如何更改所有内容的不透明度?

[英]How can I change the opacity of everything but the element I'm hovering over in jQuery?

I am trying to have people highlight over a laptop image and once they do so, I want everything but the laptop image I'm hovering over to ease into a black opacity color. 我试图让人们在笔记本电脑图像上突出显示,一旦这样做,我希望除了悬停的笔记本电脑图像以外的所有内容,以减轻黑色不透明的颜色。

This is the code I have so far which makes everything in the body to change opacity: 到目前为止,这是我拥有的代码,它使体内的所有东西都改变不透明度:

$(document).ready(function () {
    $(".laptop_img").mouseenter(function() {
        console.log("Mouse over laptop");
        $("body").css("opacity", "0.5");
    });

    $(".laptop_img").mouseleave(function() {
        console.log("Mouse leaves laptop");
        $("body").css("opacity", "1");
    });
});

I think an easier way to achieve this would be to create a fixed black semi-transparent element that is the size of the window, on top of everything, make it hidden by default and show it whenever the mouse hovers laptop_img . 我认为实现此目的的更简单方法是创建一个固定的黑色半透明元素,该元素即窗口的大小,位于所有内容的顶部,默认情况下将其隐藏,并在鼠标悬停laptop_img时显示它。

You would also have to handle the hovered element's z-index so that it would appear on top of your black element. 您还必须处理悬停元素的z-index,以便它出现在黑色元素的顶部。

Demo on JSFiddle: http://jsfiddle.net/edc642pz/2/ JSFiddle上的演示: http : //jsfiddle.net/edc642pz/2/

opacity CSS property applies to all the children of the element so if you do it on body then you img tag will have opacity:0.5 too. opacity CSS属性适用于该元素的所有子元素,因此,如果在bodyimg标签也将具有opacity:0.5

So better solution could be add/show dynamically a div witch will be covering all body except the your image. 因此,更好的解决方案是动态添加/显示div巫婆将覆盖除您的图像之外的所有身体。

There are two ways to get what You want: 有两种获取所需内容的方法:

1/ You need to have these two compontents in separate container. 1 /您需要将这两个组件放在单独的容器中。 For example 例如

<div id="div-with-opacity" style="opacity:.7">
      everything what goes here will has opacity also
   </div>
   <img src="path-to-image" alt="" id="this-image-has-no-opacity"/>

2/ Just use rgba(0,0,0,.7) color as a background of layer. 2 /只需使用rgba(0,0,0,.7)颜色作为图层背景即可。 You can have elements inside div with this background and it's not affect these elements. 您可以在具有这种背景的div内包含元素,并且不会影响这些元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM