Home
css
javascript
HTML
View All Categories
Category 1
Category 2
Category 3
Category 4
Article 1
Article 2
Article 3
Article 4
Article 5
Article 6
Article 7
Article 8
Article 9
Article 10
Article 11
Article 12
Article 13
Article 14
Article 15
Article 16
Article 17
Article 18
Article 19
Article 20
Article 21
Article 22
Article 23
Article 24
Article 25
Article 26
Article 27
Article 28
Article 29
Article 30
Article 31
Article 32
Article 33
Show More
CSS
/** * used by js as flags for the css breakpoints/mediaqueries */ body:before { display: none; content: "mobile"; } ul { list-style-type: none; padding: 0px; margin: 0px; width: 100%; } ul li { display: block; margin-top: 10px; padding: 5%; min-height: 30px; background-color: #E6E6E6; border-top: 1px solid #BDBDBD; } @media (min-width:767px) { body:before { content: "desktop"; } ul li { display: inline-block; margin: 1%; width: 20%; border: 1px solid #BDBDBD; } } .jsHidden { display: none; }
JavaScript
(function(){ const articleNodeList = document.querySelectorAll("ul li"), $article = Array.from(articleNodeList), $filter = document.getElementById("filter"), $more = document.getElementById("more"); var articlesDefault = 6, articlesDefaultDesktop = 9, eventsInitialized = false, articlesLimit = 0; var module = { breakpoint: null, init: function() { module.initEvents(); module.getBreakpoint(); }, initEvents: function() { if(!eventsInitialized) { window.addEventListener("resize", resizeThrottler, false); var resizeTimeout; function resizeThrottler() { // wait for resizeHandler execution to complete if ( !resizeTimeout ) { resizeTimeout = setTimeout(function next() { resizeTimeout = null; module.getBreakpoint(); }, 66); } } } eventsInitialized = true; }, /** * Sets the breakpoint value based on :before content of the body element. The content is controlled by mediaqueries. * Possible values are mobile | desktop */ getBreakpoint: function() { module.breakpoint = window.getComputedStyle(document.querySelector('body'), ':before').getPropertyValue('content').replace(/\"|\'/g, ''); console.log("BREAKPOINT: "+ module.breakpoint);$article module.displayArticles(); }, displayArticles: function() { $more.classList.add('jsHidden'); val = $filter.options[$filter.selectedIndex].value; if(module.breakpoint == 'desktop') { articlesLimit = articlesDefaultDesktop; } else { articlesLimit = articlesDefault; } var count = 0; $article.forEach(el => { if (el !== null) { el.classList.add('jsHidden'); if (val.length > 0) { catArr = el.dataset.category; if (catArr.indexOf(val) != -1) { count = count + 1; // break if past limit if (articlesLimit > 0 && (count > articlesLimit)) { module.moreArticles(count, articlesLimit); return false; } else { el.classList.remove('jsHidden'); } } } else { // show all articles // break if past limit count = count + 1; if (articlesLimit > 0 && (count > articlesLimit)) { module.moreArticles(count, articlesLimit); return false; } else { el.classList.remove('jsHidden'); } } } }); }, moreArticles: function(count, limit) { if (count && limit) { if (count > limit) { $more.classList.remove('jsHidden'); } } else { // display 9 more articles articlesDefault = articlesDefault + 9; articlesDefaultDesktop = articlesDefaultDesktop + 9; module.displayArticles(); } } }; module.init(); $filter.addEventListener("change", function() { module.displayArticles(); }); $more.addEventListener("click", function(event) { event.preventDefault(); module.moreArticles(); }); })();