Hello stackoverflow contributors! I have this script that gets the starting position of my jCarousel through the browser URL’s hash. Something like text.html#2.
What am trying to achieve is make jCarousel scroll to the given position on page load. However my code below seems to work only if I bind it to clicks – it does not respond to on page load requests.
Initialize jCarousel
jQuery('#body_list').jcarousel({
scroll: 1,
initCallback: bodylist_initCallback
});
Callback function
function bodylist_initCallback(carousel) {
$(window).load(function () {
if(window.location.hash) {
var hash = window.location.hash.slice(1);
carousel.scroll(jQuery.jcarousel.intval(hash));
}
});
});
Alternative scroll call
The following lines works except in Safari
if(window.location.hash) {
var hash = window.location.hash.slice(1);
jQuery('#body_list').jcarousel('scroll', hash);
}
You could set the
startoption when you initialize the jCarousel:Update
If you want to see the scrolling animation when you load the page, try setting a timeout in the
initCallbackoption for jCarousel:Seems to work in FF/Chrome. I’m on Ubuntu so I can’t try it on IE or Safari.