所以我在图像上有一个按钮,我想改变用户将鼠标悬停在图像上时的颜色。
按钮本身的css
.prev{
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
padding: 16px;
margin-top: -50px;
color: white;
font-weight: bold;
font-size: 20px;
transition: 0.6s ease;
border-radius: 0 3px 3px 0;
user-select: none;
-webkit-user-select: none;
这是html
<body>
<h2 style="text-align:center">Lightbox</h2>
<div class="row">
<div class="column">
<img src="img_nature.jpg" style="width:100%" onclick="openModal();currentSlide(1)" class="hover-shadow cursor">
</div>
<div class="column">
<img src="img_fjords.jpg" style="width:100%" onclick="openModal();currentSlide(2)" class="hover-shadow cursor">
</div>
<div class="column">
<img src="img_mountains.jpg" style="width:100%" onclick="openModal();currentSlide(3)" class="hover-shadow cursor">
</div>
<div class="column">
<img src="img_lights.jpg" style="width:100%" onclick="openModal();currentSlide(4)" class="hover-shadow cursor">
</div>
</div>
<div id="myModal" class="modal">
<span class="close cursor" onclick="closeModal()">×</span>
<div class="modal-content">
<div class="mySlides">
<div class="numbertext">1 / 4</div>
<img src="img_nature_wide.jpg" style="width:100%">
</div>
<div class="mySlides">
<div class="numbertext">2 / 4</div>
<img src="img_fjords_wide.jpg" style="width:100%">
</div>
<div class="mySlides">
<div class="numbertext">3 / 4</div>
<img src="img_mountains_wide.jpg" style="width:100%">
</div>
<div class="mySlides">
<div class="numbertext">4 / 4</div>
<img src="img_lights_wide.jpg" style="width:100%">
</div>
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
<div class="caption-container">
<p id="caption"></p>
</div>
<div class="column">
<img class="demo cursor" src="img_nature_wide.jpg" style="width:100%" onclick="currentSlide(1)" alt="Nature and sunrise">
</div>
<div class="column">
<img class="demo cursor" src="img_fjords_wide.jpg" style="width:100%" onclick="currentSlide(2)" alt="Trolltunga, Norway">
</div>
<div class="column">
<img class="demo cursor" src="img_mountains_wide.jpg" style="width:100%" onclick="currentSlide(3)" alt="Mountains and fjords">
</div>
<div class="column">
<img class="demo cursor" src="img_lights_wide.jpg" style="width:100%" onclick="currentSlide(4)" alt="Northern Lights">
</div>
</div>
</div>
这是我正在尝试使用的CSS。 div.mySlides是包含我的图像的div。
.prev {
color:white;
}
div.mySlides:hover + .prev, .prev:hover {
color:red;
}
我正在使用的javascript
<script>
function openModal() {
document.getElementById('myModal').style.display = "block";
}
function closeModal() {
document.getElementById('myModal').style.display = "none";
}
var slideIndex = 1;
showSlides(slideIndex);
function plusSlides(n) {
showSlides(slideIndex += n);
}
function currentSlide(n) {
showSlides(slideIndex = n);
}
function showSlides(n) {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("demo");
var captionText = document.getElementById("caption");
if (n > slides.length) {slideIndex = 1}
if (n < 1) {slideIndex = slides.length}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
slides[slideIndex-1].style.display = "block";
dots[slideIndex-1].className += " active";
captionText.innerHTML = dots[slideIndex-1].alt;
}
</script>
此时,只有当用户将鼠标悬停在按钮位置时,颜色才会发生变化。我试过改变css中的顺序只是为了找不到任何效果。
答案 0 :(得分:1)
所以你已经基本上工作了,只是为了最后一个.mySlides元素,因为你只选择了紧随其后的.prev兄弟(使用相邻兄弟组合 +
),而不是所有具有该类的兄弟姐妹,无论他们来自徘徊的div之后有多远“ - ~
,一般兄弟组合。
以下基本考试表明它原则上有效(无需添加实际图像或其他样式。)
.prev {
color:blue;
}
div.mySlides {
margin:.5em 0;
padding: .5em;
border: 1px solid;
cursor: pointer;
}
div.mySlides:hover ~ .prev, .prev:hover {
color:red;
}
<div id="myModal" class="modal">
<div class="modal-content">
<div class="mySlides">
<div class="numbertext">1 / 4</div>
</div>
<div class="mySlides">
<div class="numbertext">2 / 4</div>
</div>
<div class="mySlides">
<div class="numbertext">3 / 4</div>
</div>
<div class="mySlides">
<div class="numbertext">4 / 4</div>
</div>
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
</div>
答案 1 :(得分:0)
尝试以下代码
#a:hover ~ #b {
color: #ccc
}
<div id="a">Div A</div>
<div>elements</div>
<div>elements</div>
<div>elements</div>
<div id="b">Div B</div>
答案 2 :(得分:0)
button{
padding:30px;
color:white;
background-color: red;
}
img:hover + button{
background-color: black;
}
<img src="https://www.google.ca/logos/doodles/2018/har-gobind-khoranas-96th-birthday-4731112378073088-s.png" width="200" />
<button>my button</button>
答案 3 :(得分:0)
更改此
div.mySlides:hover + .prev, .prev:hover {
color:red;
}
到这个
div.mySlides:hover ~ .prev, .prev:hover {
color:red;
}
参见https://developer.mozilla.org/en-US/docs/Web/CSS/Adjacent_sibling_selectors vs。https://developer.mozilla.org/en-US/docs/Web/CSS/General_sibling_selectors了解更多信息。