2017-05-05 7 views
5

これは、以下のコードを実行するときに最も簡単に理解できます。赤いバーの上にマウスを置いたときに、列と中央の行の両方でホバー状態をトリガーするようにしています。CSSを取得する:重複する兄弟にカーソルを合わせる

私はフレックスに基づいて列を維持し、それらの上に絶対的にバーを配置したいと考えています。

これは可能ですか?

EDIT:

私はマウスが青色にする上にマウスを移動さだけで列をしたいと思います。あいまいさを残して申し訳ありません。スニペットを目的の結果に更新しました。

列は白い線で分けられます。灰色の領域にカーソルを合わせると、列がハイライト表示されます。

ありがとうございました。

.root { 
 
    width: 100px; 
 
    height: 100px; 
 
    background: grey; 
 
    position: relative; 
 
    display: flex; 
 
} 
 

 
.column { 
 
    display: flex; 
 
    flex: 1 1 auto; 
 
    border-right: 1px solid white; 
 
} 
 

 
.column:hover { 
 
    background: blue; 
 
} 
 

 
.bar { 
 
    position: absolute; 
 
    left: 0; 
 
    right: 0; 
 
    top: 33px; 
 
    bottom: 33px; 
 
    background: red; 
 
} 
 

 
.bar:hover { 
 
    background: green; 
 
} 
 

 
.green { 
 
    background: green; 
 
} 
 

 
.blue { 
 
    background: blue; 
 
}
Hover over the middle of the square. I want the middle column to turn blue and the bar to turn green. 
 
Right now, only the bar turns green. 
 

 
<div class='root'> 
 
    <div class='column'> 
 
    </div> 
 
    <div class='column'> 
 
    </div> 
 
    <div class='column'> 
 
    </div> 
 
    <div class='bar'> 
 
    </div> 
 
</div> 
 

 
Desired result: 
 

 
<div class='root'> 
 
    <div class='column'> 
 
    </div> 
 
    <div class='column blue'> 
 
    </div> 
 
    <div class='column'> 
 
    </div> 
 
    <div class='bar green'> 
 
    </div> 
 
</div>

最終編集:

私は私のユースケースが何であるかを完全に肉付けバージョンを提供しています。私はCSSがこれを解決できるとは思わないが、私は私の元の質問のために働く答えを受け入れた。あなたが行くあり

function enterColumn() { 
 
    document.getElementById('column-status').innerHTML = 'In column' 
 
} 
 

 
function leaveColumn() { 
 
    document.getElementById('column-status').innerHTML = 'Out of column' 
 
} 
 

 
function enterBar() { 
 
    document.getElementById('bar-status').innerHTML = 'In bar' 
 
} 
 

 
function leaveBar() { 
 
    document.getElementById('bar-status').innerHTML = 'Out of bar' 
 
}
.root { 
 
    width: 100px; 
 
    height: 100px; 
 
    background: grey; 
 
    position: relative; 
 
    display: flex; 
 
} 
 

 
.column { 
 
    display: flex; 
 
    flex: 1 1 auto; 
 
    border-right: 1px solid white; 
 
} 
 

 
.column:hover { 
 
    background: blue; 
 
} 
 

 
.bar-container { 
 
    position: absolute; 
 
    left: 0; 
 
    right: 0; 
 
    top: 33px; 
 
    bottom: 33px; 
 
} 
 

 
.bar { 
 
    position: absolute; 
 
    top: 0; 
 
    bottom: 0; 
 
    background: red; 
 
} 
 

 
.bar:hover { 
 
    background: green; 
 
} 
 

 
.green { 
 
    background: green; 
 
} 
 

 
.blue { 
 
    background: blue; 
 
}
Hovering over a column or bar should be independent. Right now you can never have the 'In column' and 'In bar' status at the same time :(
 
<br /> 
 
It should scale to support any number of columns and any number of bars (where bars can be absolutely positioned anywhere along the x-axis) 
 
<br /> 
 
Javascript events should be called on mouse events for both columns and bars. 
 

 
<div class='root'> 
 
    <div class='column' onmouseenter='enterColumn();' onmouseleave='leaveColumn()'> 
 
    </div> 
 
    <div class='column' onmouseenter='enterColumn();' onmouseleave='leaveColumn()'> 
 
    </div> 
 
    <div class='column' onmouseenter='enterColumn();' onmouseleave='leaveColumn()'> 
 
    </div> 
 
    <div class='bar-container'> 
 
    <div class='bar' style='left: 5px; right: 40px' onmouseenter='enterBar();' onmouseleave='leaveBar()'> 
 
    </div> 
 
    <div class='bar' style='left: 65px; right: 5px' onmouseenter='enterBar();' onmouseleave='leaveBar()'> 
 
    </div> 
 
    </div> 
 
</div> 
 

 
<div id='column-status'> 
 
    Out of column 
 
</div> 
 
<div id='bar-status'> 
 
    Out of bar 
 
</div>

+0

CSSを推移している要素を_where_知る方法がないので、これは可能性が高いだけではCSSが解決しようとするつもりはない以上のものです。 – chazsolo

+0

@chazsolo私はポインタイベントのように動作するポインタイベントのようなものを期待していましたが、それでもまだ:ホバーセレクタとonmouseover javascriptイベントを引き起こしました。私はこれのためにjavascriptを使用しなければならないように見えるようになった。 –

答えて

2

、試行錯誤の2時間後、私は最終的にこの小さなハックを思い付きました。

.root { 
 
    width: 100px; 
 
    height: 100px; 
 
    background: grey; 
 
    position: relative; 
 
    display: flex; 
 
} 
 

 
.column { 
 
    display: flex; 
 
    flex: 1 1 auto; 
 
    border-right: solid #fff 1px; 
 
} 
 

 
.column:hover { 
 
    background: blue; 
 
} 
 

 
.column .toggle{ 
 
    margin-top:33px; 
 
    height: 33px; 
 
    width: 100%; 
 
} 
 

 
.column .toggle:before{ 
 
    content: ""; 
 
    position: absolute; 
 
    width: 34px; 
 
    height: 33px; 
 
} 
 

 
.column .toggle:hover:after{ 
 
    content: ""; 
 
    position: absolute; 
 
    z-index: 10; 
 
    left: 0; 
 
    right: 0; 
 
    top: 33px; 
 
    bottom: 33px; 
 
    background: green; 
 
    pointer-events:none; 
 
} 
 

 
.bar { 
 
    position: absolute; 
 
    z-index: 1; 
 
    left: 0; 
 
    right: 0; 
 
    top: 33px; 
 
    bottom: 33px; 
 
    background: red; 
 
    pointer-events:none; 
 
}
<div class='root'> 
 
    <div class='column'><div class='toggle'></div></div> 
 
    <div class='column'><div class='toggle'></div></div> 
 
    <div class='column'><div class='toggle'></div></div> 
 
    <div class='bar'></div> 
 
</div>

あなたは.bar要素にいくつかのJavaScriptのイベントをバインドする必要がある場合は今、その代わり.toggleに取り付けます。

+0

1つではなく、すべての列の背景が変更されます –

+0

それがあなたの探しているものかどうか教えてください。 –

+1

賢いハック。私はそれを私のユースケースではうまくいきませんが、それを受け入れています。私はバーが複数のバーを含む可能性があり、それらはJavaScriptのイベントをトリガする必要があると述べたはずです。実際のユースケースで質問を更新しますが、CSSをあまりにも遠くに押し込もうとしています。私はこの正確な能力を持つことをあきらめています。 –

0

私はあなたが何を意味するか理解していれば、あなたはどんな要素の上にマウスを置く場合をオンにする必要があり.column.barをオンにする必要があることを意味します。それが事実なら、それは実際にはかなり簡単です。その後、チェックアウトすること」ではない場合場合

.root { 
 
    width: 100px; 
 
    height: 100px; 
 
    background: grey; 
 
    position: relative; 
 
    display: flex; 
 
} 
 

 
.column { 
 
    display: flex; 
 
    flex: 1 1 auto; 
 
    border-right: 1px solid white; 
 
} 
 

 
.bar { 
 
    position: absolute; 
 
    left: 0; 
 
    right: 0; 
 
    top: 33px; 
 
    bottom: 33px; 
 
    background: red; 
 
} 
 

 
.root:hover .bar { 
 
    background: green; 
 
} 
 

 
.root:hover .column { 
 
    background: blue; 
 
}
<div class='root'> 
 
    <div class='column'> 
 
    </div> 
 
    <div class='column'> 
 
    </div> 
 
    <div class='column'> 
 
    </div> 
 
    <div class='bar'> 
 
    </div> 
 
</div>

と、あなたは.barにカーソルを合わせると.columnの色を変更したい:だけではなく、そのよう.root要素であなたのhoverイベントを配置下のスニペット。 HTMLマークアップを少し変更したことに注意してください。 .barposition: absoluteなので、.root要素内に配置する場所にはまったく影響しません。

.root { 
 
    width: 100px; 
 
    height: 100px; 
 
    background: grey; 
 
    position: relative; 
 
    display: flex; 
 
} 
 

 
.column { 
 
    display: flex; 
 
    flex: 1 1 auto; 
 
    border-right: 1px solid white; 
 
} 
 

 
.bar { 
 
    position: absolute; 
 
    left: 0; 
 
    right: 0; 
 
    top: 33px; 
 
    bottom: 33px; 
 
    background: red; 
 
} 
 

 
.bar:hover { 
 
    background: green; 
 
} 
 

 
.bar:hover ~ .column { 
 
    background: blue; 
 
}
<div class='root'> 
 
    <div class='bar'> 
 
    </div> 
 
    <div class='column'> 
 
    </div> 
 
    <div class='column'> 
 
    </div> 
 
    <div class='column'> 
 
    </div> 
 
</div>

それはあなたがdivsの再配置が許可されている場合、あなただけの真ん中.column.barを配置し、隣接兄弟セレクタを使用することができます:-)

1

助けなら、私に教えてください。

.root { 
 
    width: 100px; 
 
    height: 100px; 
 
    background: grey; 
 
    position: relative; 
 
    display: flex; 
 
} 
 

 
.column { 
 
    display: flex; 
 
    flex: 1 1 auto; 
 
    border-right: 1px solid white; 
 
} 
 

 
.column:hover { 
 
    background: blue; 
 
} 
 

 
.bar { 
 
    position: absolute; 
 
    left: 0; 
 
    right: 0; 
 
    top: 33px; 
 
    bottom: 33px; 
 
    background: red; 
 
} 
 

 
.bar:hover { 
 
    background: green; 
 
} 
 

 
.bar:hover + .column { 
 
    background: blue; 
 
} 
 

 
.green { 
 
    background: green; 
 
} 
 

 
.blue { 
 
    background: blue; 
 
}
<div class='root'> 
 
    <div class='column'> 
 
    </div> 
 
    <div class='bar'> 
 
    </div> 
 
    <div class='column'> 
 
    </div> 
 
    <div class='column'> 
 
    </div> 
 
</div>
.bar:hover + .column { 
    background: blue; 
} 

+0

あなたはこの質問をよく理解していないと思います。それは少し曖昧です。 OPはホバー効果がセントラルコラムだけでなく、3つすべてに影響することを望んでいます。 –

関連する問題