PhotoSwipe中文API(二)

配置

选项是在键 - 值对添加作为参数传递给PhotoSwipe构造,例如通过:

 1 var options = {
 2     index: 3,
 3     escKey: false,
 4 
 5     // ui option
 6     timeToIdle: 4000
 7 };
 8 var gallery = new PhotoSwipe( someElement, PhotoSwipeUI_Default, someItems, options);
 9 gallery.init();
10 
11 // Note that options object is cloned during the initialization.
12 // But you can access it via `gallery.options`.
13 // For example, to dynamically change `escKey`:
14 gallery.options.escKey = false;
15 
16 // `options.escKey = false` will not work

核心

index integer 0

开始放映指数。 0是第一滑动。必须是整数,而不是字符串。

getThumbBoundsFn function undefined

功能应该与坐标从初始变焦的动画将启动(或缩小出动画将结束)返回一个对象。

对象应包含三个属性:X(X位置,相对于文档),Y(Y位置,相对于文档),W(元素的宽度)。高度会自动根据大的图像大小来计算。例如,如果您返回{X:0,Y:0,W:50}缩放动画将在你的页面的左上角开始。

函数有一个参数 - 即打开或关闭项目的索引。

在非模态模式,相对于视口模板的位置应该从x和y中减去。看常见问题以获取更多信息。

例如,计算缩略图的位置:

 1 getThumbBoundsFn: function(index) {
 2 
 3     // find thumbnail element
 4     var thumbnail = document.querySelectorAll('.my-gallery-thumbnails')[index];
 5 
 6     // get window scroll Y
 7     var pageYScroll = window.pageYOffset || document.documentElement.scrollTop; 
 8     // optionally get horizontal scroll
 9 
10     // get position of element relative to viewport
11     var rect = thumbnail.getBoundingClientRect(); 
12 
13     // w = width
14     return {x:rect.left, y:rect.top + pageYScroll, w:rect.width};
15 
16 
17     // Good guide on how to get element coordinates:
18     // http://javascript.info/tutorial/coordinates
19 }

如果你的小缩略图的尺寸不匹配大的图像尺寸,考虑启用变焦+淡出过渡。您可以通过添加选项showHideOpacity做到这一点:真(尝试将其添加到上面CodePen来测试它的外观)。或通过添加hideAnimationDuration完全禁用过渡:0,showAnimationDuration:0。这个常见问题中的更多信息。

如果你想利用动画时不透明度为“隐藏”小缩略图:0,不可见性:隐藏或显示:无。不要强迫油漆和布局在动画的开头,以避免滞后。

showHideOpacity boolean false

如果设置为false:背景透明度和图像规模将动画(图像透明度始终为1)。如果设置为true:根PhotoSwipe元素的不透明性和图像规模将动画。
为了让刚转型的不透明度(未经调整),不要定义getThumbBoundsFn选项。

showAnimationDuration integer 333

以毫秒为单位的初始放大的过渡时间。设置为0来禁用。除了这个JS选项,你也需要改变PhotoSwipe CSS文件转换时间:

1 .pswp--animate_opacity,
2 .pswp__bg,
3 .pswp__caption,
4 .pswp__top-bar,
5 .pswp--has_mouse .pswp__button--arrow--left,
6 .pswp--has_mouse .pswp__button--arrow--right{
7     -webkit-transition: opacity 333ms cubic-bezier(.4,0,.22,1);
8     transition: opacity 333ms cubic-bezier(.4,0,.22,1);
9 }

如果你使用的无礼,你只需要更改主settings.scss过渡时间变量。

hideAnimationDuration integer 333

同样作为一个选项,只是关闭(缩放出)转型。 PhotoSwipe被打开后PSWP - 公开课将被添加到根元素,你可以用它在CSS中使用不同的过渡时间。

bgOpacity number 1

背景(.pswp_bg)透明度。应该是从0到1,即0.7。此样式是通过JS限定,而不是通过CSS,因为此值用于一些基于姿势的过渡。

spacing number 0.12

幻灯片之间的间距比。例如,0.12将呈现为滑动视口宽度的12%(四舍五入)。

allowPanToNext boolean true

允许刷卡导航到下一个/上一个项目时,当前项目被放大。选项始终是在没有硬件支持触控设备假的。

maxSpreadZoom number 2

进行扩展(变焦)手势时,最大缩放级别。 2意味着图像可以从原始尺寸被放大2倍。尽量避免在这里巨大的价值,因为过大的图像可以在移动导致内存问题(特别是在iOS)。

getDoubleTapZoom function

函数将返回缩放级别的图像将双击手势之后进行缩放其中,或图像本身,当用户点击缩放图标,或者鼠标点击。如果返回1的图像将被放大到原来的大小。

Default value:

 1 getDoubleTapZoom: function(isMouseClick, item) {
 2 
 3     // isMouseClick          - true if mouse, false if double-tap
 4     // item                  - slide object that is zoomed, usually current
 5     // item.initialZoomLevel - initial scale ratio of image
 6     //                         e.g. if viewport is 700px and image is 1400px,
 7     //                              initialZoomLevel will be 0.5
 8 
 9     if(isMouseClick) {
10 
11         // is mouse click on image or zoom icon
12 
13         // zoom to original
14         return 1;
15 
16         // e.g. for 1400px image:
17         // 0.5 - zooms to 700px
18         // 2   - zooms to 2800px
19 
20     } else {
21 
22         // is double-tap
23 
24         // zoom to original if initial zoom is less than 0.7x,
25         // otherwise to 1.5x, to make sure that double-tap gesture always zooms image
26         return item.initialZoomLevel < 0.7 ? 1 : 1.5;
27     }
28 }

函数被调用每一个放大的动画启动的时间。可以随意根据自己的尺寸和屏幕的DPI不同的图像返回不同的值。

loop boolean true

循环使用滑动手势时,幻灯片。如果设置为true,你就可以从上轻扫到第一张图像。选项始终是假的时,有不到3张幻灯片。

此选项没有关系箭头导航。箭头循环永久开启。您可以修改通过自定义UI此行为。

pinchToClose boolean true

捏关闭画廊的姿态。画廊的背景将逐渐淡出作为用户缩小。当手势完成后,画廊将关闭。

closeOnScroll boolean true

在页面滚动关闭画廊。选项可只是没有硬件支持触控设备。

closeOnVerticalDrag boolean true

垂直拖动关闭画廊时,当影像未缩放。始终为假时使用鼠标。

mouseUsed boolean false

选项允许如果使用与否鼠标就预定义。有些PhotoSwipe功能依赖于它,例如默认的UI左/右箭头会显示使用鼠标之后。如果设置为false,PhotoSwipe将开始检测时,鼠标的使用本身,当鼠标被发现mouseUsed事件触发。

escKey boolean true

键盘ESC键关闭PhotoSwipe。选项可以更改动态(yourPhotoSwipeInstance.options.escKey= FALSE)。

arrowKeys boolean true

键盘向左或向右箭头键导航。选项可以动态改变(yourPhotoSwipeInstance.options.arrowKeys=假)。

history boolean true

如果设置为false禁用历史模块(后退按钮关闭画廊,独特的URL为每张幻灯片)。您也可以刚刚从构建排除history.js模块。

galleryUID integer 1

画廊的唯一ID。由历史形成的模块URL时使用。例如,UID1画廊的第二张照片将有网址:http://example.com/#&gid=1&pid=2。

galleryPIDs boolean false

启用对于正在形成URL时使用的每个幻灯片对象自定义标识。如果选项设置为true,幻灯片对象必须具有PID(图片标识符)属性,可以是一个字符串或一个整数。 例如:

 1 var slides = [
 2     {
 3         src: 'path/to/1.jpg',
 4         w:500,
 5         h:400,
 6         pid: 'image-one'
 7     },
 8     {
 9         src: 'path/to/2.jpg',
10         w:300,
11         h:700,
12         pid: 'image-two'
13     },
14 
15     ... 
16 ];

......第二张幻灯片将URL http://example.com/#&gid=1&pid=image-two。

了解更多关于如何实现在FAQ部分定制的PID。

errorMsg string

未加载图像时的错误消息。 %URL%将图像的URL来代替。

Default value:

<div class="pswp__error-msg"><a href="%url%" target="_blank">The image</a> could not be loaded.</div>

preload array [1,1]

基于运动方向附近的幻灯片延迟加载。应该是两个整数数组,第一个 - 当前图像之前预加载的项目数,第二个 - 当前图像之后。 例如。如果你把它设置为[1,3],它会之前,在当前负载1的图像,目前后3图像。值不能小于1。

mainClass string undefined

字符串将被添加到根PhotoSwipe(.pswp)的元素类的名称。可以包含由空格分隔的多个类。

getNumItemsFn function

功能应该在画廊返回的项目总数。默认情况下它返回幻灯片数组的长度。不要把很复杂的代码在这里,函数经常执行。

focus boolean true

之后它的开放将焦点设置PhotoSwipe元素上。

isClickableElement function

Default value:

1 isClickableElement: function(el) {
2     return el.tagName === 'A';
3 }

函数应该检查元素(EL)是可以点击的。如果是 - PhotoSwipe不会叫的preventDefault和点击事件会通过。功能应该是轻是可能的,因为它是在拖动开始和拖动发布执行多次。

modal boolean true

控制PhotoSwipe是否应该扩大到占据整个视口。如果为假,则PhotoSwipe元件将模板的定位的父的大小。看看常见问题以获取更多信息。

Default UI Options

对于PhotoSwipe用户界面默认(DIST/ UI / photoswipe-UI-default.js)选项添加同样的方式和同样的目标为核心的选项。

  1 // Size of top & bottom bars in pixels,
  2 // "bottom" parameter can be 'auto' (will calculate height of caption)
  3 // option applies only when mouse is used, 
  4 // or width of screen is more than 1200px
  5 // 
  6 // (Also refer to `parseVerticalMargin` event)
  7 barsSize: {top:44, bottom:'auto'}, 
  8 
  9 // Adds class pswp__ui--idle to pswp__ui element when mouse isn't moving for 4000ms
 10 timeToIdle: 4000,
 11 
 12 // Same as above, but this timer applies when mouse leaves the window
 13 timeToIdleOutside: 1000,
 14 
 15 // Delay until loading indicator is displayed
 16 loadingIndicatorDelay: 1000,
 17 
 18 // Function builds caption markup
 19 addCaptionHTMLFn: function(item, captionEl, isFake) {
 20     // item      - slide object
 21     // captionEl - caption DOM element
 22     // isFake    - true when content is added to fake caption container
 23     //             (used to get size of next or previous caption)
 24 
 25     if(!item.title) {
 26         captionEl.children[0].innerHTML = '';
 27         return false;
 28     }
 29     captionEl.children[0].innerHTML = item.title;
 30     return true;
 31 },
 32 
 33 // Buttons/elements
 34 closeEl:true,
 35 captionEl: true,
 36 fullscreenEl: true,
 37 zoomEl: true,
 38 shareEl: true,
 39 counterEl: true,
 40 arrowEl: true,
 41 preloaderEl: true,
 42 
 43 // Tap on sliding area should close gallery
 44 tapToClose: false,
 45 
 46 // Tap should toggle visibility of controls
 47 tapToToggleControls: true,
 48 
 49 // Mouse click on image should close the gallery,
 50 // only when image is smaller than size of the viewport
 51 clickToCloseNonZoomable: true,
 52 
 53 // Element classes click on which should close the PhotoSwipe.
 54 // In HTML markup, class should always start with "pswp__", e.g.: "pswp__item", "pswp__caption".
 55 // 
 56 // "pswp__ui--over-close" class will be added to root element of UI when mouse is over one of these elements
 57 // By default it's used to highlight the close button.
 58 closeElClasses: ['item', 'caption', 'zoom-wrap', 'ui', 'top-bar'], 
 59 
 60 // Separator for "1 of X" counter
 61 indexIndicatorSep: ' / ',
 62 
 63 
 64 
 65 // Share buttons
 66 // 
 67 // Available variables for URL:
 68 // {{url}}             - url to current page
 69 // {{text}}            - title
 70 // {{image_url}}       - encoded image url
 71 // {{raw_image_url}}   - raw image url
 72 shareButtons: [
 73     {id:'facebook', label:'Share on Facebook', url:'https://www.facebook.com/sharer/sharer.php?u={{url}}'},
 74     {id:'twitter', label:'Tweet', url:'https://twitter.com/intent/tweet?text={{text}}&url={{url}}'},
 75     {id:'pinterest', label:'Pin it', url:'http://www.pinterest.com/pin/create/button/?url={{url}}&media={{image_url}}&description={{text}}'},
 76     {id:'download', label:'Download image', url:'{{raw_image_url}}', download:true}
 77 ],
 78 
 79 
 80 // Next 3 functions return data for share links
 81 // 
 82 // functions are triggered after click on button that opens share modal,
 83 // which means that data should be about current (active) slide
 84 getImageURLForShare: function( shareButtonData ) {
 85     // `shareButtonData` - object from shareButtons array
 86     // 
 87     // `pswp` is the gallery instance object,
 88     // you should define it by yourself
 89     // 
 90     return pswp.currItem.src || '';
 91 },
 92 getPageURLForShare: function( shareButtonData ) {
 93     return window.location.href;
 94 },
 95 getTextForShare: function( shareButtonData ) {
 96     return pswp.currItem.title || '';
 97 },
 98 
 99 // Parse output of share links
100 parseShareButtonOut: function(shareButtonData, shareButtonOut) {
101     // `shareButtonData` - object from shareButtons array
102     // `shareButtonOut` - raw string of share link element
103     return shareButtonOut;
104 }
原文地址:https://www.cnblogs.com/jiangxiaobo/p/5695930.html