0

我有一个用 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>

我将如何向当前可以在屏幕上查看的图像添加一个名为“当前”的新类,任何帮助将不胜感激,在此先感谢!

4

1 回答 1

1

两种可能的解决方案:

1)如果你跟踪幻灯片的当前索引,你可以用它来设置类,如下:

$('.current').removeClass('current');
$('.img-game:eq(' + currentIndex + ')').addClass('current');

2)点击左或右时,使用班级的位置将current班级重新分配到其他地方。以下是左键示例:

$('.current').removeClass('current').parent().next().child('.img-game')
    .addClass('current')

对于右键单击,您将使用.prev()而不是.next(). 您还应该检查以确保在更改选择之前存在下一个元素。

于 2013-05-21T17:22:20.457 回答