Repository: myst729/Waterfall
Branch: master
Commit: 12bebc5b4a5d
Files: 6
Total size: 16.2 KB
Directory structure:
gitextract_g5c2aicf/
├── LICENSE
├── README.md
├── index.html
├── json.php
├── script.js
└── style.css
================================================
FILE CONTENTS
================================================
================================================
FILE: LICENSE
================================================
Copyright (C) 2013 Leo Deng
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
================================================
FILE: README.md
================================================
# Waterfall Layout
Responsive waterfall layout.
================================================
FILE: index.html
================================================
Waterfall Layout
Waterfall Layout
loading
================================================
FILE: json.php
================================================
"demo picture " . $src, "src" => $src, "height" => $height, "width" => 190);
}
// Faking network latency.
sleep(2);
echo json_encode($arr);
?>
================================================
FILE: script.js
================================================
var isGithubDemo = isGithubDemo || false; // This is for GitHub demo only. Remove it in your project
void function(window, document, undefined) {
// ES5 strict mode
"user strict";
var MIN_COLUMN_COUNT = 3; // minimal column count
var COLUMN_WIDTH = 220; // cell width: 190, padding: 14 * 2, border: 1 * 2
var CELL_PADDING = 26; // cell padding: 14 + 10, border: 1 * 2
var GAP_HEIGHT = 15; // vertical gap between cells
var GAP_WIDTH = 15; // horizontal gap between cells
var THRESHOLD = 2000; // determines whether a cell is too far away from viewport (px)
var columnHeights; // array of every column's height
var columnCount; // number of columns
var noticeDelay; // popup notice timer
var resizeDelay; // resize throttle timer
var scrollDelay; // scroll throttle timer
var managing = false; // flag for managing cells state
var loading = false; // flag for loading cells state
var noticeContainer = document.getElementById('notice');
var cellsContainer = document.getElementById('cells');
// Cross-browser compatible event handler.
var addEvent = function(element, type, handler) {
if(element.addEventListener) {
addEvent = function(element, type, handler) {
element.addEventListener(type, handler, false);
};
} else if(element.attachEvent) {
addEvent = function(element, type, handler) {
element.attachEvent('on' + type, handler);
};
} else {
addEvent = function(element, type, handler) {
element['on' + type] = handler;
};
}
addEvent(element, type, handler);
};
// Get the minimal value within an array of numbers.
var getMinVal = function(arr) {
return Math.min.apply(Math, arr);
};
// Get the maximal value within an array of numbers.
var getMaxVal = function(arr) {
return Math.max.apply(Math, arr);
};
// Get index of the minimal value within an array of numbers.
var getMinKey = function(arr) {
var key = 0;
var min = arr[0];
for(var i = 1, len = arr.length; i < len; i++) {
if(arr[i] < min) {
key = i;
min = arr[i];
}
}
return key;
};
// Get index of the maximal value within an array of numbers.
var getMaxKey = function(arr) {
var key = 0;
var max = arr[0];
for(var i = 1, len = arr.length; i < len; i++) {
if(arr[i] > max) {
key = i;
max = arr[i];
}
}
return key;
};
// Pop notice tag after user liked or marked an item.
var updateNotice = function(event) {
clearTimeout(noticeDelay);
var e = event || window.event;
var target = e.target || e.srcElement;
if(target.tagName == 'SPAN') {
var targetTitle = target.parentNode.tagLine;
noticeContainer.innerHTML = (target.className == 'like' ? 'Liked ' : 'Marked ') + '' + targetTitle + '';
noticeContainer.className = 'on';
noticeDelay = setTimeout(function() {
noticeContainer.className = 'off';
}, 2000);
}
};
// Calculate column count from current page width.
var getColumnCount = function() {
return Math.max(MIN_COLUMN_COUNT, Math.floor((document.body.offsetWidth + GAP_WIDTH) / (COLUMN_WIDTH + GAP_WIDTH)));
};
// Reset array of column heights and container width.
var resetHeights = function(count) {
columnHeights = [];
for(var i = 0; i < count; i++) {
columnHeights.push(0);
}
cellsContainer.style.width = (count * (COLUMN_WIDTH + GAP_WIDTH) - GAP_WIDTH) + 'px';
};
// Fetch JSON string via Ajax, parse to HTML and append to the container.
var appendCells = function(num) {
if(loading) {
// Avoid sending too many requests to get new cells.
return;
}
var xhrRequest = new XMLHttpRequest();
var fragment = document.createDocumentFragment();
var cells = [];
var images;
xhrRequest.open('GET', 'json.php?n=' + num, true);
xhrRequest.onreadystatechange = function() {
if(xhrRequest.readyState == 4 && xhrRequest.status == 200) {
images = JSON.parse(xhrRequest.responseText);
for(var j = 0, k = images.length; j < k; j++) {
var cell = document.createElement('div');
cell.className = 'cell pending';
cell.tagLine = images[j].title;
cells.push(cell);
cell.innerHTML = `