-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement load more script (#51)
* feat: implement load more script * chore: update all enquiry templates
- Loading branch information
1 parent
97ef88a
commit 08def4d
Showing
5 changed files
with
181 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
;(() => { | ||
const getButton = (section) => section.querySelector('button') | ||
const getArea = (section) => section.querySelector('[load-more-area]') | ||
const getCurrentPage = (area) => parseInt(area.getAttribute('data-page')) | ||
const getPerPage = (area) => parseInt(area.getAttribute('data-per-page')) | ||
const getTotal = (area) => parseInt(area.getAttribute('data-total')) | ||
|
||
const init = (section) => { | ||
const area = getArea(section) | ||
const perPage = getPerPage(area) | ||
const total = area.children.length | ||
|
||
area.setAttribute('data-total', total) | ||
area.setAttribute('data-page', 1) | ||
Array.from(area.children) | ||
.slice(perPage) | ||
.forEach((child) => child.classList.add('d-none')) | ||
} | ||
|
||
const handleLoadMore = (section) => { | ||
const area = getArea(section) | ||
const currentPage = getCurrentPage(area) | ||
const perPage = getPerPage(area) | ||
const total = getTotal(area) | ||
|
||
const start = currentPage * perPage | ||
const end = Math.min(start + perPage, total) | ||
|
||
Array.from(area.children) | ||
.slice(start, end) | ||
.forEach((child) => child.classList.remove('d-none')) | ||
|
||
area.setAttribute('data-page', currentPage + 1) | ||
|
||
if (end >= total) { | ||
getButton(section).classList.add('d-none') | ||
} | ||
} | ||
|
||
document.querySelectorAll('[load-more-section]').forEach((section) => { | ||
init(section) | ||
getButton(section).addEventListener('click', () => handleLoadMore(section)) | ||
}) | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import 'bootstrap' | ||
import './darkmode.js' | ||
import './tiles-showcase.js' | ||
import './load-more.js' | ||
import './tiles-showcase.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters