Jquery Smooth Scroll To DIV - Using ID value from Link

前端 未结 3 1304
心在旅途
心在旅途 2020-12-08 08:05

So i\'m having some issues with my JQuery which is suppose to scroll to particular divs.

HTML

3条回答
  •  难免孤独
    2020-12-08 08:23

    You can do this:

    $('.searchbychar').click(function () {
        var divID = '#' + this.id;
        $('html, body').animate({
            scrollTop: $(divID).offset().top
        }, 2000);
    });
    

    F.Y.I.

    • You need to prefix a class name with a . (dot) like in your first line of code.
    • $( 'searchbychar' ).click(function() {
    • Also, your code $('.searchbychar').attr('id') will return a string ID not a jQuery object. Hence, you can not apply .offset() method to it.

提交回复
热议问题