forked from germanysbestkeptsecret/Wookmark-jQuery
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.html
More file actions
197 lines (162 loc) · 5.59 KB
/
server.html
File metadata and controls
197 lines (162 loc) · 5.59 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<!doctype html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>jQuery Wookmark Plug-in API Example</title>
<meta name="description" content="An very basic example of how to use the Wookmark jQuery plug-in.">
<meta name="author" content="Christoph Ono, Sebastian Helzle">
<meta name="viewport" content="width=device-width,initial-scale=1">
<!-- CSS Reset -->
<link rel="stylesheet" href="../css/reset.css">
<!-- Global CSS for the page and tiles -->
<link rel="stylesheet" href="../css/main.css">
<!-- Specific CSS for the example -->
<style>
/**
* Grid items
*/
#tiles li {
opacity: 0;
-webkit-transition: all 0.8s ease-out;
-moz-transition: all 0.8s ease-out;
-o-transition: all 0.8s ease-out;
transition: all 0.8s ease-out;
}
</style>
</head>
<body>
<div id="container">
<header>
<h1>jQuery Wookmark Plug-in API Example with custom server</h1>
<p>Scroll down to see more content loaded via a custom server script.</p>
</header>
<div id="main" role="main">
<ul id="tiles">
<!-- These is where we place content loaded from the Wookmark API -->
</ul>
<div id="loader">
<div id="loaderCircle"></div>
</div>
</div>
<footer>
</footer>
</div>
<!-- include jQuery -->
<script src="../libs/jquery.min.js"></script>
<!-- Include the imagesLoaded plug-in -->
<script src="../libs/jquery.imagesloaded.js"></script>
<!-- Include the plug-in -->
<script src="../jquery.wookmark.js"></script>
<!-- Once the page is loaded, initalize the plug-in. -->
<script type="text/javascript">
(function ($) {
var $tiles = $('#tiles'),
$handler = $('li', $tiles),
page = 1,
isLoading = false,
apiURL = 'server.php',
lastRequestTimestamp = 0,
fadeInDelay = 2000,
$window = $(window),
$document = $(document);
// Prepare layout options.
var options = {
autoResize: true, // This will auto-update the layout when the browser window is resized.
container: $('#tiles'), // Optional, used for some extra CSS styling
offset: 2, // Optional, the distance between grid items
itemWidth: 210 // Optional, the width of a grid item
};
/**
* When scrolled all the way to the bottom, add more tiles.
*/
function onScroll(event) {
// Only check when we're not still waiting for data.
if (!isLoading) {
// Check if we're within 100 pixels of the bottom edge of the broser window.
var closeToBottom = ($window.scrollTop() + $window.height() > $document.height() - 100);
if (closeToBottom) {
// Only allow requests every second
var currentTime = new Date().getTime();
if (lastRequestTimestamp < currentTime - 1000) {
lastRequestTimestamp = currentTime;
loadData();
}
}
}
};
/**
* Refreshes the layout.
*/
function applyLayout($newImages) {
options.container.imagesLoaded(function() {
// Destroy the old handler
if ($handler.wookmarkInstance) {
$handler.wookmarkInstance.clear();
}
// Create a new layout handler.
$tiles.append($newImages);
$handler = $('li', $tiles);
$handler.wookmark(options);
// Set opacity for each new image at a random time
$newImages.each(function() {
var $self = $(this);
window.setTimeout(function() {
$self.css('opacity', 1);
}, Math.random() * fadeInDelay);
});
});
};
/**
* Loads data from the API.
*/
function loadData() {
isLoading = true;
$('#loaderCircle').show();
$.ajax({
url: apiURL,
dataType: 'json', // Set to jsonp if you use a server on a different domain and change it's setting accordingly
data: {page: page}, // Page parameter to make sure we load new data
success: onLoadData
});
};
/**
* Receives data from the API, creates HTML for images and updates the layout
*/
function onLoadData(response) {
isLoading = false;
$('#loaderCircle').hide();
// Increment page index for future calls.
page++;
// Create HTML for the images.
var html = '',
data = response.data,
i = 0, length = data.length, image, opacity,
$newImages;
for (; i < length; i++) {
image = data[i];
html += '<li>';
html += '<a target="_blank" href="'+image.image+'" title="'+image.title+'">';
// Image tag (preview in Wookmark are 200px wide, so we calculate the height based on that).
html += '<img src="'+image.preview+'" width="200" height="'+Math.round(image.height/image.width*200)+'">';
html += '</a>';
// Image title.
html += '<p>'+image.title+'</p>';
html += '</li>';
}
$newImages = $(html);
// Disable requests if we reached the end
if (response.message == 'No more pictures') {
$document.off('scroll', onScroll);
}
// Apply layout.
applyLayout($newImages);
};
// Capture scroll event.
$document.on('scroll', onScroll);
// Load first data from the API.
loadData();
})(jQuery);
</script>
</body>
</html>