图片/图形

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

也想出现在这里?联系我们
面包多

扑克堆叠式卡片轮播图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

服务范围 1、专业提供WordPress主题、插件汉化、优化、PHP环境配置等服务请详询在线客服
2、本站承接 WordPress、DedeCMS、ThinkPHP 等系统建站、仿站、开发、定制等服务
3、英文模板(主题)安装费用为120元/次,汉化主题首次免费安装(二次安装30元/次)
售后时间 周一至周五(法定节假日除外) 10:00-22:00
免责声明 本站所提供的模板(主题/插件)等资源仅供学习交流,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担,有部分资源为网上收集或仿制而来,若模板侵犯了您的合法权益,请来信通知我们(Email: 2107117185@qq.com),我们会及时删除,给您带来的不便,我们深表歉意!
免费下载
(0)

本文由 创客云 作者:PetitQ 发表,转载请注明来源!

也想出现在这里?联系我们
创客主机

热评文章

发表评论

精彩推荐

Supro - 极简电商网站模板WordPress汉化主题

Envato Affiliates

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

Hi, 如果您有主题插件代购(30-600元)汉化等建站相关业务,可以 跟我联系 哦!
欢迎投稿
嘿,欢迎咨询!