/
jquery.contentcycle.js
140 lines (123 loc) · 5.25 KB
/
jquery.contentcycle.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* Name: Content Cycle jQuery Plugin
* Author: Christopher Grabowski
*
* Description: Cycles sections of html content like a
* slide show.
*
* If you find any errors or have suggested changes,
* please post a comment on the github project:
* http://github.com/christophergrabowski/Content-Cycle-jQuery-Plugin
*/
/*global console, jQuery*/
/*jslint browser: true*/
(function ($, window, undefined) {
"use strict";
var methods = {
init: function (options) {
var $this = this,
children = this.children('div'),
data = this.data('cycle');
if (!data) {
this.data('contentcycle', {
count : 0,
handle : null,
settings : {
inSpeed : 1500,
outSpeed : 1500,
duration : 5000
}
});
}
if (typeof options === 'object') {
$.extend(this.data('contentcycle').settings, options);
}
$(window).load(function (event) {
// add the cycle bar
for (var i = 0; i < $('.content-cycle-bar').length; i ++) {
$('.content-cycle-bar').append('<a class="content-cycle-bullet">•</a>');
}
$('.content-cycle-bar').each(function(index, element) {
$($(element).children('.content-cycle-bullet')).removeClass('content-cycle-bullet-active');
$($(element).children('.content-cycle-bullet')[index]).addClass('content-cycle-bullet-active');
});
$('.content-cycle-bar .content-cycle-bullet').each(function(index, element) {
$($('.content-cycle-bullet')[index]).click(function (event) {
methods.change($this, (index % $('.project-page').length));
});
});
// set the initial height for the project pages container
// then remove all pages from the layout except for the first
methods.containHighestContentPage($this);
methods.start($this);
children.css('display', 'none');
children.css('visibility', 'visible');
$(children[0]).css('display', 'block');
}).on('resize', function (event) {
methods.containHighestContentPage($this);
});
return this;
},
start: function ($this) {
var data = $this.data('contentcycle');
// use set interval to cycle through the pages
data.handle = setInterval(function () {
methods.forward($this);
}, data.settings.duration);
return $this;
},
stop: function ($this) {
clearInterval($this.data('contentcycle').handle);
return $this;
},
back: function ($this) {
var data = $this.data('contentcycle'),
children = $this.children('div');
$(children[data.count]).fadeOut(data.settings.OutSpeed);
data.count = (data.count <= 0) ? children.size() - 1 : data.count -= 1;
$(children[data.count]).fadeIn(data.settings.inSpeed, function (event) {
$(children[data.count]).trigger('contentpagechange', [$(children[data.count])]);
});
return $this;
},
forward: function ($this) {
var data = $this.data('contentcycle'),
children = $this.children('div');
$(children[data.count]).fadeOut(data.settings.outSpeed);
data.count = (data.count >= children.size() - 1) ? 0 : data.count += 1;
$(children[data.count]).fadeIn(data.settings.inSpeed, function (event) {
$(children[data.count]).trigger('contentpagechange', [$(children[data.count])]);
});
return $this;
},
change: function($this, index) {
methods.stop($this);
var data = $this.data('contentcycle'),
children = $this.children('div');
$(children).fadeOut(data.settings.outSpeed);
$($this.children('div')[index]).fadeIn(data.settings.inSpeed, function (event) {
$(children[index]).trigger('contentpagechange', [$(children[index])]);
});
return $this;
},
// Causes container to be at least as high as its highest child.
containHighestContentPage: function ($this) {
var gstHeight = 0, eleHeight = 0;
$this.children().each(function (index, element) {
eleHeight = $(element).height();
gstHeight = (gstHeight > eleHeight) ? gstHeight : eleHeight;
});
return $this.height(gstHeight);
}
};
$.fn.contentCycle = function (arg) {
if (methods[arg]) {
return methods[arg].apply(this, arguments);
} else if (typeof arg === 'object' || !arg) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + arg + ' does not exist on jQuery.contentCycle');
}
return this;
};
}(jQuery, window));