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 = `

${images[j].title}

Like! Mark! ` fragment.appendChild(cell); } cellsContainer.appendChild(fragment); loading = false; adjustCells(cells); } }; loading = true; xhrRequest.send(null); }; // Fake mode, only for GitHub demo. Delete this function in your project. var appendCellsDemo = function(num) { if(loading) { // Avoid sending too many requests to get new cells. return; } var fragment = document.createDocumentFragment(); var cells = []; var images = [0, 286, 143, 270, 143, 190, 285, 152, 275, 285, 285, 128, 281, 242, 339, 236, 157, 286, 259, 267, 137, 253, 127, 190, 190, 225, 269, 264, 272, 126, 265, 287, 269, 125, 285, 190, 314, 141, 119, 274, 274, 285, 126, 279, 143, 266, 279, 600, 276, 285, 182, 143, 287, 126, 190, 285, 143, 241, 166, 240, 190]; for(var j = 0; j < num; j++) { var key = Math.floor(Math.random() * 60) + 1; var cell = document.createElement('div'); cell.className = 'cell pending'; cell.tagLine = 'demo picture ' + key; cells.push(cell); cell.innerHTML = `

demo picture ${key}

Like! Mark! ` fragment.appendChild(cell); } // Faking network latency. setTimeout(function() { loading = false; cellsContainer.appendChild(fragment); adjustCells(cells); }, 2000); }; // Position the newly appended cells and update array of column heights. var adjustCells = function(cells, reflow) { var columnIndex; var columnHeight; for(var j = 0, k = cells.length; j < k; j++) { // Place the cell to column with the minimal height. columnIndex = getMinKey(columnHeights); columnHeight = columnHeights[columnIndex]; cells[j].style.height = (cells[j].offsetHeight - CELL_PADDING) + 'px'; cells[j].style.left = columnIndex * (COLUMN_WIDTH + GAP_WIDTH) + 'px'; cells[j].style.top = columnHeight + 'px'; columnHeights[columnIndex] = columnHeight + GAP_HEIGHT + cells[j].offsetHeight; if(!reflow) { cells[j].className = 'cell ready'; } } cellsContainer.style.height = getMaxVal(columnHeights) + 'px'; manageCells(); }; // Calculate new column data if it's necessary after resize. var reflowCells = function() { // Calculate new column count after resize. columnCount = getColumnCount(); if(columnHeights.length != columnCount) { // Reset array of column heights and container width. resetHeights(columnCount); adjustCells(cellsContainer.children, true); } else { manageCells(); } }; // Toggle old cells' contents from the DOM depending on their offset from the viewport, save memory. // Load and append new cells if there's space in viewport for a cell. var manageCells = function() { // Lock managing state to avoid another async call. See {Function} delayedScroll. managing = true; var cells = cellsContainer.children; var viewportTop = (document.body.scrollTop || document.documentElement.scrollTop) - cellsContainer.offsetTop; var viewportBottom = (window.innerHeight || document.documentElement.clientHeight) + viewportTop; // Remove cells' contents if they are too far away from the viewport. Get them back if they are near. // TODO: remove the cells from DOM should be better :< for(var i = 0, l = cells.length; i < l; i++) { if((cells[i].offsetTop - viewportBottom > THRESHOLD) || (viewportTop - cells[i].offsetTop - cells[i].offsetHeight > THRESHOLD)) { if(cells[i].className === 'cell ready') { cells[i].fragment = cells[i].innerHTML; cells[i].innerHTML = ''; cells[i].className = 'cell shadow'; } } else { if(cells[i].className === 'cell shadow') { cells[i].innerHTML = cells[i].fragment; cells[i].className = 'cell ready'; } } } // If there's space in viewport for a cell, request new cells. if(viewportBottom > getMinVal(columnHeights)) { // Remove the if/else statement in your project, just call the appendCells function. if(isGithubDemo) { appendCellsDemo(columnCount); } else { appendCells(columnCount); } } // Unlock managing state. managing = false; }; // Add 500ms throttle to window scroll. var delayedScroll = function() { clearTimeout(scrollDelay); if(!managing) { // Avoid managing cells for unnecessity. scrollDelay = setTimeout(manageCells, 500); } }; // Add 500ms throttle to window resize. var delayedResize = function() { clearTimeout(resizeDelay); resizeDelay = setTimeout(reflowCells, 500); }; // Initialize the layout. var init = function() { // Add other event listeners. addEvent(cellsContainer, 'click', updateNotice); addEvent(window, 'resize', delayedResize); addEvent(window, 'scroll', delayedScroll); // Initialize array of column heights and container width. columnCount = getColumnCount(); resetHeights(columnCount); // Load cells for the first time. manageCells(); }; // Ready to go! addEvent(window, 'load', init); }(window, document); ================================================ FILE: style.css ================================================ /* global */ body { background: #eee; border: 0 none; margin: 0; padding: 0; font-family: Arial, sans-serif; font-size: 12px; line-height: 1; overflow-y: scroll; } h1 { margin: 0; height: 100px; line-height: 100px; font-size: 48px; text-align: center; } #cells { margin: 0 auto; position: relative; } #loader { margin: 0 auto; text-align: center; } #loader span { background: url("data:image/gif;base64,R0lGODlhEAAQAPIAAP///wAAAMLCwoKCggAAAAAAAAAAAAAAACH/C05FVFNDQVBFMi4wAwEAAAAh/hpDcmVhdGVkIHdpdGggYWpheGxvYWQuaW5mbwAh+QQJCgAAACwAAAAAEAAQAAADGwi6MjRiSenIm9hqPOvljAOBZGmeaKqubOu6CQAh+QQJCgAAACwAAAAAEAAQAAADHAi63A5ikCEek2TalftWmPZFU/WdaKqubOu+bwIAIfkECQoAAAAsAAAAABAAEAAAAxwIutz+UIlBhoiKkorB/p3GYVN1dWiqrmzrvmkCACH5BAkKAAAALAAAAAAQABAAAAMbCLrc/jDKycQgQ8xL8OzgBg6ThWlUqq5s604JACH5BAkKAAAALAAAAAAQABAAAAMbCLrc/jDKSautYpAhpibbBI7eOEzZ1l1s6yoJACH5BAkKAAAALAAAAAAQABAAAAMaCLrc/jDKSau9OOspBhnC5BHfRJ7iOXAe2CQAIfkECQoAAAAsAAAAABAAEAAAAxoIutz+MMpJ6xSDDDEz0dMnduJwZZulrmzbJAAh+QQJCgAAACwAAAAAEAAQAAADGwi63P4wRjHIEBJUYjP/2dZJlIVlaKqubOuyCQAh+QQJCgAAACwAAAAAEAAQAAADHAi63A5ikCEek2TalftWmPZFU/WdaKqubOu+bwIAOwAAAAAAAAAAAA==") left center no-repeat; display: inline-block; height: 36px; line-height: 36px; padding: 0 0 0 18px; font-weight: bold; text-transform: uppercase; } /* notice */ #notice { background: #fefec1; border: 1px solid #d9ccb4; padding: 0 10px; height: 28px; color: #333; font-size: 12px; line-height: 28px; position: fixed; left: 0px; z-index: 9600; -webkit-transition: top 500ms ease-in-out; -moz-transition: top 500ms ease-in-out; -o-transition: top 500ms ease-in-out; transition: top 500ms ease-in-out; } #notice.off { top: -36px; } #notice.on { top: 0px; } /* cell */ .cell { background: #fff; border: 1px solid #ddd; padding: 14px 14px 10px; width: 190px; position: absolute; } .cell:hover { box-shadow: 0 0 10px #aaa; } .pending { opacity: 0; -webkit-transform: translateY(50px); -moz-transform: translateY(50px); -o-transform: translateY(50px); transform: translateY(50px); } .ready { -webkit-transition: opacity 1s ease-in-out, box-shadow 300ms ease-in-out, left 700ms ease-in-out, top 700ms ease-in-out, -webkit-transform 700ms ease-in-out; -moz-transition: opacity 1s ease-in-out, box-shadow 300ms ease-in-out, left 700ms ease-in-out, top 700ms ease-in-out, -moz-transform 700ms ease-in-out; -o-transition: opacity 1s ease-in-out, box-shadow 300ms ease-in-out, left 700ms ease-in-out, top 700ms ease-in-out, -o-transform 700ms ease-in-out; transition: opacity 1s ease-in-out, box-shadow 300ms ease-in-out, left 700ms ease-in-out, top 700ms ease-in-out, transform 700ms ease-in-out; } .shadow { visibility: hidden; } .cell p { margin: 0 0 10px; } .cell img { display: block; vertical-align: bottom; border: 0 none; } .cell h2 { font-size: 12px; margin: 0; height: 14px; line-height: 14px; } .cell a { color: #666; text-decoration: none; } .cell a:hover { color: #f3c; } /* user actions */ .cell span { background-color: #f5f5f5; background-position: 5px center; background-repeat: no-repeat; border: 1px solid #999; border-radius: 2px; padding: 0 7px 0 26px; height: 24px; font-size: 14px; line-height: 24px; cursor: pointer; position: absolute; top: 20px; z-index: 9000; visibility: hidden; } .ready:hover span { visibility: visible; } .cell span:hover { background-color: #fff; border: 1px solid #39f; } .cell span.like { background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABF0lEQVQ4y6XSPytGcRjG8TPIn0gRpZTy73kTshhsH7LIYFAGg01ZFCUlr0JGUYqBUkbvQanH4JlIBsWijuX+6XSch6cM1/K9r+vq/O5zZ3meZ/9Rlud5hqRZ7OEEO5gJPo0tnOMIq/hRcIi8QsdN+FWxYD3gB7axiF28BH/FPuaxgnrwg1RwFWCp+D6M4QG1Ep8K/20CbwF6yktCfwVrC/9nAu8BulrZPAbD/5zAdYCFFgvWwn+WwEaAOob+CNfwFP7l4m9Mi7zDcJPwJBrhOy3fwQBuYniPkVJ4Ao8xP0d71SX2FfbxgNGYjxfCF+iousSkXlyGuYG5UrgzeZsVZOiOzyye7gW6ir7fCpI2I7hZNf8u+I++AJOxCNmiwfyzAAAAAElFTkSuQmCC"); left: 20px; } .cell span.mark { background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABDElEQVQ4y5WTzwoBURjFp+QVhA0loigvMlupKSkpSlaUN1DWQkQTFv7kRWy8iI2dP2Wcq6M+4840Fr/mu+f7zpl754/hOI6hwzTNMXDI2GvOyxyl8UlUHfknYE7THhxYzwIFYDBPwwNkQJa10nLaADQsMAEn3blRT4V+4qz1DhDb/XAFRxATAXFqN9fs3BCmBiiCsM+bCXOmQc/XA1uDkJdZhITAVu4gAS4URgECRpw9q6N9xII4V9vH3BFzha/XCKHFxjDA3Zs/3wHEAZtdn4AeZ/q6gB2bJa6TvONQ1dTKnNnoApZsVoAN7uK8ql6AKte2LqDu+kgUK+LWa9p/gSFLXlNCTysTd1aTnhej9TkzczH9aQAAAABJRU5ErkJggg=="); right: 20px; }