Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <main>
	<h1>💡 Pagination Prototype</h1>
	<p>This is a simple demo showing how you can write a reusable <code>paginate()</code> function.</p>

	<ul class="pagination" data-pagination="10">
		<li><a href="#" data-paginate="first">First</a></li>
		<li><a href="#" data-paginate="prev">Previous</a></li>
		<li><a href="#" data-paginate="1" class="is-active">1</a></li>
		<li><a href="#" data-paginate="2">2</a></li>
		<li><a href="#" data-paginate="3">3</a></li>
		<li><a href="#" data-paginate="4">4</a></li>
		<li><a href="#" data-paginate="5">5</a></li>
		<li><a href="#" data-paginate="6">6</a></li>
		<li><a href="#" data-paginate="7">7</a></li>
		<li><a href="#" data-paginate="8">8</a></li>
		<li><a href="#" data-paginate="9">9</a></li>
		<li><a href="#" data-paginate="10">10</a></li>
		<li><a href="#" data-paginate="next">Next</a></li>
		<li><a href="#" data-paginate="last">Last</a></li>
	</ul>

	<div id="result">
		Page #1
	</div>
</main>

              
            
!

CSS

              
                /* ------------------------------ */
/* Base                           */
/* ------------------------------ */

html {
	box-sizing: border-box;
}

*,
::after,
::before {
	box-sizing: inherit;
}

body {
	margin: 1rem auto 3rem;
	line-height: 1.4;
	tab-size: 4;
}

main {
	display: block; /* Fix IE rendering main as inline */
	margin: 0 auto;
	max-width: 42rem;
	padding-left: 1rem;
	padding-right: 1rem;
}

/* ------------------------------ */
/* Pagination                     */
/* ------------------------------ */

.pagination {
	display: flex;
	margin: 0 0 1rem;
	padding: 0;
	list-style: none;
}

.pagination a {
	display: block;
	min-width: 1rem;
	padding: 0.25rem 0.5rem;
	border: 1px solid black;
	text-align: center;
}

.pagination a.is-active {
	background: lightblue;
}

              
            
!

JS

              
                /**
 * Pagination container.
 * @type {Element}
 */
const pagination = document.querySelector('[data-pagination]');

/**
 * Pagination buttons.
 * @type {Element[]}
 */
const buttons = Array.from(pagination.querySelectorAll('[data-paginate]'));

/**
 * Container to add results to.
 * @type {Element}
 */
const result = document.getElementById('result');

/**
 * Keep track of the current page. Start on page 1.
 * @type {Number}
 */
let currentPage = 1;

/**
 * Keep track of the total number of pages. Start with the
 * value defined in the `data-pagination` attribute.
 * @type {Number}
 */
const totalPages = parseInt(pagination.getAttribute('data-pagination'), 10) || 1;

/**
 * Get pagination history.
 * @param  {Event}  event
 * @return {void}
 */
function paginate(event) {
	// Prevent default click handling so links don't cause the page to jump
	event.preventDefault();
	
	// Which page should we request?
	let page = event.currentTarget.dataset.paginate;
	
	// Get relative page numbers
	if (page === 'first') {
		page = 1;
	} else if (page === 'prev') {
		page = currentPage - 1;
	} else if (page === 'next') {
		page = currentPage + 1;
	} else if (page === 'last') {
		page = totalPages;
	}
	
	// To be consistent, make sure current page is always a Number
	page = parseInt(page, 10);
	
	// Set page boundaries:
	// 1. You can't go before the first page
	page = Math.max(1, page);
	// 2. You can't go after the last page
	page = Math.min(totalPages, page);

	// TODO: REQUEST PAGE DATA
	result.innerHTML = `Page #${page}`;
	
	// Update current page
	currentPage = page;
	
	// Toggle button active classes
	buttons.forEach((button) => {
		const buttonPage = parseInt(button.dataset.paginate, 10);
		
		// Activate the button for the current page
		if (buttonPage === currentPage) {
			button.classList.add('is-active');
		// Deactivate all the other buttons
		} else {
			button.classList.remove('is-active')
		}
	});
}

// Add click handlers on page load
buttons.forEach(button => button.addEventListener('click', paginate));

              
            
!
999px

Console