2017-11-22 3 views
0

これはCSSで可能なのかどうかはわかりませんが、要素上にマウスを置くと、より暗くなります(不透明度0.9)その他の要素は軽くなります(不透明度0.1)。要素が何も配置されていないときは、すべての要素の不透明度を0.5にしたいと思います。マークアップは次のようになります:箱のホバリングされた要素をより暗くしますが、他の要素はすべて明るくします

<div class="box a"></div> 
<div class="box b"></div> 
<div class="box c"></div> 
<div class="box d"></div> 

enter image description here

レイアウトは無関係です。

JS/jQueryが必要ですか?

答えて

6

jqueryは必要ありません。これはCSSで行うことができます。下のコードを試してみて、あなたが望むものを作る方法をあなたに教えてくれるでしょう。

.box { 
 
    background-color: blue; 
 
    height: 50px; 
 
    width: 50px; 
 
    margin: 10px; 
 
    opacity: 0.6; 
 
} 
 

 
.wrap:hover .box:hover { 
 
    opacity: 0.9; 
 
} 
 

 
.wrap:hover .box:not(:hover) { 
 
    opacity: 0.4; 
 
}
<div class="wrap"> 
 
    <div class="box a"></div> 
 
    <div class="box b"></div> 
 
    <div class="box c"></div> 
 
    <div class="box d"></div> 
 
</div>

0

それはCSSで可能ですわかりません。しかし、簡単にjQueryのjsのと少しの助けを借りて行うことができます。

$(".box").hover(
 
\t function() { 
 
    \t $(".box").each(function(){ 
 
    \t $(this).css("opacity", 0.1); 
 
    }); 
 
    \t $(this).css("opacity", 0.9); 
 
    }, 
 
    function() { 
 
    \t $(".box").each(function(){ 
 
    \t $(this).css("opacity", 0.5); 
 
    }); 
 
    } 
 
);
.box { 
 
    background-color: green; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="box a">a</div> 
 
<br> 
 
<div class="box b">a</div> 
 
<br> 
 
<div class="box c">a</div> 
 
<br> 
 
<div class="box d">a</div>

+0

いけない '本当にこれを達成するために?JSなぜ –

0

非常にまっすぐ進む.hover()方法は、あなたの意図した動作を与えることができます。

コードスニペットデモンストレーション:

jQuery('.box').hover(function(){ 
 
    jQuery(this).addClass('darken'); /* darken hovered element */ 
 
    jQuery('.box').not(jQuery(this)).addClass('lighten'); /* lighten all other elements */ 
 
}, function(){ 
 
    jQuery('.box').removeClass('darken lighten'); /* reset after hover out */ 
 
});
.box { 
 
    background: #0072ff; 
 
    width: 55px; 
 
    height: 55px; 
 
    display: inline-block; 
 
    transition: .7s; 
 
    opacity: .5; 
 
} 
 

 
.darken { 
 
    opacity: .9; 
 
} 
 

 
.lighten { 
 
    opacity: .1; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="box a"></div> 
 
<div class="box b"></div> 
 
<div class="box c"></div> 
 
<div class="box d"></div>

0

は本当にこれを達成するために、JS!基本的なCSSの知識が必要です。 :)

.Container { 
 
    cursor: default; 
 
} 
 

 
.Container:hover .Element:not(:hover) { 
 
opacity: 0.1; 
 
} 
 

 

 
.Element { 
 
    width: 50px; 
 
    height: 50px; 
 
    background-color: rebeccapurple; 
 
    display: inline-block; 
 
    margin: 16px; 
 
    cursor: pointer; 
 
    opacity: 0.5; 
 
    transition: all 200ms; 
 
} 
 

 

 
.Element:hover { 
 
    opacity: 0.9; 
 
}
<div class="Container"> 
 
    <div class="Element"></div> 
 
    <div class="Element"></div> 
 
    <div class="Element"></div> 
 
    <div class="Element"></div> 
 
</div>

+0

簡単な何かのためのjQueryを使用!'コメント? OPはそれが必要であると言っているわけではない、彼は単にそれがあるかどうか尋ねている。 –

+0

あなたの解決策の両方が、望ましい結果ではない、atm。これは純粋なCSSではできませんが、おそらくCSS4で可能になります。 – VXp

+0

@VXpこれがどのようにOPが求めているものにならないのか分かりませんか? –