Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A swipeable cards interface. The swipe-left/swipe-right for yes/no input. As seen in apps like Jelly and Tinder.

License

NotificationsYou must be signed in to change notification settings

gajus/swing

Repository files navigation

GitSpo MentionsTravis build statusNPM versionjs-canonical-style

A swipeable cards interface. The swipe-left/swipe-right for yes/no input. As seen in apps likeJelly andTinder, andmany others.

Give it aswing! and pleasetweet it if you like it. : )

Card stack example.

Contents

Usage Examples

The code for all of the examples is in the./examples/ folder.

Raise an issue if you are missing an example.

Use Case

A collection of observations about the extended use case of the swipeable cards interface, that I found useful when considering the implementation.

Single-Handed Navigation

Mobile devices are frequently used on-the-go, which drastically increases the probability that you'll attempt to navigate apps using just one hand, with the key digit being the mighty thumb.Instead of browsing endless lists for the hidden perfect piece of data — be it the right music for the moment, what to do tonight, or your next potential hookup — card-swiping turns decision making into a highly engaging Choose-Your-Own-Adventure game.

– https://medium.com/@janel_az/small-data-why-tinder-like-apps-are-the-way-of-the-future-1a4d5703b4b

Digestible Unit of Information

[..] the "card" on a mobile device becomes more and more important as a digestible unit of information on a small screen for users who are on the go and mostly glancing through their apps before settling into the ones that truly engage them.

– http://techcrunch.com/2013/09/22/mobile-apps-card-interfaces-and-our-opposable-thumbs/

Data

More than a scroll and perhaps even more than discrete taps themselves, cards create repetitive, deliberate, discrete decision moments over and over. And as the user swipes, you can learn.The time they swipe, the speed they swipe, what they swiped, the geolocation where they swiped, and even how similar the results of that swipe are vs. a swipe earlier that session are all possibilities that are yielding smarter apps for you and me every day.

http://www.itsmakeable.com/unconventional-wisdom/good-user-experience-design-ux-can-do-what-now/

Quick Start

<ul><li></li><li></li><li></li></ul>
// Prepare the cards in the stack for iteration.constcards=[].slice.call(document.querySelectorAll('ul li'));// An instance of the Stack is used to attach event listeners.conststack=Swing.Stack();cards.forEach((targetElement)=>{// Add card element to the Stack.stack.createCard(targetElement);});// Add event listener for when a card is thrown out of the stack.stack.on('throwout',(event)=>{// e.target Reference to the element that has been thrown out of the stack.// e.throwDirection Direction in which the element has been thrown (Direction.LEFT, Direction.RIGHT).console.log('Card has been thrown out of the stack.');console.log('Throw direction: '+(event.throwDirection==Direction.LEFT ?'left' :'right'));});// Add event listener for when a card is thrown in the stack, including the spring back into place effect.stack.on('throwin',()=>{console.log('Card has snapped back to the stack.');});

Configuration

constconfig={/**   * Invoked in the event of dragmove.   * Returns a value between 0 and 1 indicating the completeness of the throw out condition.   * Ration of the absolute distance from the original card position and element width.   *   *@param {number} xOffset Distance from the dragStart.   *@param {number} yOffset Distance from the dragStart.   *@param {HTMLElement} element Element.   *@returns {number}   */throwOutConfidence:(xOffset,yOffset,element)=>{constxConfidence=Math.min(Math.abs(xOffset)/element.offsetWidth,1);constyConfidence=Math.min(Math.abs(yOffset)/element.offsetHeight,1);returnMath.max(xConfidence,yConfidence);}};conststack=stack=Swing.Stack(config);
NameDescriptionDefault
isThrowOutInvoked in the event ofdragend. Determines if element is being thrown out of the stack.Element is considered to be thrown out whenthrowOutConfidence is equal to 1.
allowedDirectionsArray of directions in which cards can be thrown out.[Direction.DOWN, Direction.LEFT, Direction.RIGHT, Direction.UP].
throwOutConfidenceInvoked in the event ofdragmove. Returns a value between 0 and 1 indicating the completeness of the throw out condition.Ration of the absolute distance from the original card position and element width.
throwOutDistanceInvoked when card is added to the stack. The card is thrown to this offset from the stack.The value is a random number betweenminThrowOutDistance andmaxThrowOutDistance.
minThrowOutDistanceIn effect whenthrowOutDistance is not overwritten.450.
maxThrowOutDistanceIn effect whenthrowOutDistance is not overwritten.500.
rotationInvoked in the event ofdragmove. Determine the rotation of the element.Rotation is equal to the proportion of horizontal and vertical offset times themaximumRotation constant.
maxRotationIn effect whenrotation is not overwritten.20.
transformInvoked in the event ofdragmove and every time the physics solver is triggered.Uses CSS transform to translate element position and rotation.
allowMovementFunction that determines if movement is allowed when a movement event is fired. It has two arguments, theevent object and aboolean set to true if on a touch device.A function that returns true for all movement events.

All of the configuration parameters are optional. Refer to the source code of thecard module to learn the parameters associated with every callback.

Methods

conststack=Swing.Stack();constcard=stack.createCard(HTMLElement);
NameDescription
stack.createCard(element, prepend)Creates an instance of Card and associates it with the element. If prepend is true, the card is prepended to the stack, instead of appended [default: false].
stack.getCard(element)Returns card associated with an element.
stack.on(event, listener)Attaches anevent listener.
card.on(event, listener)Attaches anevent listener.
card.throwIn(coordinateX, coordinateY)Throws a card into the stack from an arbitrary position.coordinateX, coordinateY is the position at the start of the throw.
card.throwOut(coordinateX, coordinateY)Throws a card out of the stack in the direction away from the original offset.coordinateX, coordinateY is the position at the start of the throw.
card.destroy()Unbinds all Hammer.Manager events. Removes the listeners from the physics simulation.

Throwing Card Out of the Stack

Use thecard.throwOut(coordinateX, coordinateY) method to throw the card out of the stack. Offset the position to whatever direction you want to throw the card, e.g.

card.throwOut(Direction.LEFT,0);card.throwOut(Direction.RIGHT,0);

To make the animation more diverse, use random value for thecoordinateY parameter.

Events

Event listener can be attached to an instance ofSwing.Stack orSwing.Card using theon method:

conststack=Swing.Stack();constcard=stack.createCard(HTMLElement);card.on('throwout',()=>{});stack.on('throwout',()=>{});
NameDescription
throwoutWhen card has been thrown out of the stack.
throwoutendWhen card has been thrown out of the stack and the animation has ended.
throwoutdownShorthand forthrowout event in theDirection.DOWN direction.
throwoutleftShorthand forthrowout event in theDirection.LEFT direction.
throwoutrightShorthand forthrowout event in theDirection.RIGHT direction.
throwoutupShorthand forthrowout event in theDirection.UP direction.
throwinWhen card has been thrown into the stack.
throwinendWhen card has been thrown into the stack and the animation has ended.
dragstartHammerpanstart.
dragmoveHammerpanmove.
dragendHammerpanend.
destroyCardWhen card.destroy calls stack.destroyCard.

Event Object

Event listener is invoked with a singleeventObject parameter:

conststack=Swing.Stack();stack.on('throwout',(eventObject)=>{});
NameValue
targetThe element being dragged.
directionThe direction in which the element is being dragged:Direction.DOWN,Direction.LEFT,Direction.RIGHT orDirection.UP.
throwOutConfidenceA value between 0 and 1 indicating the completeness of the throw out condition.

About

A swipeable cards interface. The swipe-left/swipe-right for yes/no input. As seen in apps like Jelly and Tinder.

Resources

License

Stars

Watchers

Forks

Sponsor this project

  •  

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp