mattdbryce
mattdbryce

Reputation: 19

smooth scrolling between pages

I am trying to add some smooth scrolling to my portfolio site, so that when you click on a project button it will load the new page and smooth scroll down to the project -http://www.mattdbryce.co.uk/. I've found this code - http://css-tricks.com/snippets/jquery/smooth-scrolling/ - however that only allows smooth scrolling for links on the same page. When i used this code, no matter which link you click on in the navigation, it would just smooth scroll down on the existing page.

I considered having one long page with lots of anchor tags but wouldnt this take forever to load?

Any ideas guys?

Many thanks,

Matt

My HTML:

<!DOCTYPE HTML>

<head>
    <!--web fonts-->
    <link href='http://fonts.googleapis.com/css?family=Ubuntu+Mono:400italic,700italic' rel='stylesheet' type='text/css'>
    <!--end web fonts-->

    <meta charset="UTF-8">

    <title>Matt Bryce | East London Graphic Design, Web Design, Logo Design and Brand Identity</title>

    <!--css files-->
    <link rel="stylesheet" href="css/reset.css">
    <link rel="stylesheet" href="css/text.css">
    <link rel="stylesheet" href="css/960_24_col.css">
    <link rel="stylesheet" href="css/style.css">
    <link rel="stylesheet" href="css/font-awesome.min.css">
    <!--End css files-->

    <!--JS-->
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="js/smooth-scroll.js"></script>
    <!--END JS-->

    <!--favicon-->
    <link rel="shortcut icon" href="favicon.png">
    <!--End favicon-->


</head>

<body>
<div class="shadow">
    <div class="container_24">
        <header>
            <h1 class="clearfix"><a href="index.html">Matt Bryce</a></h1>
            <div id="title"><h2>Matthew Bryce Portfolio</h2></div>
            <nav>
                <ul>

                    <li><a href="mailto:[email protected]"><img src="img/contact.png" alt="Contact"/></a></li>


                </ul>
            </nav>

        </header>
        <div class="portfolio-nav">

            <div class="grid_4">
                <h3><a href="good-taste.html#project-top"><img src="img/good-taste-button-1.png" alt="Good Taste Delicatessen - Branding + Website"></a>Good Taste Delicatessen</h1>
                    <p>Branding + Website</p>
            </div>

            <div class="grid_4">
                <h3><a href="jims-cafe.html#project-top"><img src="img/jims-cafe-button-1.png" alt="Jim's Cafe - Logo Design"></a>Jim's Cafe</h1>
                    <p>Logo Design</p>
            </div>

            <div class="grid_4">
                <h3><a href="coffee-revolution.html#project-top"><img src="img/the-coffee-revolution-button-1.png" alt="The Coffee Revolution - Events Poster"></a>The Coffee Revolution</h1>
                    <p>Events Poster</p>
            </div>

            <div class="grid_4">
                <h3><a href="wilton-way.html#project-top"><img src="img/wilton-way-cafe-button-1.png" alt="Wilton Way Cafe - Logo Design"></a>The Wilton Way Cafe</h1>
                    <p>Logo Design</p>
            </div>

            <div class="grid_4">
                <h3><a href="halligan-poster.html#project-top"><img src="img/halligan-poster-button-1.png" alt="Halligan - Gig Poster"></a>Halligan</h1>
                    <p>Gig Poster</p>
            </div>

            <div class="grid_4">
                <h3><a href="mj-wedding.html#project-top"><img src="img/martyn-and-jo-button-1.png" alt="Martyn + Jo - Wedding Invitiations"></a>Martyn + Jo</h1>
                    <p>Wedding Invitation</p>
            </div>

            <div class="grid_4">
                <h3><a href="dog-and-wardrobe.html#project-top"><img src="img/the-dog-and-wardrobe-button-1.png" alt="The Dog + Wardrobe - Campaign Material"></a>The Dog + Wardrobe</h1>
                    <p>Campaign Material</p>
            </div>

            <div class="grid_4">
                <h3><a href="early-years.html#project-top"><img src="img/the-early-years-button-1.png" alt=""></a>The Early Years</h1>
                    <p>Gig Poster</p>
            </div>

            <div class="grid_4">
                <h3><a href="halligan-album.html#project-top"><img src="img/halligan-album-button-1.png" alt="Halligan - Album Artwork"></a>Halligan</h1>
                    <p>Album Artwork</p>
            </div>

            <div class="grid_4">
                <h3><a href="part-chimp.html#project-top"><img src="img/part-chimp-button-1.png" alt="Part Chimp - Gig Poster"></a>Part Chimp</h1>
                    <p>Gig Poster</p>
            </div>

        </div> 
        <footer><p>Copyright Matthew Bryce Design</p></footer>

    </div> <!--end container-->
</div>
    <!--JQuery-->
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

    <!--Include plugin-->

    <!--hook it up-->

</body>

Upvotes: 1

Views: 2420

Answers (3)

Vincent De Jong
Vincent De Jong

Reputation: 31

In case you have a clean URL system and anchor links between pages look like this:

<a href="index#someId">someId</a>

You might want to take a look at the following code:

$(document).ready(function() {  
  $('a[href*=\\#]').on('click', function(e){
      e.preventDefault();
      var fullTargetLink = this.href;
      var targetLink = fullTargetLink.substr(0, fullTargetLink.indexOf('#'));
      var fullCurrentLink = window.location.href;
      if (fullCurrentLink.indexOf('#') > -1) {
          var currentLink = fullCurrentLink.substr(0, fullCurrentLink.indexOf('#'));
      }else{
          var currentLink = fullCurrentLink;    
      }
      if (targetLink !== currentLink){
          window.location.href = fullTargetLink;
      }else{
          $('html, body').animate({
              scrollTop : $(this.hash).offset().top
          }, 500);
      }
  })
});

Upvotes: 0

Hive7
Hive7

Reputation: 3685

What I think you need to do is when the new page loads find the distance it is from the top of the page then use this jquery:

$('body').animate({
    'scroll-top': distance from top
});

Upvotes: 0

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167220

Okay, you can use the same logic given in the CSS Tricks website to smooth scroll. While loading the document, you can check if there's a # exists in the page and if it is the case, smooth scroll the page to the element's top.

$(window).on("load", function () {
    var urlHash = window.location.href.split("#")[1];
    if (urlHash.length > 0)
        $('html,body').animate({
            scrollTop: $('#' + urlHash).offset().top
        }, 4000);
});

Or,

$(document).ready(function(){
    var urlHash = window.location.href.split("#")[1];
    if (urlHash.length > 0)
        $('html,body').animate({
            scrollTop: $('#' + urlHash).offset().top
        }, 4000);
});

Upvotes: 3

Related Questions