我正在尝试设置视频库,当我将鼠标悬停在视频缩略图上时,我希望我的播放按钮从不透明度:.3变为不透明度:1。 我试图通过使用带有toggleClass()的jQuery脚本来实现这一目标。我尝试过使用不透明度的animate(),这有效。但我想改为使用toggleClass()方法。但是当我悬停时没有任何反应。
//Fade in Play Button
$('.gallery-poster-play-button').hover(function() {
$('.gallery-poster-play-button img').toggleClass('toggle-opacity-play-button');
});

.video-list {
display: flex;
justify-content: flex-start;
list-style: none;
}
.video-list li {
margin-right: 10px;
transition: all .3s;
}
.video-list li:hover {
transform: scale(0.98);
cursor: pointer;
}
.video-list li img {
border-radius: 4px;
}
.video-list li a {
display: block;
padding: 5px;
text-decoration: none;
}
.play-button {
position: relative;
}
.gallery-poster-play-button {
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
}
.gallery-poster-play-button img {
position: absolute;
left: 40%;
top: 35%;
vertical-align: middle;
opacity: .3;
}
.toggle-opacity-play-button {
opacity: 1;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="video-gallery" class="video-list">
<li class="play-button" data-poster="../assets/videos/posters/video-poster-1.png" data-sub-html="video caption1" data-html="#video1">
<img src="../assets/videos/posters/video-poster-11.png" />
<div class="gallery-poster-play-button">
<img src="../assets/data/node_modules/lightgallery/dist/img/video-play.png" alt="">
</div>
</li>
<li class="play-button" data-poster="../assets/videos/posters/video-poster-2.png" data-sub-html="video caption2" data-html="#video2">
<img src="../assets/videos/posters/video-poster-22.png" />
<div class="gallery-poster-play-button">
<img src="../assets/data/node_modules/lightgallery/dist/img/video-play.png" alt="">
</div>
</li>
</ul>
&#13;
答案 0 :(得分:3)
问题是因为.toggle-opacity-play-button
规则不够具体,无法覆盖opacity
的{{1}}设置。你需要增加特异性。您还需要使用.gallery-poster-play-button img
仅影响当前元素的子元素,如下所示:
$(this).find('img')
.gallery-poster-play-button img.toggle-opacity-play-button {
opacity: 1;
}
&#13;
//Fade in Play Button
$('.gallery-poster-play-button').hover(function() {
$(this).find('img').toggleClass('toggle-opacity-play-button');
});
&#13;
.video-list {
display: flex;
justify-content: flex-start;
list-style: none;
}
.video-list li {
margin-right: 10px;
transition: all .3s;
}
.video-list li:hover {
transform: scale(0.98);
cursor: pointer;
}
.video-list li img {
border-radius: 4px;
}
.video-list li a {
display: block;
padding: 5px;
text-decoration: none;
}
.play-button {
position: relative;
}
.gallery-poster-play-button {
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
}
.gallery-poster-play-button img {
position: absolute;
left: 40%;
top: 35%;
vertical-align: middle;
opacity: .3;
}
.gallery-poster-play-button img.toggle-opacity-play-button {
opacity: 1;
}
&#13;
话虽如此,你根本不需要使用JS,因为CSS <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="video-gallery" class="video-list">
<li class="play-button" data-poster="../assets/videos/posters/video-poster-1.png" data-sub-html="video caption1" data-html="#video1">
<img src="../assets/videos/posters/video-poster-11.png" />
<div class="gallery-poster-play-button">
<img src="../assets/data/node_modules/lightgallery/dist/img/video-play.png" alt="">
</div>
</li>
<li class="play-button" data-poster="../assets/videos/posters/video-poster-2.png" data-sub-html="video caption2" data-html="#video2">
<img src="../assets/videos/posters/video-poster-22.png" />
<div class="gallery-poster-play-button">
<img src="../assets/data/node_modules/lightgallery/dist/img/video-play.png" alt="">
</div>
</li>
</ul>
伪选择器在这里可以正常工作:
:hover
&#13;
.video-list {
display: flex;
justify-content: flex-start;
list-style: none;
}
.video-list li {
margin-right: 10px;
transition: all .3s;
}
.video-list li:hover {
transform: scale(0.98);
cursor: pointer;
}
.video-list li img {
border-radius: 4px;
}
.video-list li a {
display: block;
padding: 5px;
text-decoration: none;
}
.play-button {
position: relative;
}
.gallery-poster-play-button {
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
}
.gallery-poster-play-button img {
position: absolute;
left: 40%;
top: 35%;
vertical-align: middle;
opacity: .3;
}
.gallery-poster-play-button:hover img {
opacity: 1;
}
&#13;