简体   繁体   English

单击下一个和上一个时,如何从导航菜单中添加和删除类?

[英]How do I add and remove classes from the navigation menu when clicking on next and previous?

I'm trying to implement a carousel with Javascript but I'm having trouble with my navigation buttons. 我正在尝试使用Javascript实现轮播,但导航按钮遇到了麻烦。 I want to add a class active to those items (.carousel-dots-nav-item) when their respective images are showing. 我想在显示它们各自的图像时向这些项目(.carousel-dots-nav-item)添加活动的类。

I figured out how to do it when clicking on the navigation menu but I'm having trouble with the Prev and Next buttons. 单击导航菜单时,我想出了解决方法,但是上一个下一个按钮遇到了麻烦。

You can see the full code at jsfiddle 您可以在jsfiddle上看到完整的代码

Here is the javascript file: 这是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();
});

嗨,请检查jsfiddle上的更新代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 遍历数组时,如何从符合特定条件的元素中添加/删除类? - When looping through an array how do I add/remove classes from elements that fit a specific criteria? 如何向模态添加导航按钮(上一个和下一个) - How to add navigation buttons(previous and next) to a modal 单击下一步按钮时删除以前的 JS function - Remove previous JS function when clicking next button 如何在不破坏它们的情况下删除“上一个”和“下一个”innerHTML - How do I remove the "previous" and "next" innerHTML without breaking them 从使用 Jquery 加载的导航中添加/删除类 - Add/Remove classes from navigation loaded with Jquery 如何使用jQuery将下一个和上一个按钮添加到我的幻灯片? - How do I add next and previous buttons to my slideshow with jQuery? 如何在垂直传送带上添加上一个和下一个 function? - How do I add a previous and next function to on a vertical carousel? 如何在 Tampermonkey 脚本上添加下一个/上一个按钮分页? - How do I add next/previous button pagination on tampermonkey script? 如何使用导航菜单按钮(第一,上一个,下一个,最后一个)插入数组 - How to interate an array with navigation menu buttons (first, previous, next, last) 如何从$(this)中删除下一个元素? - How do I remove the next elemen from $(this)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM