图片/图形

扑克堆叠式卡片轮播图jQuery插件

阿里云


这是一款 js 扑克堆叠式卡片轮播图插件。高轮播图的所有图片像扑克牌一样堆叠在一起,通过鼠标拖拽最上面的一张图片,就可以显示出下一张图片来。

使用方法

在 HTML 文件中引入

也想出现在这里?联系我们
创客主机
  1. <script src="//code.jquery.com/hammer.min.js"></script>

HTML 结构

  1. <div id="board"></div>

初始化插件

然后通过下面的代码来初始化插件。

  1. class Carousel {
  2.  
  3.   constructor(element) {
  4.  
  5.     this.board = element
  6.  
  7.     // add first two cards programmatically
  8.     this.push()
  9.     this.push()
  10.  
  11.     // handle gestures
  12.     this.handle()
  13.  
  14.   }
  15.  
  16.   handle() {
  17.  
  18.     // list all cards
  19.     this.cards = this.board.querySelectorAll('.card')
  20.  
  21.     // get top card
  22.     this.topCard = this.cards[this.cards.length-1]
  23.  
  24.     // get next card
  25.     this.nextCard = this.cards[this.cards.length-2]
  26.  
  27.     // if at least one card is present
  28.     if (this.cards.length > 0) {
  29.  
  30.       // set default top card position and scale
  31.       this.topCard.style.transform = 'translateX(-50%) translateY(-50%) rotate(0deg) rotateY(0deg) scale(1)'
  32.  
  33.       // destroy previous Hammer instance, if present
  34.       if (this.hammer) this.hammer.destroy()
  35.  
  36.       // listen for tap and pan gestures on top card
  37.       this.hammer = new Hammer(this.topCard)
  38.       this.hammer.add(new Hammer.Tap())
  39.       this.hammer.add(new Hammer.Pan({ position: Hammer.position_ALL, threshold: 0 }))
  40.  
  41.       // pass events data to custom callbacks
  42.       this.hammer.on('tap', (e) => { this.onTap(e) })
  43.       this.hammer.on('pan', (e) => { this.onPan(e) })
  44.  
  45.     }
  46.  
  47.   }
  48.  
  49.   onTap(e) {
  50.  
  51.     // get finger position on top card
  52.     let propX = (e.center.x - e.target.getBoundingClientRect().left) / e.target.clientWidth
  53.  
  54.     // get degree of Y rotation (+/-15 degrees)
  55.     let rotateY = 15 * (propX < 0.05 ? -1 : 1)
  56.  
  57.     // change the transition property
  58.     this.topCard.style.transition = 'transform 100ms ease-out'
  59.  
  60.     // rotate
  61.     this.topCard.style.transform = 'translateX(-50%) translateY(-50%) rotate(0deg) rotateY(' + rotateY + 'deg) scale(1)'
  62.  
  63.     // wait transition end
  64.     setTimeout(() => {
  65.       // reset transform properties
  66.       this.topCard.style.transform = 'translateX(-50%) translateY(-50%) rotate(0deg) rotateY(0deg) scale(1)'
  67.     }, 100)
  68.  
  69.   }
  70.  
  71.   onPan(e) {
  72.  
  73.     if (!this.isPanning) {
  74.  
  75.       this.isPanning = true
  76.  
  77.       // remove transition properties
  78.       this.topCard.style.transition = null
  79.       if (this.nextCard) this.nextCard.style.transition = null
  80.  
  81.       // get top card coordinates in pixels
  82.       let style = window.getComputedStyle(this.topCard)
  83.       let mx = style.transform.match(/^matrix\((.+)\)$/)
  84.       this.startPosX = mx ? parseFloat(mx[1].split(', ')[4]) : 0
  85.       this.startPosY = mx ? parseFloat(mx[1].split(', ')[5]) : 0
  86.  
  87.       // get top card bounds
  88.       let bounds = this.topCard.getBoundingClientRect()
  89.  
  90.       // get finger position on top card, top (1) or bottom (-1)
  91.       this.isDraggingFrom = (e.center.y - bounds.top) > this.topCard.clientHeight / 2 ? -1 : 1
  92.  
  93.     }
  94.  
  95.     // calculate new coordinates
  96.     let posX = e.deltaX + this.startPosX
  97.     let posY = e.deltaY + this.startPosY
  98.  
  99.     // get ratio between swiped pixels and the axes
  100.     let propX = e.deltaX / this.board.clientWidth
  101.     let propY = e.deltaY / this.board.clientHeight
  102.  
  103.     // get swipe direction, left (-1) or right (1)
  104.     let dirX = e.deltaX < 0 ? -1 : 1
  105.  
  106.     // calculate rotation, between 0 and +/- 45 deg
  107.     let deg = this.isDraggingFrom * dirX * Math.abs(propX) * 45
  108.  
  109.     // calculate scale ratio, between 95 and 100 %
  110.     let scale = (95 + (5 * Math.abs(propX))) / 100
  111.  
  112.     // move top card
  113.     this.topCard.style.transform = 'translateX(' + posX + 'px) translateY(' + posY + 'px) rotate(' + deg + 'deg) rotateY(0deg) scale(1)'
  114.  
  115.     // scale next card
  116.     if (this.nextCard) this.nextCard.style.transform = 'translateX(-50%) translateY(-50%) rotate(0deg) rotateY(0deg) scale(' + scale + ')'
  117.  
  118.     if (e.isFinal) {
  119.  
  120.       this.isPanning = false
  121.  
  122.       let successful = false
  123.  
  124.       // set back transition properties
  125.       this.topCard.style.transition = 'transform 200ms ease-out'
  126.       if (this.nextCard) this.nextCard.style.transition = 'transform 100ms linear'
  127.  
  128.       // check threshold
  129.       if (propX > 0.25 && e.direction == Hammer.DIRECTION_RIGHT) {
  130.  
  131.         successful = true
  132.         // get right border position
  133.         posX = this.board.clientWidth
  134.  
  135.       } else if (propX < -0.25 && e.direction == Hammer.DIRECTION_LEFT) {
  136.  
  137.         successful = true
  138.         // get left border position
  139.         posX = - (this.board.clientWidth + this.topCard.clientWidth)
  140.  
  141.       } else if (propY < -0.25 && e.direction == Hammer.DIRECTION_UP) {
  142.  
  143.         successful = true
  144.         // get top border position
  145.         posY = - (this.board.clientHeight + this.topCard.clientHeight)
  146.  
  147.       }
  148.  
  149.       if (successful) {
  150.  
  151.         // throw card in the chosen direction
  152.         this.topCard.style.transform = 'translateX(' + posX + 'px) translateY(' + posY + 'px) rotate(' + deg + 'deg)'
  153.  
  154.         // wait transition end
  155.         setTimeout(() => {
  156.           // remove swiped card
  157.           this.board.removeChild(this.topCard)
  158.           // add new card
  159.           this.push()
  160.           // handle gestures on new top card
  161.           this.handle()
  162.         }, 200)
  163.  
  164.       } else {
  165.  
  166.         // reset cards position
  167.         this.topCard.style.transform = 'translateX(-50%) translateY(-50%) rotate(0deg) rotateY(0deg) scale(1)'
  168.         if (this.nextCard) this.nextCard.style.transform = 'translateX(-50%) translateY(-50%) rotate(0deg) rotateY(0deg) scale(0.95)'
  169.  
  170.       }
  171.  
  172.     }
  173.  
  174.   }
  175.  
  176.   push() {
  177.  
  178.     let card = document.createElement('div')
  179.  
  180.     card.classList.add('card')
  181.  
  182.     // add you own content to the cards
  183.  
  184.     if (this.board.firstChild) {
  185.       this.board.insertBefore(card, this.board.firstChild)
  186.     } else {
  187.       this.board.append(card)
  188.     }
  189.  
  190.   }
  191.  
  192. }

Github 网址:https://github.com/simonepm/likecarousel

扑克堆叠式卡片轮播图 jQuery 插件

已有 556 人购买
  • cgq5
查看演示升级 VIP立刻购买

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

发表回复

热销模板

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

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