gpt4 book ai didi

jquery - toggleClass() 对我不起作用

转载 作者:行者123 更新时间:2023-12-01 07:43:22 29 4
gpt4 key购买 nike

我正在尝试设置一个视频库,当我将鼠标悬停在视频缩略图上时,我希望播放按钮从不透明度:.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>

最佳答案

问题是因为 .toggle-opacity-play-button 规则不够具体,无法覆盖 .gallery-poster-play 中的 opacity 设置-按钮img。您需要增加特异性。您还需要使用 $(this).find('img') 仅影响当前元素的子元素,如下所示:

.gallery-poster-play-button img.toggle-opacity-play-button {
opacity: 1;
}

//Fade in Play Button
$('.gallery-poster-play-button').hover(function() {
$(this).find('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;
}

.gallery-poster-play-button img.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>

话虽如此,您根本不需要使用 JS,因为 CSS :hover 伪选择器在这里可以正常工作:

.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;
}
<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>

关于jquery - toggleClass() 对我不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43736118/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com