响应式和灵活 内容滑块插件 flickity

  • 源码大小:151.18KB
  • 所需积分:1积分
  • 源码编号:19JP-3771
  • 浏览次数:1091次
  • 最后更新:2023年07月21日
  • 所属栏目:滑块
我要下载
加入收藏
本站默认解压密码:19jp.com 或 19jp_com

简介

flickity是一个通用的、灵活的、模块化的jQuery滑块插件,用于创建响应灵敏的、支持触摸的内容滑块/画廊/幻灯片/旋转木马,具有基于物理的动画和全功能API。

当前许可的GPL v3。也可以用作Vanilla JavaScript插件。

参见:

  • jQuery/JavaScript/CSS中的最佳旋转木马插件
  • flickity响应:flickity的响应选项。

安装和下载:

  1. # NPM
  2. $ npm install flickity --save

基本用法:

1.在文档中包含flickity的JavaScript和CSS。

  1. <!-- from local -->
  2. <link href="dist/flickity.min.css" rel="stylesheet">
  3. <script src="dist/flickity.pkgd.min.js"></script>
  4. <!-- OR from a CDN -->
  5. <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/flickity.min.css" rel="stylesheet">
  6. <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/flickity.pkgd.min.js"></script>

2.在文档中包含jQuery库(可选)。

  1. <script src="/path/to/cdn/jquery.min.js"></script>

3.将您的内容嵌入到滑块中,如下所示:

  1. <div class="carousel">
  2. <div class="carousel-cell">Slide 1</div>
  3. <div class="carousel-cell">Slide 2</div>
  4. <div class="carousel-cell">Slide 3</div>
  5. ... More Slides Here ...
  6. </div>

4.将data flickity属性添加到顶部容器以初始化转盘。

  1. <div class="carousel" data-flickity='{ options here }'>
  2. <div class="carousel-cell">Slide 1</div>
  3. <div class="carousel-cell">Slide 2</div>
  4. <div class="carousel-cell">Slide 3</div>
  5. ... More Slides Here ...
  6. </div>

5.使用JavaScript(jQuery)初始化转盘。

  1. $('.carousel').flickity();

6.使用香草JavaScript初始化滑块。

  1. // jQuery
  2. $('.carousel').flickity({
  3. // options here
  4. });
  5.  
  6. // Vanilla JS
  7. var myCarousel = new Flickity( '.carousel', {
  8. // options here
  9. });

7.通过将选项对象传递到Flickity来配置转盘滑块。

  1. $('.carousel').flickity({
  2.  
  3. // enable keyboard navigation
  4. accessibility: true,
  5.  
  6. // make the carousel's height fit the selected slide
  7. adaptiveHeight: false,
  8.  
  9. // enable auto play
  10. // set delay time to enable the autoplay
  11. // e.g. autoPlay: 5000
  12. autoPlay: false,
  13.  
  14. // 'center', 'left', or 'right'
  15. // or a decimal 0-1, 0 is beginning (left) of container, 1 is end (right)
  16. cellAlign: 'center',
  17.  
  18. // cell selector
  19. cellSelector: undefined,
  20.  
  21. // will contain cells to container
  22. // so no excess scroll at beginning or end
  23. // has no effect if wrapAround is enabled
  24. contain: false,
  25.  
  26. // enable dragging & flicking when the slides > 1
  27. draggable: '>1',
  28.  
  29. // the number of pixels a user must scroll horizontally to start dragging
  30. dragThreshold: 3,
  31.  
  32. // enable content to be freely scrolled and flicked
  33. // without aligning cells
  34. freeScroll: false,
  35.  
  36. // smaller number = easier to flick farther
  37. friction: 0.2,
  38.  
  39. // group cells together in slides
  40. groupCells: false,
  41.  
  42. // enable image lazy load
  43. // you can specify the original img src in the data-flickity-lazyload attribute
  44. lazyLoad: true,
  45.  
  46. // intial slide
  47. initialIndex: 0,
  48.  
  49. // set positioning in percent values, rather than pixels
  50. // Enable if items have percent widths
  51. // Disable if items have pixel widths, like images
  52. percentPosition: true,
  53.  
  54. // enable next/prev buttons
  55. prevNextButtons: true,
  56.  
  57. // enable pagination dots
  58. pageDots: true,
  59.  
  60. // listens to window resize events to adjust size & positions
  61. resize: true,
  62.  
  63. // enable RTL support
  64. rightToLeft: false,
  65.  
  66. // set the height of the gallery
  67. setGallerySize: true,
  68.  
  69. // watches the content of :after of the element
  70. // activates if #element:after { content: 'flickity' }
  71. // IE8 and Android 2.3 do not support watching :after
  72. // set watch: 'fallbackOn' to enable for these browsers
  73. watchCSS: false,
  74.  
  75. // at end of cells, wraps-around to first for infinite scrolling
  76. wrapAround: false
  77.  
  78. });

8.API方法。

  1. // jQuery
  2. // $carousel.flickity('methodName', parameters);
  3. // Vanilla
  4. // myCarousel.methodName(parameters);
  5.  
  6. // go to a specific slide
  7. $carousel.flickity( 'select', index, isWrapped, isInstant );
  8.  
  9. // go to the previous slide
  10. $carousel.flickity( 'previous', isWrapped, isInstant );
  11.  
  12. // go to the next slide
  13. $carousel.flickity( 'next', isWrapped, isInstant );
  14.  
  15. // select a slide
  16. $carousel.flickity( 'selectCell', value, isWrapped, isInstant );
  17.  
  18. // resize & re-position the carousel
  19. $carousel.flickity('resize');
  20. $carousel.flickity('reposition');
  21.  
  22. // add more slides
  23. $carousel.flickity( 'prepend', elements );
  24. $carousel.flickity( 'append', elements );
  25.  
  26. // insert a slide
  27. $carousel.flickity( 'insert', elements, index );
  28.  
  29. // remove a slide
  30. $carousel.flickity( 'remove', elements );
  31.  
  32. // enable/disable/pause/resume autoplay
  33. $carousel.flickity('playPlayer');
  34. $carousel.flickity('stopPlayer');
  35. $carousel.flickity('pausePlayer')
  36. $carousel.flickity('unpausePlayer');
  37.  
  38. // set the carousel to fullscreen
  39. $carousel.flickity('viewFullscreen');
  40.  
  41. // exit the fullscreen mode
  42. $carousel.flickity('exitFullscreen');
  43.  
  44. // toggle the fullscreen mode
  45. $carousel.flickity('toggleFullscreen');
  46.  
  47. // destroy the plugin
  48. $carousel.flickity('destroy');
  49.  
  50. // reload all slides
  51. $carousel.flickity('reloadCells');
  52.  
  53. // get the elements of the slide
  54. var cellElements = $carousel.flickity('getCellElements');

9.事件处理程序。

  1. // jQuery
  2. // $carousel.on('eventName', function);
  3. // $carousel.off('eventName', function);
  4. // $carousel.once('eventName', function);
  5. // Vanilla
  6. // myCarousel.on('eventName', function);
  7. // myCarousel.off('eventName', function);
  8. // myCarousel.once('eventName', function);
  9.  
  10. $carousel.on( 'ready.flickity', function() {
  11. // on ready
  12. });
  13.  
  14. $carousel.on( 'change.flickity', function( event, index ) {
  15. // on change
  16. });
  17.  
  18. $carousel.on( 'select.flickity', function( event, index ) {
  19. // when a slide is selected
  20. });
  21.  
  22. $carousel.on( 'settle.flickity', function( event, index ) {
  23. // when the carousel is settled at its end position
  24. });
  25.  
  26. $carousel.on( 'scroll.flickity', function( event, progress ) {
  27. // on move
  28. });
  29.  
  30. $carousel.on( 'dragStart.flickity', function( event, pointer ) {
  31. // on drag start
  32. });
  33.  
  34. $carousel.on( 'dragMove.flickity', function( event, pointer, moveVector ) {
  35. // on drag move
  36. });
  37.  
  38. $carousel.on( 'dragEnd.flickity', function( event, pointer ) {
  39. // on drag end
  40. });
  41.  
  42. $carousel.on( 'pointerDown.flickity', function( event, pointer ) {
  43. // when the user's pointer (mouse, touch, pointer) presses down
  44. });
  45.  
  46. $carousel.on( 'pointerMove.flickity', function( event, pointer, moveVector ) {
  47. // when the user's pointer moves.
  48. });
  49.  
  50. $carousel.on( 'pointerUp.flickity', function( event, pointer ) {
  51. // when the user's pointer unpresses
  52. });
  53.  
  54. $carousel.on( 'staticClick.flickity', function( event, pointer, cellElement, cellIndex ) {
  55. // when the user's pointer is pressed and unpressed and has not moved enough to start dragging
  56. });
  57.  
  58. $carousel.on( 'lazyLoad.flickity', function( event, cellElement ) {
  59. // after an image has been loaded
  60. });
  61.  
  62. $carousel.on( 'bgLazyLoad.flickity', function( event, element ) {
  63. // after a background image has been loaded
  64. });
  65.  
  66. $carousel.on( 'fullscreenChange.flickity', function( event, isFullscreen ) {
  67. // after entering or exiting fullscreen mode
  68. });

更新日志:

v2.3.0(2021 12月20日)

  • 解决iOS 15上的拖动和滚动问题

v2.2.2(2021 4月1日)

  • 碰撞安全警报的几个依赖项
  • 从Gulp脚本切换到npm脚本
  • 将linting从JSHint切换到ESLint
  • 删除销毁时隐藏的咏叹调

2019-08-20

  • 文档已更新

v2.1.2版本(2018-10-08)

  • 修复在(iOS 9.3/Safar)上拖动,在_pointerDownDefault中复制页面X、页面Y

预览截图