图片/图形

swing 弹性摇摆效果的卡片js拖拽插件

阿里云


swing 是一款可以容器中的卡片元素拖拽到容器外的 js 拖拽插件。该拖拽插件在拖动卡片元素到容器外后,放下卡片时有很酷的卡片弹性左右摇摆效果。如果拖动距离不够,卡片将被拉回容器中。

HTML 结构

html 结构使用一个无序列表来作为图片栈,它被包裹在一个作为容器的 div 中。

也想出现在这里?联系我们
创客主机
  1. <div id="viewport">
  2.     <ul class="stack">
  3.         <li class="clubs">&#x2663;</li>
  4.         <li class="diamonds">&#x2666;</li>
  5.         <li class="hearts">&#x2665;</li>
  6.         <li class="spades">&#x2660;</li>
  7.     </ul>
  8. </div>

JAVASCRIPT

  1. var stack,
  2.     cards;
  3.  
  4. // Prepare the cards in the stack for iteration.
  5. cards = [].slice.call(document.querySelectorAll('ul li'))
  6.  
  7. // An instance of the Stack is used to attach event listeners.
  8. stack = Swing.Stack();
  9.  
  10. cards.forEach(function (targetElement) {
  11.     // Add card element to the Stack.
  12.     stack.createCard(targetElement);
  13. });
  14.  
  15. // Add event listener for when a card is thrown out of the stack.
  16. stack.on('throwout', function (e) {
  17.     // e.target Reference to the element that has been thrown out of the stack.
  18.     // e.throwDirection Direction in which the element has been thrown (Card.DIRECTION_LEFT, Card.DIRECTION_RIGHT).
  19.  
  20.     console.log('Card has been thrown out of the stack.');
  21.     console.log('Throw direction: ' + (e.throwDirection == Card.DIRECTION_LEFT ? 'left' : 'right'));
  22. });
  23.  
  24. // Add event listener for when a card is thrown in the stack, including the spring back into place effect.
  25. stack.on('throwin', function (e) {
  26.     console.log('Card has snapped back to the stack.');
  27. });

配置

  1. var stack,
  2.     config;
  3.  
  4. config = {
  5.     /**
  6.      * Invoked in the event of dragmove.
  7.      * Returns a value between 0 and 1 indicating the completeness of the throw out condition.
  8.      * Ration of the absolute distance from the original card position and element width.
  9.      * 
  10.      * @param {Number} offset Distance from the dragStart.
  11.      * @param {HTMLElement} element Element.
  12.      * @return {Number}
  13.      */
  14.     throwOutConfidence: function (offset, element) {
  15.         return Math.min(Math.abs(offset) / element.offsetWidth, 1);
  16.     }
  17. };
  18. stack = stack = Swing.Stack(config);

详细参数

名称 描述 默认值
isThrowOut Invoked in the event of dragend. Determines if element is being thrown out of the stack. Element is considered to be thrown out when throwOutConfidence is equal to 1.
throwOutConfidence 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.
throwOutDistance Invoked when card is added to the stack. The card is thrown to this offset from the stack. The value is a random number between minThrowOutDistance and maxThrowOutDistance.
minThrowOutDistance In effect when throwOutDistance is not overwritten. 450
maxThrowOutDistance In effect when throwOutDistance is not overwritten. 500
rotation Invoked in the event of dragmove. Determine the rotation of the element. Rotation is equal to the proportion of horizontal and vertical offset times the maximumRotation constant.
maxRotation In effect when rotation is not overwritten. 20
transform Invoked in the event of dragmove and every time the physics solver is triggered. Uses CSS transform to translate element position and rotation.

所有的配置参数都是可选的。

方法

  1. var stack,
  2.     card;
  3.  
  4. stack = stack = Swing.Stack();
  5. card = stack.createCard(HTMLElement);
名称 描述
stack.createCard(element) 创建一个和元素相关联的卡片实例。
stack.getCard(element) 返回卡片相关联的元素。
stack.on(event, listener) 为栈添加一个事件监听。
card.on(event, listener) 为卡片添加一个事件监听。
card.throwIn(x, y) 从任意位置将卡片拖拽回栈中。x 和 y 是开始抛出的位置。
card.throwOut(x, y) 将卡片拖拽到栈外任意位置。x 和 y 是开始抛出的位置。
card.destroy() 销毁卡片。

将卡片拖拽到容器外

可以使用 card.throwOut(x, y)方法来将卡片拖拽到容器外。你可已设置卡片的拖拽方向,例如:

  1. card.throwOut(Card.DIRECTION_LEFT, 0);
  2. card.throwOut(Card.DIRECTION_RIGHT, 0);

如果想各种拖拽行为各不相同,可以把 y 参数设置为一个随机值。

事件监听

事件监听可以使用 on 方法附加到 Swing.Stack 或 Swing.Card 上。

  1. var stack,
  2.     card;
  3.  
  4. stack = stack = Swing.Stack();
  5.  
  6. card = stack.createCard(HTMLElement);
  7.  
  8. card.on('throwout', function () {});
  9. stack.on('throwout', function () {});
名称 描述
throwout When card has been thrown out of the stack.
throwoutend When card has been thrown out of the stack and the animation has ended.
throwoutleft Shorthand for throwout event in the Card.DIRECTION_LEFT direction.
throwoutright Shorthand for throwout event in the Card.DIRECTION_RIGHT direction.
throwin When card has been thrown into the stack.
throwinend When card has been thrown into the stack and the animation has ended.
dragstart drag start.
dragmove drag move.
dragend drag end.

事件对象

事件侦听器是用一个参数 eventobject 调用:

  1. var stack;
  2.  
  3. stack = stack = Swing.Stack();
  4.  
  5. stack.on('throwout', function (eventObject) {});
名称 描述
target The element being dragged.
direction The direction in which the element is being dragged: Card.DIRECTION_LEFT or Card.DIRECTION_RIGHT
throwOutConfidence A value between 0 and 1 indicating the completeness of the throw out condition.

更多资料:https://github.com/gajus/swing

swing 弹性摇摆效果的卡片 js 拖拽插件

已有 450 人购买
查看演示升级 VIP立刻购买

演示地址 下载地址
收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!