简体   繁体   中英

jQuery easing library - Uncaught Error: Syntax error, unrecognized expression

I am trying to use http://gsgd.co.uk/sandbox/jquery/easing library in my single page app to navigate to different section of my page with smooth scrolling effect.

When I use the following html markup:

<ul class="navbar-nav ml-auto">
    <li class="nav-item">
        <a class="nav-link page-scroll" href="#home">HOME <span class="sr-only">(current)</span></a>
    </li>
    <li class="nav-item">
        <a class="nav-link page-scroll" href="#page1">PAGE 1</a>
    </li>
    <li class="nav-item">
        <a class="nav-link page-scroll" href="#page2">PAGE 2</a>
    </li>
    <li class="nav-item">
        <a class="nav-link page-scroll" href="#page3">PAGE 3</a>
    </li>
</ul>

along with this javascript:

$(function() {
    $(document).on('click', 'a.page-scroll', function(event) {
        var $anchor = $(this);
        $('html, body').stop().animate({
            scrollTop: $($anchor.attr('href')).offset().top
        }, 600, 'easeInOutExpo');
        event.preventDefault();
    });
});

This works exactly as expected. However, my nav is shared with other page (eg contact us), which is not on the single page, so I updated my nav like this:

<ul class="navbar-nav ml-auto">
    <li class="nav-item">
        <a class="nav-link page-scroll" href="/#home">HOME <span class="sr-only">(current)</span></a>
    </li>
    <li class="nav-item">
        <a class="nav-link page-scroll" href="/#page1">PAGE 1</a>
    </li>
    <li class="nav-item">
        <a class="nav-link page-scroll" href="/#page2">PAGE 2</a>
    </li>
    <li class="nav-item">
        <a class="nav-link page-scroll" href="/#page3">PAGE 3</a>
    </li>
    <li class="nav-item">
        <a class="nav-link page-scroll" href="/contact-us">Contact Us</a>
    </li>
</ul>

When I did this; I started to get this error:

Uncaught Error: Syntax error, unrecognized expression: http://example.com/#page1

I tried updating my javascript like this;

$(function() {
    $(document).on('click', 'a.page-scroll', function(event) {
        $('html, body').stop().animate({
            scrollTop: $($(location).attr('hash')).offset().top
        }, 600, 'easeInOutExpo');
        event.preventDefault();
    });
});

This does not help. However, what's strange is; when I run this in the chrome console:

$($(location).attr('hash')).offset().top

This works and I am seeing float value.

Any idea what might be wrong?

I found a way to fix this like so:

$(function() {
    $(document).on('click', 'a.page-scroll', function(event) {
        var $anchor = $(this);
        var targetUrl = $anchor.attr('href');
        var hash = targetUrl.split('#')[1];
        var targetDiv = $('#' + hash);
        if (targetDiv && targetDiv.length > 0) {
            event.preventDefault();
            $('html, body').stop().animate({
                scrollTop: targetDiv.offset().top
            }, 600, 'easeInOutExpo');
        }
    });
});

this might not be the right approach, but it's working for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM