2

I replaced the scrolling animation of my one page website with another scrolling animation which changes the URLs when you use the topbar (it was build in foundation)

While the URLs now change when I click an item in the topbar all the other links or clickable elements on my page make it scroll back to the top of the page.

For example when I try to click the next/prev buttons of my slider it scrolls back to the top of the page as if I clicked on Home.

Can someone see whats wrong with the code for the animation?

$(document).ready(function () {
    $('a[href^="#"]').click(function () {
        var target = $(this.hash),
            hash = this.hash;

        if (target.length == 0) {
            target = $('a[name="' + this.hash.substr(1) + '"]');
        }
        if (target.length == 0) {
            target = $('html');
        }

        $('html, body').animate({
            scrollTop: target.offset().top
        }, 500, function () {
            location.hash = hash;
        });

        return false;
    });
});

PS: When I scroll manually the URLs don't change when I go down to the next page. If anyone has a fix for this I'll be happy to here from you! (I tried using history.js but that only seems to work if you have a server, which I don't)

//* EDIT *// I just found out it's not all links that make it scroll to the top of the page, just the buttons of my orbit slider and the menu button when the topbar is collapsed

//EDIT 2// The URL now changes when I scroll to the next page! The only problem I am seeing right now is that the buttons of my orbit slider and the menu button of the collapsed topbar act the same as my home button (makes the page scroll all the way back to the top) for some reason. So the last thing I need to do is get the buttons working again. Making the menu button expand the topbar and making the next and prev buttons of my slider work as normal

Jesse
  • 35
  • 7
  • could you provide a Pen or Fiddle? – Ivan Modric Feb 10 '16 at 10:21
  • I could create a simplified version as fiddle but I don't think it would be of much use since my website was created with foundation and the slider wouldn't work in the fiddle but i'll make one! – Jesse Feb 10 '16 at 10:26
  • https://jsfiddle.net/0shto9df/ – Jesse Feb 10 '16 at 10:40
  • Thank you :) Can you make a list of what is not functioning as it should? Do you only want to change the hash depending on the scroll position? – Ivan Modric Feb 10 '16 at 12:38
  • Yes I want the hash to show the location on the site at all times. The things that aren't working are the next and prev buttons of my orbit slider and the hamburger/menu button when I minimize the screen so the topbar collapses. If theres a code I can use so the hash shows what page the user is on I can get rid of the code that messes up the buttons. – Jesse Feb 10 '16 at 12:43
  • could you provide the relevant `JS` and `HTML`? When you click them and the scroll occurs, does the `hash` in the URL bar change? – Ivan Modric Feb 10 '16 at 15:26
  • I pasted in the JS you provided and it works great. The URL does change but not on the exact moment I click on it but the moment it reaches the top. Could you explain what HTML you need from me? I can't possibly upload all of it because of the framework files needed.. – Jesse Feb 10 '16 at 15:36
  • The **buttons**, the **topbar** and the **orbit slider** should be sufficient. Also the `JS` that binds them. – Ivan Modric Feb 10 '16 at 16:56

1 Answers1

0

If you only want to change the hash depending on the scrollPosition you are half way there.

You'll need to bind some logic to the scroll event. Here is a fork of your Fiddle where the hash is changed on scroll.

When the user scrolls the page we iterate through all .page elements and compare their offset().top against $( document ).scrollTop().

We set the new hash to be the id of the last .page element that has a lower offset().top than $( document ).scrollTop() is.

(I also debounced the function so it doesn't fire constantly when scrolling - you could of course remove that part)

You should however consider that by changing the hash you will jump to the equivalent element. Here is a guide on how to suppress that behaviour.

EDIT:

Here is an updated Fiddle where I implemented the solution from above for suppressing forced scroll on hash change.

Community
  • 1
  • 1
Ivan Modric
  • 609
  • 6
  • 14
  • Thank you for helping! The hash does now change when I scroll over the pages but is indeed jumping tot the top of the div. Also it still causes the buttons to break. I looked at the guide you told me to but there are just so many answers and I have no idea which will work (I still really suck at Java) Could you help out just a bit more? – Jesse Feb 10 '16 at 13:12
  • 1
    Sure. First of all it's not **Java** but **JavaScript** ;)(http://stackoverflow.com/questions/245062/whats-the-difference-between-javascript-and-java) - I updated the fiddle, the jumping should now be fixed. Could you update you question and make a list of the behaviour you are expecting vs the behaviour you are seeing? (also: is there any relevant console output?) – Ivan Modric Feb 10 '16 at 13:25
  • Thank you very much sir! I'll update it in a minute! – Jesse Feb 10 '16 at 14:30