jquery Scroll to class name

后端 未结 4 1361
慢半拍i
慢半拍i 2020-12-16 13:33

I have the Html code like below,

相关标签:
4条回答
  • 2020-12-16 13:42

    these two codes worked for me like a charm this first will take to scroll to the top of the page but if you want to scroll to some specific div use the second one with your div id.

    $('body, html, #containerDiv').scrollTop(0);
    document.getElementById('yourDivID').scrollIntoView();
    

    If you want to scroll to by a class name use the below code

    enter code here
    var $container = $("html,body");
    var $scrollTo = $('.main-content');
    
    $container.animate({scrollTop: $scrollTo.offset().top - $container.offset().top + 
    $container.scrollTop(), scrollLeft: 0},300);
    
    0 讨论(0)
  • 2020-12-16 13:56

    You are not targeting a DOM object, you are targeting a string.

    scrollTo = $(this).find('.saveIcon').attr('data-unique', 456); -> this is wrong
    

    So, while you are trying to target an element, you are actually setting the 'data-unique' to the '.saveIcon' element.

    Try this:

    scrollTo = $('.saveIcon');
    

    Working code:

    var $container = $("html,body");
    var $scrollTo = $('.saveIcon');
    
    $container.animate({scrollTop: $scrollTo.offset().top - $container.offset().top + $container.scrollTop(), scrollLeft: 0},300); 
    
    0 讨论(0)
  • 2020-12-16 13:58

    have you looked at scroll to view function?

    https://developer.mozilla.org/en-US/docs/Web/API/element.scrollIntoView

    element.scrollIntoView(true);
    
    0 讨论(0)
  • 2020-12-16 13:59

    I am using following plain js, please try if you can use it in you codes:

    $('a.smooth-scroll[href*=#]:not([href=#])').click(function () {
        if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
            var target = $(this.hash);
            target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
            if (target.length) {
                $('html,body').animate({
                    scrollTop: target.offset().top
                }, 1000);
                return false;
            }
        }
    });
    
    0 讨论(0)
提交回复
热议问题