forked from malsup/cycle2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.cycle2.hash.js
52 lines (43 loc) · 1.3 KB
/
jquery.cycle2.hash.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*! hash plugin for Cycle2; version: 20130905 */
(function($) {
"use strict";
$(document).on( 'cycle-pre-initialize', function( e, opts ) {
onHashChange( opts, true );
opts._onHashChange = function() {
onHashChange( opts, false );
};
$( window ).on( 'hashchange', opts._onHashChange);
});
$(document).on( 'cycle-update-view', function( e, opts, slideOpts ) {
if ( slideOpts.hash && ( '#' + slideOpts.hash ) != window.location.hash ) {
opts._hashFence = true;
window.location.hash = slideOpts.hash;
}
});
$(document).on( 'cycle-destroyed', function( e, opts) {
if ( opts._onHashChange ) {
$( window ).off( 'hashchange', opts._onHashChange );
}
});
function onHashChange( opts, setStartingSlide ) {
var hash;
if ( opts._hashFence ) {
opts._hashFence = false;
return;
}
hash = window.location.hash.substring(1);
opts.slides.each(function(i) {
if ( $(this).data( 'cycle-hash' ) == hash ) {
if ( setStartingSlide === true ) {
opts.startingSlide = i;
}
else {
var fwd = opts.currSlide < i;
opts.nextSlide = i;
opts.API.prepareTx( true, fwd );
}
return false;
}
});
}
})(jQuery);