简体   繁体   中英

jQuery smooth scrolling on a different webpage

I have managed to get the smooth scrolling working on a single page using the following code.

Note the HTML link is stored in a header.php and used across multiple pages below is a code snippet:

HTML Script:

<a class="item" href="index.php#contact">

<a name="contact"></a>

JS Script:

$('a[href="index.php#contact"]').click(function (e) { // user clicks on html link

    e.preventDefault();  // prevent the default behaviour that occurs in the browser

    var targetOffset = $('a[name="contact"]').offset().top; // define a variable to point at a particular section & offset from the top of browser

    $('body').animate( // create animation

        { scrollTop: targetOffset }, 1000); // scrollTop property = target variable

    });

Problem:

When I go to a different webpage and click the contact link it does not link back to the index.php#contact and scroll down to the contact anchor point.

Any assistance or advice is much appreciated I'm sure its a simple tweak in the code somewhere.

检查您的href应该是:index.php /#contact

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