Git Product home page Git Product logo

snapper's Introduction

⚠️ This project is archived and the repository is no longer maintained.

snapper

A CSS Snap-Points based carousel (and lightweight polyfill)

MIT License [c] 2020 Filament Group, Inc

Dependencies

  • jQuery or similar DOM library
  • Intersection Observer Polyfill. Run $ npm install to download a copy to ./node_modules/intersection-observer/intersection-observer.js

Demo

https://filamentgroup.github.io/snapper/demo

Docs

  1. Include dependencies, plus the css and js files in the src dir.
  2. Use the markup pattern below.
<div class="snapper">
	<div class="snapper_pane">
		<div class="snapper_items">
			<div class="snapper_item" id="img-a">
				<img src="a-image.jpg" alt="">
			</div>
			<div class="snapper_item" id="img-b">
				<img src="b-image.jpg" alt="">
			</div>
			<div class="snapper_item" id="img-c">
				<img src="c-image.jpg" alt="">
			</div>
			<div class="snapper_item" id="img-d">
				<img src="d-image.jpg" alt="">
			</div>
		</div>
	</div>
</div>
  1. Trigger an "enhance" event on a parent of the markup to initialize. You might do this on domready, as shown below:
$( function(){
	$( document ).trigger( "enhance" );
});

Adding thumbnails

To add thumbnail or graphic navigation to the carousel, you can append the following markup to the end of the snapper div (substituting your own styles, images, and hrefs to correspond to the IDs of their associated slides):

<div class="snapper_nav">
	<a href="#img-a"><img src="a-thumb.jpg" alt=""></a>
	<a href="#img-b"><img src="b-thumb.jpg" alt=""></a>
	<a href="#img-c"><img src="c-thumb.jpg" alt=""></a>
	<a href="#img-d"><img src="d-thumb.jpg" alt=""></a>
</div>

Adding next/prev navigation

To add next and previous links that persist state, you can add a data-snapper-nextprev attribute to the snapper div.

<div class="snapper" data-snapper-nextprev>
	...
</div>

Showing multiple images at a time

If you want to show more than one snapper item at a time, you can set the widths on .snapper_item elements. You can aslo adjust widths as viewport width changes. For backwards compatibility, we recommend adding a scroll-snap-points-x rule on the .snapper_pane that matches the widths. As shown below.

@media (min-width: 30em){
	.snapper_pane {
		scroll-snap-points-x: repeat(50%);
	}
	.snapper_item {
		width: 50%;
	}
}

Showing partial image reveals

Just as the above specifies, you can use widths to reveal part of the next image to show there's more to scroll.

@media (min-width: 30em){
	.snapper_pane {
		scroll-snap-points-x: repeat(45%);
	}
	.snapper_item {
		width: 45%;
	}
}


### Looping (*experimental)

To make a snapper loop endlessly in either direction, you can add the data-snapper-loop attribute. This feature is experimental in this release.


``` html
<div class="snapper" data-snapper-loop>
	...
</div>

Changes in 4.0x

Version 4.0 breaks a few features and changes the way snapper works. Some notes on that:

  • The HTML is largely the same
  • Fake snapping is no longer supported. If a browser doesn't support CSS scroll snap, it won't happen, but the scrolling will still work.
  • Snap and scroll related events no longer fire. This is because we no longer support polyfilled snapping. The goto, next, prev events remain as they were.
  • Active state is tracked via intersection observer for performance reasons.
  • A "snapper.active" and "snapper.inactive" event is fired whenever snapper items become one or the other.
  • Endless looping is optionally available as an experimental feature. Accessibility impact is TBD on this feature.
  • CSS now uses flexbox, not floats. This means the JS can do less work calculating widths. You can set the widths on snapper item elements directly now instead of worrying about calculated total widths on the parent. If you set widths on the parent, it'll likely conflict with this. Instead, just set desired widths on the items.

Support

CSS Scroll Snap support can be found here: CSS Snap Points on Caniuse.com This plugin is tested to work broadly across modern browsers, and as long as you use thumbnail navigation. Various features may not work as well across older browsers, such as those that do not support snapping, but scroller content will still be accessible.

snapper's People

Contributors

agcolom avatar johnbender avatar scottjehl avatar toddparker avatar zachleat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

snapper's Issues

Vertical snap points ?

Do we have a support vertical snap points, I'm just curious to get an example so I can have a play with it. Currently the demo contains only horizontal slides example.

Hide cropped scrollbar more thoroughly with css props

These will more robustly hide it

.snapper-enhanced .snapper_pane_crop .snapper_pane,
.enhanced .snapper_pane_crop .snapper_pane {
  /* IE and edge */
	-ms-overflow-style: -ms-autohiding-scrollbar;
	/* Firefox */
	scrollbar-width: none;
}
.snapper-enhanced .snapper_pane_crop .snapper_pane::-webkit-scrollbar,
.enhanced .snapper_pane_crop .snapper_pane::-webkit-scrollbar {
	display: none;
}

Note: keying off the pane_crop here since that's there only when hiding is desired.

Decrease snap threshold to 20% - 30%

Currently you only snap to the next slide if the current slide is dragged all tge way to the middle. This results jn unintentional "snap backs." decreasing default sensitivity to 20% - 30% would be more natural. Also making it configurable it isn't already.
Cool project.

move to a non-float based layout so that the pre-JS layout can be closer to enhanced layout

We have pre-enhanced state that shows only the first item in the carousel at full-width. This works well because the items would otherwise stack vertically until widths are set on the items and the parent. It doesn't work well however when items are meant to display at less than 100% width, such as when we show 2 or 4 items at a time. Floats are the reason we hit this issue because they really need a parent and item width to start their layout.

If we move to another model like inlineblock/flexbox/table we'll get a better initial layout and cleaner rendering process.

How can we invoke the `next();` method ?

Hi,
I'd like to be able to trigger the sliding of the next slide every 5 seconds...
Right now, I did it using a setInterval + something like

$('.snapper_nextprev_next').trigger('click');

but this is causing a side effect while the click event is bubbling...

is there another way to call the next() method ?

Thank you for your help

Inaccurate snapper_items width?

Greetings from Cloud Four, Filament Group friends! 👋

We've been using this plugin on a recent site we helped design and build. It's so much nicer than the alternatives we've tried on devices. Thank you for making it!

Recently I encountered an issue where sometimes, the snapper_items width will just be plain wrong. As near as I can tell because it isn't consistent, if images haven't loaded yet, it's possible for snapper_items to have a width of 0%, or some number lower than what it should be (100% for two images, for example).

Screenshots or it didn't happen:

screen shot 2017-09-15 at 10 42 52 am

screen shot 2017-09-15 at 11 45 07 am

Any idea what's going on? I thought it had to do with another plugin, and then maybe with some CSS customizations we'd made for vertical alignment, but removing those things didn't alleviate the problem. I double-checked that we are firing the plugin on ready (as per the docs).

I realize without a self-contained and consistently reproducible test case it's difficult to bug-fix, but a point in the right direction would be greatly appreciated. Thanks in advance! 🙏

describe feature detect in readme

it would be great if the readme could mention how to feature detect the css scroll snap feature and prevent the polyfill from beeing loaded when the browser supports it natively

Active slide is forgotten across resize

Error that shows sometimes is:

TypeError: $items[startSlide] is undefined

When that happens, any attempt to set the active slide after resize will end up scrolling to 0 and losing the placement. We should verify if this happens in Snapper's demos too.

Steps to reproduce

  1. Go to https://master-origin-snapper.fgview.com/demo/ with iOS mode in Chrome
  2. Navigate to slide 4
  3. Rotate device

Wrong slide seen. Slides 1 and 2 are ok.

drop dom lib dependency

aside from initializing via the enhance event, it should be simple to make this jquery/shoestring independent

iphone lock-in

iPhone iOS 9

worth testing:

  1. scroll down to carousel
  2. swipe to advance carousel
  3. try to scroll back up, with touch starting on carousel

Remove automatic init

Snapper currently includes the automatic init at the bottom of the main source. It would be nice to split that out so that one can include the JS from the npm module and initialize on demand.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.