我为视频滑块库设置了单击功能,当我单击主视频播放/或暂停时,我试图向相应的缩略图添加“活动-缩略图”类。它正在工作并正在添加类。当我再次单击主视频时,它不会删除课程。不确定我在哪里出问题了,有什么建议吗?
jquery:
$('.video-main').on('click', function () {
var mainVideoIndex = $('.video-main').index(this),
activeThumbnail = $(this).closest('.video-playlist').find('.l-thumbnail__title').eq(mainVideoIndex),
$('.l-thumbnail__title').not(activeThumbnail).removeClass('active--thumbnail');
activeThumbnail.addClass('active--thumbnail');
});
答案 0 :(得分:2)
您需要检查单击的项目是否具有活动类别,然后继续添加或删除活动类别
我从另一个答案中借来了html和css
$('.video-main').on('click', function() {
var $this = $(this);
var mainVideoIndex = $('.video-main').index(this),
activeThumbnail = $this.closest('.video-playlist').find('.l-thumbnail__title').eq(mainVideoIndex);
// check if the clicked is the same as currently active item
// if it is then remove active
if (activeThumbnail.hasClass('active--thumbnail')) {
activeThumbnail.removeClass('active--thumbnail');
}
// remove active from all thumbs titles then add active to clicked item
else {
$('.l-thumbnail__title').removeClass('active--thumbnail');
activeThumbnail.addClass('active--thumbnail');
}
});
ul.video-playlist li p {
border-radius: 5px;
padding: 5px;
}
.active--thumbnail {
box-shadow: 0px 0px 5px green;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="video-playlist">
<li>
<p class="l-thumbnail__title">Thumbnail 1</p>
<button class="video-main">Video 1</button>
</li>
<li>
<p class="l-thumbnail__title">Thumbnail 2</p>
<button class="video-main">Video 2</button>
</li>
<li>
<p class="l-thumbnail__title">Thumbnail 3</p>
<button class="video-main">Video 3</button>
</li>
<li>
<p class="l-thumbnail__title">Thumbnail 4</p>
<button class="video-main">Video 4</button>
</li>
</ul>
答案 1 :(得分:0)
请添加一个Codepen以了解您的HTML代码。
但是我在第3行中看到的是(,)而不是(;)。
$('.video-main').on('click', function () {
var mainVideoIndex = $('.video-main').index(this),
activeThumbnail = $(this).closest('.video-playlist').find('.l-thumbnail__title').eq(mainVideoIndex);
$('.l-thumbnail__title').not(activeThumbnail).removeClass('active--thumbnail');
activeThumbnail.addClass('active--thumbnail');
});