jquery - inpage scroll smooth links with button next and prev

前端 未结 1 1514
臣服心动
臣服心动 2021-01-27 08:05

I found this site and i want the way it work but unfortunately I have less knowledge in jquery but I know how to do simple modification to make it work,

my current js c

1条回答
  •  灰色年华
    2021-01-27 08:36

    I have updated your markup and script to do what you want. One of the main issues was that you were using the same class name on the prev & next buttons...

    The updated script uses a single function to control the scrolling. The various buttons update the item index to scroll to, then call that function.

    here's your updated fiddle. and your updated JS.

    var itemIndex = 1;
    function scrollToContent() {
    
        //dont allow zero or greater than 5
        if (itemIndex <= 1) {
            itemIndex = 1;
            $('#fade').hide();
        }
        else{
            $('#fade').show();
        }
        if (itemIndex >= 5) {
            itemIndex = 5;
            $('#fade1').hide();
        }
        else{
            $('#fade1').show();
        }
        //scroll
        $('html, body').animate({
            scrollTop : $('#content' + itemIndex).offset().top
        }, 2000);
        //add & remove active class name
        $('button.active').removeClass('active');
        $('.navigation li:nth-child(' + itemIndex + ') button').addClass('active');
    }
    
    //click handlers
    function clickFuncs() {
        $(".navigation button").click(function() {
            itemIndex = $(this).attr('data-index');
            scrollToContent();
        });
    
        $('#fade1').click(function() {++itemIndex;
            scrollToContent();
        });
    
        $('#fade').click(function() {--itemIndex;
            scrollToContent();
        });
    }
    
    
    $(document).ready(function() {
    
        //setup click handlers
        clickFuncs();
    
    });
    

    updated HTML

    
    
    

    0 讨论(0)
提交回复
热议问题