Skip to content

Commit

Permalink
πŸ›  use change event, not select; πŸ‘· v1.0.1
Browse files Browse the repository at this point in the history
🐞 fixes bug changing hash on resize
πŸ“ revise README & description
  • Loading branch information
desandro committed Mar 19, 2018
1 parent 5918987 commit 19c5c52
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 27 deletions.
60 changes: 44 additions & 16 deletions README.md
@@ -1,32 +1,34 @@
# Flickity hash

_Enable hash navigation for Flickity_
_Select Flickity slides with links_

## Features

Connect hash links (i.e. `href="#cell2"`) to select Flickity slides.
+ Connect hash links to select Flickity slides. `<a href="#cell2">View cell 2</a>`
+ Use URLs for slides: `https://example.com/#cell2`
+ Change page hash to match selected slide.

``` html
<a href="#cell2">View cell 2</a>
```
## Install

Use URLs for slides: `https://example.com/#cell2`
Add `hash.js` to your scripts.

Change page hash to match selected slide.
### Download

## Install
+ [hash.js](https://unpkg.com/flickity-hash@1/hash.js)

### Manual
### CDN

+ Add `hash.js` to your scripts
``` html
<script src="https://unpkg.com/flickity-hash@1/hash.js"></script>
```

### Package managers

npm: `npm install flickity-hash`

Bower: `bower install flickity-hash`

## Initialize
## Usage

Enable hash behavior by setting `hash: true` in Flickity options.

Expand All @@ -39,7 +41,7 @@ var $carousel = $('.carousel').flickity({

``` js
// vanilla JS
var flkty = $('.carousel').flickity({
var flkty = new Flickity( '.carousel', {
hash: true,
});
```
Expand All @@ -51,18 +53,44 @@ var flkty = $('.carousel').flickity({
</div>
```

## HTML
### HTML

Add `id` attributes to cell elements.

``` html
<div class="carousel">
<div class="carousel-cell" id="cell1">...</div>
<div class="carousel-cell" id="cell2">...</div>
<div class="carousel-cell" id="cell3">...</div>
<div class="carousel-cell" id="carousel-cell1">...</div>
<div class="carousel-cell" id="carousel-cell2">...</div>
<div class="carousel-cell" id="carousel-cell3">...</div>
</div>
```

Hash links will select slides on click.

``` html
<a href="#carousel-cell2">View cell 2</a>
```

### Webpack & Browserify

``` js
var Flickity = require('flickity-hash');

var flkty = new Flickity( '.carousel', {
hash: true,
});
```

### RequireJS

``` js
requirejs( [ 'path/to/flickity-hash' ], function( Flickity ) {
var flkty = new Flickity( '.carousel', {
hash: true,
});
});
```

---

By [Metafizzy](https://metafizzy.co) 🌈🐻
2 changes: 1 addition & 1 deletion bower.json
@@ -1,6 +1,6 @@
{
"name": "flickity-hash",
"description": "Enable hash navigation for Flickity",
"description": "Select Flickity slides with links ",
"main": "hash.js",
"dependencies": {
"flickity": "^2.1.0"
Expand Down
12 changes: 4 additions & 8 deletions hash.js
@@ -1,5 +1,5 @@
/*!
* Flickity hash v1.0.0
* Flickity hash v1.0.1
* Enable hash navigation for Flickity
*/

Expand Down Expand Up @@ -52,7 +52,7 @@ proto._createHash = function() {
};

proto.activateHash = function() {
this.on( 'select', this.onSelectHash );
this.on( 'change', this.onChangeHash );

// overwrite initialIndex
if ( this.options.initialIndex === undefined && location.hash ) {
Expand All @@ -67,15 +67,11 @@ proto.activateHash = function() {


proto.deactivateHash = function() {
this.off( 'select', this.onSelectHash );
this.off( 'change', this.onChangeHash );
this.disconnectHashLinks();
};

proto.onSelectHash = function() {
if ( !this.isInitActivated ) {
// do not set hash for initial select
return;
}
proto.onChangeHash = function() {
var id = this.selectedElement.id;
if ( id ) {
var url = '#' + id;
Expand Down
4 changes: 2 additions & 2 deletions package.json
@@ -1,7 +1,7 @@
{
"name": "flickity-hash",
"version": "1.0.0",
"description": "Enable hash navigation for Flickity",
"version": "1.0.1",
"description": "Select Flickity slides with links ",
"main": "hash.js",
"dependencies": {
"flickity": "^2.1.0"
Expand Down

0 comments on commit 19c5c52

Please sign in to comment.