我有一个用 JQuery 定制的幻灯片;
$(document).ready(function () {
$animating_slideshow = false;
$animation_speed = 1000;
$amount_of_slides = $("#slider").find(".img-games").find(".img-game").length;
$("#slider-left").on("click", function() {
if (!$animating_slideshow) {
$slidden = parseInt($(this).parent().find(".img-games").css("margin-left"));
if ($slidden > 0 || $slidden < 0) {
$(this).parent().find(".img-games").animate({"margin-left":"+=170"}, $animation_speed, function() {
$animating_slideshow = false;
});
$animating_slideshow = true;
} else {
$(this).parent().find(".img-games").animate({"margin-left":-($amount_of_slides-1) * 170}, $animation_speed, function() {
$animating_slideshow = false;
});
$animating_slideshow = true;
}
}
});
$("#slider-right").on("click", function() {
if (!$animating_slideshow) {
$slidden = parseInt($(this).parent().find(".img-games").css("margin-left"));
if ($slidden > -($amount_of_slides-1) * 170) {
$(this).parent().find(".img-games").animate({"margin-left":"-=170"}, $animation_speed, function() {
$animating_slideshow = false;
});
$animating_slideshow = true;
} else {
$(this).parent().find(".img-games").animate({"margin-left":"0"}, $animation_speed, function() {
$animating_slideshow = false;
});
$animating_slideshow = true;
}
}
});
});
这是我的 html...
<div id="slider">
<div class="img-games">
<div id="this_slide" class="img-game">
<img src="http://www.maxdamage.org/wp-content/uploads/2012/05/pokemon-gameboy.jpg" width='170px' height='115px' class='degrades' alt='pokemon' title='Pokemon'>
</div>
<div class="img-game">
<img src="http://1.bp.blogspot.com/-U62BIDPN79U/UV1hPr9xtAI/AAAAAAAAENs/mKIrEDb4RAw/s320/Super_mario_land.png" width='170px' height='115px' class='degrades' alt='Mario' title='Mario'>
</div>
<div class="img-game">
<img src="http://1.bp.blogspot.com/-J7x8JfY21Yk/UUea7UsjUZI/AAAAAAAAA0M/E2qpkAZFsnM/s1600/links_awakening2.png" width="170px" height="115px" class="degrades" alt='Zelda' title='Zelda'>
</div>
<div class="img-game">
<img src="http://www.consoleclassix.com/info_img/Rayman_GBC_ScreenShot2.gif" width="170px" height="115px" class="degrades" alt='Rayman' title='Rayman'>
</div>
</div>
</div>
我将如何为当前可以在屏幕上查看的图像添加一个名为“current”的新类,我们将不胜感激,在此先感谢您!
最佳答案
两种可能的解决方案:
1) 如果您跟踪幻灯片放映的当前索引,您可以使用它来设置类,如下所示:
$('.current').removeClass('current');
$('.img-game:eq(' + currentIndex + ')').addClass('current');
2) 单击左键或右键时,使用当前
类的位置将类重新分配到其他地方。以下是左键单击示例:
$('.current').removeClass('current').parent().next().child('.img-game')
.addClass('current')
对于右键单击,您将使用 .prev()
而不是 .next()
。您还应该包括检查以确保在更改选择之前存在下一个元素。
关于jquery - 如何将类添加到幻灯片中的当前图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16675492/