-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.simpleSlide.js
More file actions
170 lines (150 loc) · 5.2 KB
/
Copy pathjquery.simpleSlide.js
File metadata and controls
170 lines (150 loc) · 5.2 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*!
* simpleSlide - jQuery Plugin
* version: 0.8.1
* @requires jQuery v1.6 or later
*
*
* Copyright 2012 Alexander Eder
*
*/
/*!
* jQuery lightweight plugin boilerplate
* Original author: @ajpiano
* Further changes, comments: @addyosmani
* Licensed under the MIT license
*/
;
(function ($, window, document, undefined) {
var pluginName = 'simpleSlide',
defaults = {
playing: true,
shuffle: true,
speed: 9000,
};
// The actual plugin constructor
function Plugin(element, options) {
this.element = element;
this.options = $.extend({}, defaults, options);
this._defaults = defaults;
this._name = pluginName;
this.init();
}
Plugin.prototype.init = function () {
this.playing = this.options.playing;
this.shuffle = this.options.shuffle;
this.speed = this.options.speed;
this.slides = this.options.slides;
this.currentPicture = 0;
var $this = this;
if (typeof (this.slides) == 'undefined') {
var json = this.options.json;
if (typeof (json) != 'undefined') {
$.ajax({
type: 'GET',
url: json,
dataType: 'json',
success: function (data) {
$this.slides = data;
},
data: {},
async: false
});
} else {
console.error("define slides or json for simpleSlide");
}
}
if (this.shuffle) {
this.slides = this.arrayShuffle(this.slides);
}
this.buildSlideshow();
$(this.element).find(".simplePause").click(function () {
$this.togglePlay();
});
$(this.element).find(".simpleNext").click(function () {
$this.nextPicture();
});
$(this.element).find(".simplePrev").click(function () {
$this.prevPicture();
});
$(this.element).mouseenter(function () {
$(this).children(".simpleTitle").slideDown();
$(this).children(".simpleControlls").fadeIn();
}).mouseleave(function () {
$(this).children(".simpleTitle").slideUp();
$(this).children(".simpleControlls").fadeOut();
});
this.slideLoop = window.setTimeout(function () {
$this.nextPicture();
}, this.speed);
};
Plugin.prototype.togglePlay = function () {
if (this.playing) {
$(this.element).find(".simplePause span span").text(">");
this.playing = false;
window.clearTimeout(this.slideLoop);
} else {
$(this.element).find(".simplePause span span").text("||");
this.playing = true;
var $this = this;
this.slideLoop = window.setTimeout(function () {
$this.nextPicture();
}, this.speed);
}
};
Plugin.prototype.prevPicture = function () {
var prevPicture = this.currentPicture - 1;
if (prevPicture < 0) {
prevPicture = this.slides.length - 1;
}
$(this.element).children('img').eq(this.currentPicture).fadeOut(2000);
$(this.element).children('img').eq(prevPicture).fadeIn(2000);
$(this.element).children('.simpleTitle span span').text(this.slides[prevPicture].title);
this.currentPicture = prevPicture;
};
Plugin.prototype.nextPicture = function () {
var nextPicture = this.currentPicture + 1;
if (nextPicture > this.slides.length - 1) {
nextPicture = 0;
}
$(this.element).children('img').eq(this.currentPicture).fadeOut(2000);
var $this = this;
$(this.element).children('img').eq(nextPicture).fadeIn(2000, function () {
$this.slideLoop = setTimeout(function () {
$this.nextPicture();
}, $this.speed);
});
$(this.element).children('.simpleTitle span span').text(this.slides[nextPicture].title);
this.currentPicture = nextPicture;
};
Plugin.prototype.buildSlideshow = function () {
$(this.element).empty();
$(this.element).addClass("simpleSlide");
$(this.element).append("<div class=\"simpleTitle\"><span><span>" + this.slides[0].title + "</span></span> </div>");
for (var i = 0; i < this.slides.length; i++) {
var image = $('<img />', this.slides[i])
.hide();
$(this.element).append(image);
}
$(this.element).children("img").first().show();
$(this.element).append("<div class=\"simpleControlls\"><div class=\"button simplePrev\" ><span><span><<</span></span></div><div class=\"button simplePause\" ><span><span>" + ((this.playing) ? "||" : ">") + "</span></span></div><div class=\"button simpleNext\" ><span><span>>></span></span></div></div>");
};
Plugin.prototype.arrayShuffle = function (array) {
var tmp,
rand;
for (var i = 0; i < array.length; i++) {
rand = Math.floor(Math.random() * array.length);
tmp = array[i];
array[i] = array[rand];
array[rand] = tmp;
}
return array;
};
$.fn[pluginName] = function (options) {
return this.each(function () {
if (!$.data(this, 'plugin_' + pluginName)) {
$.data(this, 'plugin_' + pluginName,
new Plugin(this, options));
}
});
};
})(jQuery, window, document);