0

I have an accordion element, and I need to have different panes expand on hashchange. The code I made, expands it but it doesn't scroll the the targeted div, and page never ends loading.

function hashChange() {
    if (window.location.hash === '#senior-backend') {
        $('#senior-backend, #backend-developer, #senior-frontend, #frontend, #dev-ops').hide(50);
        $('#senior-backend').show(50);
        $('#job-posts').removeClass().addClass('beige-bg');
        $('#job-posts-top').removeClass().addClass('beige-spikes');
    } 
}

window.onhashchange = hashChange;

Could you please point out what am I doing wrong.

Thanks

Bojana Šekeljić
  • 1,056
  • 1
  • 12
  • 30

1 Answers1

1

You need to scroll the site using animate once you detect a change in the hash, for example:

var dest = $('#yourSelector').position();
var dtop = dest.top;

$('html, body').animate({
    scrollTop: dtop
});

Living demo: http://jsfiddle.net/LZbK8/

Alvaro
  • 40,778
  • 30
  • 164
  • 336
  • thanks, just out of curiosity, why isn't it scrolling on default? and also page still isn't fully loaded, the progress bar stops at some point, any idea what is preventing it? – Bojana Šekeljić Aug 27 '13 at 14:50
  • It would if you were using anchor links such as ``. [Here](http://jsfiddle.net/LZbK8/1/) you have the same example without the jQuery I added before: http://jsfiddle.net/LZbK8/1/ The scrolling by default works only for anchor links, not for jQuery selectors such as `id` or `class`. – Alvaro Aug 27 '13 at 14:57
  • The page never loading is probably a separate problem. Do you have any external content (i.e. anything from another domain) loading on the page? And is that site up and working? You should be able to see what's going on from your browser's developer tools. – Olly Hodgson Aug 27 '13 at 14:59
  • @OllyHodgson it started to happen when I added haschange and it only happens when I go to those addresses, so I wouldn't say it has something to do with other scripts. Site is currently on my local server. – Bojana Šekeljić Aug 27 '13 at 15:04