I am looking to create a page that allows use of the back and forwards buttons for content within the page. I have accomplished the basic functionality using jQuery Address as featured at
The issue I am having is how to get the callback function to work in coordination with jQuery scrollTo plugin to provide an animated back and forwards scrolling experience.
//Navigation between slides
$('.nav-button').click(function(e) {
e.preventDefault();
$.scrollTo($(this).attr('href'), 'slow', {easing:'easeInOutExpo'});
});
//Storage and activation of back and forwards attributes
$('a').click(function() {
$.address.value($(this).attr('href'));
});
//Callback to scroll on back or forwards
//NEED HELP HERE
$.address.change(function(event) {
$.scrollTo(event.value(), 'slow', {easing:'easeInOutExpo'});
});
As Im sure you can see the problem really isnt that difficult, but since Im still learning it is out of the scope of my knowledge. Any help is much appreciated.
All of the content is contained within one page divided into different "pages" by the use of various div elements with a common class
UPDATE:
I created a fiddle to display the issue more clearly.
jsfiddle.net/d7uZ7/6
The bottom section of jquery is what is to be used to trigger the scrolling after a back or forward page event
SOLUTION:
So after a long time battling with this a solution (of sorts) has been found. Also, I should not that without the chat with JacobMcLock none of this would have been possible.
Anyhow, the last section of my original post should be changed to one of the two following code blocks
//Method 1
$.address.change(function (event) {
var value = event.value,
i = value.indexOf('#'),
$element = (i === -1) ? $("body") : $(value.substr(i, value.length-1));
$.scrollTo($element, 'slow', {easing:'easeInOutExpo'});
});
//Method 2
$(window).on('hashchange', function(event) {
event.preventDefault();
$.address.change(function (event) {
var value = event.value,
i = value.indexOf('#'),
$element = (i === -1) ? $("body") : $(value.substr(i, value.length-1));
$.scrollTo($element, 'slow', {easing:'easeInOutExpo'});
});
});
Use the first block for the best speed, but with lack of compatibility. This will function as desired in Chrome and Safari and will be glitchy in others. This is because they handle hashchange
and popstate
events correctly while others do not. Oddly some older browsers will also work properly.
The second method, while much slower (there is a notable delay on animation start), will work in almost all browsers semi-well. Yep, just semi-well, sometimes it still fails. So the reality is that due to browsers not using these tags correctly youre likely stuck with boring old non-animated back and forward in page transitions. So sad. Unless you feel like doing a monumental amount of coding.
I don't think that event.value()
is what you want. See the examples on this page for what event.value
actually is: http://www.asual.com/jquery/address/samples/api/. It is a string from the URL, and not an href or element ID. You'll have to do something extra to actually get the element that you want to scroll to from that value, which depends on how you are injecting the value into the URL.