Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
345 views
in Technique[技术] by (71.8m points)

css - Change state of one div based on hovering list item within a sibling div


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Problem

1: .root .main is not a sibling of .root .menu li.

2: There isn't a CSS selector currently capable of doing what you're trying to do - See the MDN docs. You will need to use a bit javascript/jQuery. Here are a few possible solutions...

Solution 1: Classes

CSS:

/* Default styling */
.menu {
    background: green;
    color: blue;
}

/* Hovered styling */
.menu.hovered {
    background: purple;
    color: red;
}

jQuery:

$('.root .menu li').hover(function() {
    $('.root .main').addClass('hovered');
}, function() {
    $('.root .main').removeClass('hovered');
});

Solution 2: Inline styling

CSS:

/* Default styling */
.menu {
    background: green;
    color: blue;
}

jQuery:

$('.root .menu li').hover(function() {
    $('.root .main').css('background', 'purple');
    $('.root .main').css('color', 'red');
}, function() {
    $('.root .main').attr('style', '');
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...