0

この画像ギャラリーを作成しましたが、アクティブ サムに class="active" を指定したいと考えています。クリックすると.small_imageに「アクティブ」なクラスを与えることができますが、他の親指をクリックすると、それらはすべて「アクティブ」なクラスになります。したがって、if ステートメントを作成すると、大きな画像とサムネイルの rel と src が同じ場合、クラスを「アクティブ」にすることを考えましたが、私のコードは機能しません。誰が私が間違っているのか教えてもらえますか?

http://jsfiddle.net/XNsHC/8/

$(document).ready(function () {
        $(".small_image").click(function () {
            event.preventDefault();
            var image = $(this).prop("rel");
            $('#under').prop('src', image);
            $('#above').fadeOut(600, function () {
                $('#above').prop('src', $('#under').prop('src')).css('display', 'block');
            });
        });

        if($(".small_image").prop("rel") == $("#under").prop('src', image)){
            $(this).addClass('active');

        }
    }); 
4

1 に答える 1

1

次のようなものを試してください: (動作中の jsFiddle - 周囲に赤い境界線を追加.active)

$(".small_image").click(function () {
    event.preventDefault();
    $('.small_image.active').removeClass('active'); // remove "active" from current image
    $(this).addClass('active'); // add "active" to the new one
    var image = $(this).prop("rel");
    $('#under').prop('src', image);
    $('#above').fadeOut(300, function () {
        $('#current_image #above').prop('src', $('#current_image #under').prop('src'));
    });
}).eq(0).addClass('active'); // set the first one as "active" by default
于 2013-08-13T09:21:48.980 に答える