main v0.2.0
HerrHase 2 years ago
parent 80a0c015f0
commit 24efcce500

@ -35,35 +35,81 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); } function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
/**
* Tiny Masonry
*
* @author Björn Hase
* @license http://opensource.org/licenses/MIT The MIT License
* @link https://gitea.node001.net/tiny-components/masonry
*
*/
var Masonry = /*#__PURE__*/function () { var Masonry = /*#__PURE__*/function () {
/**
*
*
*/
function Masonry() { function Masonry() {
var _this = this; var _this = this;
_classCallCheck(this, Masonry); _classCallCheck(this, Masonry);
// getting elements
this.elements = document.querySelectorAll('.tiny-masonry__item-inner'); this.elements = document.querySelectorAll('.tiny-masonry__item-inner');
this.container = document.querySelector('.tiny-masonry'); this.container = document.querySelector('.tiny-masonry');
// getting gap for calculate from grid
this.gap = parseInt(getComputedStyle(this.container, 'gap').getPropertyValue('grid-gap').split(' ')[0]);
this.calculate(); this.calculate();
window.addEventListener('resize', (0,throttle_debounce__WEBPACK_IMPORTED_MODULE_0__.throttle)(300, function () { window.addEventListener('resize', (0,throttle_debounce__WEBPACK_IMPORTED_MODULE_0__.throttle)(300, function () {
_this.calculate(); _this.calculate();
}), false); }), false);
window.addEventListener('scroll', (0,throttle_debounce__WEBPACK_IMPORTED_MODULE_0__.throttle)(300, function () {
_this.calculate(true);
}), false);
} }
/**
*
*
*/
_createClass(Masonry, [{ _createClass(Masonry, [{
key: "calculate", key: "calculate",
value: function calculate() { value: function calculate() {
var onlyVisible = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
// getting number of columns
if (!onlyVisible) {
this.columns = Math.ceil(this.container.offsetWidth / this.elements[0].parentElement.offsetWidth) - 1; this.columns = Math.ceil(this.container.offsetWidth / this.elements[0].parentElement.offsetWidth) - 1;
for (var i = 0; i < this.elements.length; i++) { if (this.columns === 0) {
this.elements[i].parentElement.style.marginTop = '0px'; this.columns = 1;
}
} }
for (var _i = 0; _i < this.elements.length; _i++) { for (var i = 0; i < this.elements.length; i++) {
if (this.elements[_i + this.columns]) { var marginTop = 0;
var offset = 0;
// if style was already // getting row
if (this.elements[_i] && this.elements[_i].parentElement.style.marginTop) { var row = Math.round(i / this.columns) + 1;
offset += parseInt(this.elements[_i].parentElement.style.marginTop);
// reset margin-top for the first columns
if (i < this.columns) {
this.elements[i].parentElement.dataset.offsetMarginTop = 0;
} }
// setting // check for parent element and getting marginTop
this.elements[_i + this.columns].parentElement.style.marginTop = this.elements[_i].offsetHeight + parseInt(offset) + 'px'; if (this.elements[i - this.columns] && !onlyVisible) {
if (this.elements[i - this.columns].parentElement.dataset.offsetMarginTop) {
marginTop += parseInt(this.elements[i - this.columns].parentElement.dataset.offsetMarginTop);
}
marginTop += this.elements[i - this.columns].offsetHeight;
} else {
marginTop = this.elements[i].parentElement.dataset.offsetMarginTop;
}
if (!onlyVisible) {
this.elements[i].parentElement.dataset.offsetMarginTop = marginTop;
}
if (window.pageYOffset <= marginTop + this.elements[i].offsetHeight + this.gap * row && window.pageYOffset + window.innerHeight + this.gap * row >= marginTop) {
this.elements[i].parentElement.style.marginTop = marginTop + 'px';
this.elements[i].parentElement.style.visibility = 'visible';
} else {
this.elements[i].parentElement.style.visibility = 'hidden';
} }
} }
} }

@ -47,6 +47,18 @@
<div class="tiny-masonry__item"><div class="tiny-masonry__item-inner"><img src="/demo.jpg" loading="lazy" /><span>10</span></div></div> <div class="tiny-masonry__item"><div class="tiny-masonry__item-inner"><img src="/demo.jpg" loading="lazy" /><span>10</span></div></div>
<div class="tiny-masonry__item"><div class="tiny-masonry__item-inner"><img src="/demo.jpg" loading="lazy" /><span>11</span></div></div> <div class="tiny-masonry__item"><div class="tiny-masonry__item-inner"><img src="/demo.jpg" loading="lazy" /><span>11</span></div></div>
<div class="tiny-masonry__item"><div class="tiny-masonry__item-inner"><img src="/demo.jpg" loading="lazy" /><span>12</span></div></div> <div class="tiny-masonry__item"><div class="tiny-masonry__item-inner"><img src="/demo.jpg" loading="lazy" /><span>12</span></div></div>
<div class="tiny-masonry__item"><div class="tiny-masonry__item-inner"><img src="/demo.jpg" loading="lazy" /><span>13</span></div></div>
<div class="tiny-masonry__item"><div class="tiny-masonry__item-inner"><img src="/demo.jpg" loading="lazy" /><span>14</span></div></div>
<div class="tiny-masonry__item"><div class="tiny-masonry__item-inner"><img src="/demo.jpg" loading="lazy" /><span>15</span></div></div>
<div class="tiny-masonry__item"><div class="tiny-masonry__item-inner"><img src="/demo.jpg" loading="lazy" /><span>16</span></div></div>
<div class="tiny-masonry__item"><div class="tiny-masonry__item-inner"><img src="/demo.jpg" loading="lazy" /><span>17</span></div></div>
<div class="tiny-masonry__item"><div class="tiny-masonry__item-inner"><img src="/demo.jpg" loading="lazy" /><span>18</span></div></div>
<div class="tiny-masonry__item"><div class="tiny-masonry__item-inner"><img src="/demo.jpg" loading="lazy" /><span>19</span></div></div>
<div class="tiny-masonry__item"><div class="tiny-masonry__item-inner"><img src="/demo.jpg" loading="lazy" /><span>20</span></div></div>
<div class="tiny-masonry__item"><div class="tiny-masonry__item-inner"><img src="/demo.jpg" loading="lazy" /><span>21</span></div></div>
<div class="tiny-masonry__item"><div class="tiny-masonry__item-inner"><img src="/demo.jpg" loading="lazy" /><span>22</span></div></div>
<div class="tiny-masonry__item"><div class="tiny-masonry__item-inner"><img src="/demo.jpg" loading="lazy" /><span>23</span></div></div>
<div class="tiny-masonry__item"><div class="tiny-masonry__item-inner"><img src="/demo.jpg" loading="lazy" /><span>24</span></div></div>
</div> </div>
<script defer src="/example.js"></script> <script defer src="/example.js"></script>

@ -1,6 +1,6 @@
{ {
"name": "@tiny-components/masonry", "name": "@tiny-components/masonry",
"version": "0.1.0", "version": "0.2.0",
"description": "Masonry for Desktop and Mobile", "description": "Masonry for Desktop and Mobile",
"repository": { "repository": {
"type": "git", "type": "git",

@ -21,41 +21,67 @@ class Masonry {
this.elements = document.querySelectorAll('.tiny-masonry__item-inner') this.elements = document.querySelectorAll('.tiny-masonry__item-inner')
this.container = document.querySelector('.tiny-masonry') this.container = document.querySelector('.tiny-masonry')
// getting gap for calculate from grid
this.gap = parseInt(getComputedStyle(this.container, 'gap').getPropertyValue('grid-gap').split(' ')[0])
this.calculate() this.calculate()
window.addEventListener('resize', throttle(300, () => { window.addEventListener('resize', throttle(300, () => {
this.calculate() this.calculate()
}), false) }), false)
window.addEventListener('scroll', throttle(300, () => {
this.calculate(true)
}), false)
} }
/** /**
* *
* *
*/ */
calculate() { calculate(onlyVisible = false) {
// getting number of columns // getting number of columns
if (!onlyVisible) {
this.columns = Math.ceil(this.container.offsetWidth / this.elements[0].parentElement.offsetWidth) - 1 this.columns = Math.ceil(this.container.offsetWidth / this.elements[0].parentElement.offsetWidth) - 1
// reset margin-top if (this.columns === 0) {
for (let i = 0; i < this.elements.length; i++) { this.columns = 1
this.elements[i].parentElement.style.marginTop = '0px' }
} }
for (let i = 0; i < this.elements.length; i++) { for (let i = 0; i < this.elements.length; i++) {
// adding each parent element in column let marginTop = 0
if (this.elements[i + this.columns]) {
// getting row
let row = Math.round(i / this.columns) + 1
let offset = 0 // reset margin-top for the first columns
if (i < this.columns) {
this.elements[i].parentElement.dataset.offsetMarginTop = 0
}
// check for parent element and getting marginTop
if (this.elements[i - this.columns] && !onlyVisible) {
if (this.elements[i - this.columns].parentElement.dataset.offsetMarginTop) {
marginTop += parseInt(this.elements[i - this.columns].parentElement.dataset.offsetMarginTop)
}
marginTop += this.elements[i - this.columns].offsetHeight
} else {
marginTop = this.elements[i].parentElement.dataset.offsetMarginTop
}
// if style was already setup adding to offset if (!onlyVisible) {
if (this.elements[i] && this.elements[i].parentElement.style.marginTop) { this.elements[i].parentElement.dataset.offsetMarginTop = marginTop
offset += parseInt(this.elements[i].parentElement.style.marginTop)
} }
// setting margin top if (window.pageYOffset <= (marginTop + this.elements[i].offsetHeight + (this.gap * row)) && (window.pageYOffset + window.innerHeight + (this.gap * row)) >= marginTop) {
this.elements[i + this.columns].parentElement.style.marginTop = this.elements[i].offsetHeight + parseInt(offset) + 'px' this.elements[i].parentElement.style.marginTop = marginTop + 'px'
this.elements[i].parentElement.style.visibility = 'visible'
} else {
this.elements[i].parentElement.style.visibility = 'hidden'
} }
} }
} }

Loading…
Cancel
Save