Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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>🐭 Snap Slider “Draggable” Example</h1>
	
	<p class="warning">⚠️ <strong>IMPORTANT</strong>: This demo uses the latest <a href="https://github.com/tannerhodges/snap-slider/pull/20" target="_blank" rel="noopener">Snap Slider v3 alpha release</a>.</p>
	
	<ul class="example scrollfix" data-snap-slider="example">
		<li class="example__slide"><img src="https://tannerhodges.github.io/snap-slider/images/batman-sandwich.jpg" alt="" width="296" height="206"></li>
		<li class="example__slide"><img src="https://tannerhodges.github.io/snap-slider/images/batman-artichokes.jpg" alt="" width="296" height="206"></li>
		<li class="example__slide"><img src="https://tannerhodges.github.io/snap-slider/images/batman-dressing-room.jpg" alt="" width="296" height="206"></li>
		<li class="example__slide"><img src="https://tannerhodges.github.io/snap-slider/images/batman-bomb.jpg" alt="" width="296" height="206"></li>
	</ul>
	
	<ul class="example-nav" data-snap-slider-nav="example">
		<li><button type="button">Prev</button></li>
		<li><button type="button">1</button></li>
		<li><button type="button">2</button></li>
		<li><button type="button">3</button></li>
		<li><button type="button">4</button></li>
		<li><button type="button">Next</button></li>
	</ul>
	
	<h2>References</h2>
	<ul>
		<li><a href="https://github.com/tannerhodges/snap-slider/issues/33" target="_blank" rel="noopener">GitHub Issue: Draggable on desktop</a></li>
		<li><a href="https://htmldom.dev/drag-to-scroll/" target="_blank" rel="noopener">HTML DOM: Drag to scroll</a></li>
	</ul>
</main>

              
            
!

CSS

              
                /* ------------------------------ */
/* 🐭 Draggable Slider            */
/* ------------------------------ */

/**
 * Change slider cursor to "grab" by default.
 */
.example {
	cursor: grab;
}

/**
 * Disable click interactions on images
 * to avoid native image dragging.
 */
.example img {
	pointer-events: none;
}

/**
 * When user begins dragging:
 * 1. Disable scroll snap
 * 2. Change cursor to "grabbing"
 * 3. Disable user select
 */
.example.is-dragging {
	scroll-snap-type: none;
	cursor: grabbing;
	user-select: none;
}



/* ------------------------------ */
/* Basic Example                  */
/* ------------------------------ */

html {
	box-sizing: border-box;
}

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

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

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

h1,
h2 {
	font-family: 'Bangers', sans-serif;
	text-align: center;
}

h2 {
	margin-block-start: 3rem;
}

button {
	cursor: pointer;
}

pre {
	max-width: max-content;
	margin: auto;
	padding: 1rem;
	border: 1px solid #ccc;
	overflow: auto;
	-webkit-overflow-scroll: touch;
}

.warning {
	padding: 0.6rem 1rem;
	background: yellow;
}

/**
 * Example slider.
 */
.example {
	display: flex;
	position: relative;
	margin: 0 auto 1rem;
	padding: 0;
	overflow: auto;
	scroll-snap-type: x mandatory;
	list-style: none;
}

.example > li {
	display: block;
	flex: 0 0 auto;
	width: var(--width, 50%);
  padding: 4px;
  border: 2px solid black;
	scroll-snap-align: start;
}

.example img {
  display: block;
  width: 100%;
  height: auto;
}

/**
 * Example slider nav.
 */
.example-nav {
	display: flex;
	gap: 4px;
	justify-content: center;
	margin: 0;
	padding: 0;
	list-style: none;
	text-align: center;
	user-select: none;
}

.example-nav button {
	margin: 0;
  padding: 0.5rem 1rem;
  border: 2px solid black;
  border-radius: 6px;
  background: #fff;
  font-family: 'Bangers', sans-serif;
  font-size: 1.25rem;
  /* Prevent accidental text selection */
  user-select: none;
  /* Prevent double tap zooming in on iOS */
  touch-action: manipulation;
}

.example-nav button.is-visible {
	color: white;
	background: blue;
}

.example-nav button.is-disabled {
	color: gray;
	background: lightgray;
	border-color: gray;
}

/**
 * Fix overflow scroll ignoring margin/padding.
 * @see https://chenhuijing.com/blog/flexbox-and-padding/
 * @see https://itnext.io/horizontal-overflow-with-flexbox-css-64f530495303
 */
.scrollfix::before,
.scrollfix::after {
  content: '';
  display: inline-block;
  flex: 0 0 auto;
  width: 1px;
  margin-left: -1px;
}

/**
 * Hide scrollbars in all major browsers.
 * @see https://stackoverflow.com/a/38994837
 */
/* Chrome/Safari */
.hide-scrollbars::-webkit-scrollbar { width: 0; }
/* Firefox */
.hide-scrollbars { scrollbar-width: none; }

              
            
!

JS

              
                const ele = document.querySelector('[data-snap-slider="example"]');

let pos = { top: 0, left: 0, x: 0, y: 0 };

const mouseDownHandler = function (e) {
	ele.classList.add('is-dragging');

	pos = {
		left: ele.scrollLeft,
		top: ele.scrollTop,
		// Get the current mouse position
		x: e.clientX,
		y: e.clientY
	};

	document.addEventListener("mousemove", mouseMoveHandler);
	document.addEventListener("mouseup", mouseUpHandler);
};

const mouseMoveHandler = function (e) {
	// How far the mouse has been moved
	const dx = e.clientX - pos.x;
	const dy = e.clientY - pos.y;

	// Scroll the element
	ele.scrollTop = pos.top - dy;
	ele.scrollLeft = pos.left - dx;
};

const mouseUpHandler = function () {
	ele.classList.remove('is-dragging');

	document.removeEventListener("mousemove", mouseMoveHandler);
	document.removeEventListener("mouseup", mouseUpHandler);
};

// Attach the handler
ele.addEventListener("mousedown", mouseDownHandler);

              
            
!
999px

Console