【问题标题】:How do I add and remove classes from the navigation menu when clicking on next and previous?单击下一个和上一个时,如何从导航菜单中添加和删除类?
【发布时间】:2015-03-07 12:24:05
【问题描述】:

我正在尝试使用 Javascript 实现轮播,但我的导航按钮有问题。当它们各自的图像显示时,我想为这些项目(.carousel-dots-nav-item)添加一个活动类。

我在点击导航菜单时想出了如何操作,但我在使用 PrevNext 按钮时遇到了问题。

您可以在jsfiddle查看完整代码

这里是 javascript 文件:

$(document).ready(function() {


    var carouselItems = [
        { src: "http://placehold.it/600x300/cccccc/000000", title: "Sample 01" },
        { src: "http://placehold.it/600x300/f45a45/000000", title: "Sample 02" },
        { src: "http://placehold.it/600x300/b78d65/000000", title: "Sample 03" },
        { src: "http://placehold.it/600x300/666aa0/000000", title: "Sample 04" },
        { src: "http://placehold.it/600x300/cccddd/000000", title: "Sample 05" }
    ];

    Carousel = function() {
        // keep track of the current position
        var position = 0;

        // build carousel based on items in the carouselItems array
        $(carouselItems).each(function(index, value){
            var li = $('<li/>');
            li.addClass('carousel-item');
            li.css('width', 100 / carouselItems.length + '%');
            li.appendTo($('#carousel'));

            var img = $('<img/>');
            img.attr('src', value.src);
            img.attr('title', value.title);
            img.appendTo(li);

            var liDot = $('<li/>');
            liDot.data('position', index); // Store the position of the respectives images & dots
            liDot.data('title',value.title); //Store the image titles on each dot instance
            liDot.addClass('carousel-dots-nav-item').html('o');
            liDot.appendTo($('#carousel-dots-nav'));
        });

        //increase width of the carousel
        $('#carousel').css('width', carouselItems.length * 100 + '%');

        //add events to dots
        for (i = 0; i < $('.carousel-dots-nav-item').length; i++) {
            var dot = $('.carousel-dots-nav-item')[i];

            // show the title of the image when hovering the associated dot
            $(dot).hover(function(e){
                //$('#title').text(carouselItems[i].title);
                $('#title').text($(this).data('title'));

            }, function(e){
                $('#title').text('');
            });

            // move to the appropriate slide when a dot is clicked
            $(dot).click(function(e){
                //position = i; //i -> $('.carousel-dots-nav-item').length
                var position= $(this).data('position');  //Position based on the index
                $('.active').removeClass('active'); //Reset classes .active from all dots
                $(this).addClass('active'); //Add class .active to clicked dot
                $('#carousel').animate({
                    left: -position * 100 + '%'
                }, 500);
            });
        }


        // add click event to next button
        $("#next").click(function(e){
            e.preventDefault();

            if (position == carouselItems.length - 1) return;
            $('.active').removeClass('active');
            position++;
            //$('.carousel-dots-nav-item').data('position');

            $('#carousel').animate({
                left: -position * 100 + '%'
            }, 500);
        });

        // add click event to previous button
        $("#previous").click(function(e){
            e.preventDefault();

            if (position == 0) return;
            $('.active').removeClass('active');

            position--;
            $('#carousel').animate({
                left: -position * 100 + '%'
            }, 500);
        });
    };

    var carousel = new Carousel();
});

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    您好,请查看jsfiddle上的更新代码

    $('#carousel-dots-nav li').eq(position).addClass('active');
    

    【讨论】:

    • 几乎但是有一个错误。尝试单击第四个元素的第三个,然后尝试使用下一个和上一个。这有点偏离目标。
    • 在使用下一个和上一个之前使用导航时仍然会表现不佳。我在您更新的答案中为活动类添加了一种颜色,以帮助查看它。 jsfiddle.net/vinicius5581/dh1us9z8/4
    • 好的。对不起,我现在离开我的办公室。
    • 我建议将活动索引保存在全局变量中,并根据该索引直接分配活动类
    • 嗨,您是否检查了我的答案中的更新链接?它工作正常。请检查
    猜你喜欢
    • 1970-01-01
    • 2014-06-13
    • 2015-05-10
    • 1970-01-01
    • 1970-01-01
    • 2016-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多