PhotoSwipe中文API(三)
http://photoswipe.com/documentation/api.html
所有的方法和这个网页上列出的属性是公开的。如果你想看看例子什么API可以做的,拿在默认PhotoSwipe UI源看看。
您可以初始化,例如在得到PhotoSwipe实例对象:
var photoswipeInstance = new PhotoSwipe( /* ... */ );
Methods
1 var pswp = new PhotoSwipe( /* ... */ ); 2 3 // Initialize and open gallery 4 // (you can bind events before this method) 5 pswp.init(); 6 7 // Go to slide by index 8 // @param {int} `index` 9 pswp.goTo(index); 10 11 // Go to the next slide 12 pswp.next(); 13 14 // Go to the previous slide 15 pswp.prev(); 16 17 // Update gallery size 18 // @param {boolean} `force` If you set it to `true`, 19 // size of the gallery will be updated 20 // even if viewport size hasn't changed. 21 pswp.updateSize(force); 22 23 // Close gallery 24 pswp.close(); 25 26 // Destroy gallery, 27 // automatically called after close() 28 pswp.destroy() 29 30 // Zoom current slide to (optionally with animation) 31 // @param {number} `destZoomLevel` Destination scale number. 1 - original. 32 // pswp.currItem.fitRatio - image will fit into viewport. 33 // @param {object} `centerPoint` Object of x and y coordinates, relative to viewport. 34 // @param {int} `speed` Animation duration. Can be 0. 35 // @param {function} `easingFn` Easing function (optional). Set to false to use default easing. 36 // @param {function} `updateFn` Function will be called on each update frame. (optional) 37 // 38 // Example below will 2x zoom to center of slide: 39 // pswp.zoomTo(2, {x:pswp.viewportSize.x/2,y:pswp.viewportSize.y/2}, 2000, false, function(now) { 40 // 41 // }); 42 pswp.zoomTo(destZoomLevel, centerPoint, speed, easingFn, updateFn); 43 44 // Apply zoom and pan to the current slide 45 // 46 // @param {number} `zoomLevel` 47 // @param {int} `panX` 48 // @param {int} `panY` 49 // 50 // For example: `pswp.applyZoomPan(1, 0, 0)` 51 // will zoom current image to the original size 52 // and will place it on top left corner 53 // 54 // 55 pswp.applyZoomPan(zoomLevel, panX, panY);
Properties
// current slide object pswp.currItem // items array (slides, images) pswp.items // size of sliding viewport pswp.viewportSize // object holds all functions from framework // framework-bridge.js pswp.framework // UI object (e.g. PhotoSwipeUI_Default instance) pswp.ui // background element (pswp__bg) pswp.bg // container element (pswp__container) pswp.container // options pswp.options // Even though methods below aren't technically properties, we list them here: // current item index (int) pswp.getCurrentIndex(); // total number of items pswp.options.getNumItemsFn() // current zoom level (number) pswp.getZoomLevel(); // one (or more) pointer is used pswp.isDragging(); // two (or more) pointers are used pswp.isZooming(); // `true` when transition between is running (after swipe) pswp.isMainScrollAnimating();
Events
PhotoSwipe使用非常简单的事件/消息系统。它有两个方法,喊(触发事件),听(处理事件)。对于现在还没有解除绑定监听方法,但是当PhotoSwipe关闭所有的人都将被清除。
var pswp = new PhotoSwipe(/* ... */); // Listen for "helloWorld" event pswp.listen('helloWorld', function(name) { alert('Name is: ' + name); }); // Trigger "helloWorld" event pswp.shout('helloWorld', 'John' /* you may pass more arguments */);
Available events:
1 // Before slides change 2 // (before the content is changed, but after navigation) 3 // Update UI here (like "1 of X" indicator) 4 pswp.listen('beforeChange', function() { }); 5 6 // After slides change 7 // (after content changed) 8 pswp.listen('afterChange', function() { }); 9 10 // Image loaded 11 pswp.listen('imageLoadComplete', function(index, item) { 12 // index - index of a slide that was loaded 13 // item - slide object 14 }); 15 16 // Viewport size changed 17 pswp.listen('resize', function() { }); 18 19 // Triggers when PhotoSwipe "reads" slide object data, 20 // which happens before content is set, or before lazy-loading is initiated. 21 // Use it to dynamically change properties 22 pswp.listen('gettingData', function(index, item) { 23 // index - index of a slide that was loaded 24 // item - slide object 25 26 // e.g. change path to the image based on `something` 27 if( something ) { 28 item.src = item.something; 29 } else { 30 item.src = item.somethingElse; 31 } 32 }); 33 34 // Mouse was used (triggers only once) 35 pswp.listen('mouseUsed', function() { }); 36 37 38 // Opening zoom in animation starting 39 pswp.listen('initialZoomIn', function() { }); 40 41 // Opening zoom in animation finished 42 pswp.listen('initialZoomInEnd', function() { }); 43 44 // Closing zoom out animation started 45 pswp.listen('initialZoomOut', function() { }); 46 47 // Closing zoom out animation finished 48 pswp.listen('initialZoomOutEnd', function() { }); 49 50 51 // Allows overriding vertical margin for individual items 52 pswp.listen('parseVerticalMargin', function(item) { 53 // For example: 54 var gap = item.vGap; 55 56 gap.top = 50; // There will be 50px gap from top of viewport 57 gap.bottom = 100; // and 100px gap from the bottom 58 }) 59 60 // Gallery starts closing 61 pswp.listen('close', function() { }); 62 63 // Gallery unbinds events 64 // (triggers before closing animation) 65 pswp.listen('unbindEvents', function() { }); 66 67 // After gallery is closed and closing animation finished. 68 // Clean up your stuff here. 69 pswp.listen('destroy', function() { }); 70 71 // Called when the page scrolls. 72 // The callback is passed an offset with properties {x: number, y: number}. 73 // 74 // PhotoSwipe uses the offset to determine the top-left of the template, 75 // which by default is the top-left of the viewport. When using modal: false, 76 // you should listen to this event (before calling .init()) and modify the offset 77 // with the template's getBoundingClientRect(). 78 // 79 // Look at the "Implementing inline gallery display" FAQ section for more info. 80 pswp.listen('updateScrollOffset', function(_offset) { 81 var r = gallery.template.getBoundingClientRect(); 82 _offset.x += r.left; 83 _offset.y += r.top; 84 }); 85 86 // PhotoSwipe has a special event called pswpTap. 87 // It's dispatched using default JavaScript event model. 88 // So you can, for example, call stopPropagation on it. 89 // pswp.framework.bind - is a shorthand for addEventListener 90 pswp.framework.bind( pswp.scrollWrap /* bind on any element of gallery */, 'pswpTap', function(e) { 91 console.log('tap', e, e.detail); 92 // e.detail.origEvent // original event that finished tap (e.g. mouseup or touchend) 93 // e.detail.target // e.target of original event 94 // e.detail.releasePoint // object with x/y coordinates of tap 95 // e.detail.pointerType // mouse, touch, or pen 96 }); 97 98 // Allow to call preventDefault on down and up events 99 pswp.listen('preventDragEvent', function(e, isDown, preventObj) { 100 // e - original event 101 // isDown - true = drag start, false = drag release 102 103 // Line below will force e.preventDefault() on: 104 // touchstart/mousedown/pointerdown events 105 // as well as on: 106 // touchend/mouseup/pointerup events 107 preventObj.prevent = true; 108 }); 109 110 111 112 // Default UI events 113 // ------------------------- 114 115 // Share link clicked 116 pswp.listen('shareLinkClick', function(e, target) { 117 // e - original click event 118 // target - link that was clicked 119 120 // If `target` has `href` attribute and 121 // does not have `download` attribute - 122 // share modal window will popup 123 });
添加幻灯片动态
要添加,编辑或打开PhotoSwipe后删除幻灯片,你只需要修改的项目数组。例如,你可以把新的幻灯片对象到项目数组:
1 pswp.items.push({ 2 src: "path/to/image.jpg", 3 w:1200, 4 h:500 5 });
如果更改幻灯片是CURRENT,NEXT或PREVIOUS(你应该尽量避免) - 你需要调用方法,将更新的内容:
1 // sets a flag that slides should be updated 2 pswp.invalidateCurrItems(); 3 // updates the content of slides 4 pswp.updateSize(true);
否则,你不需要做任何事情。除非,也许,调用pswp.ui.update()如果你想默认用户界面的某些部分进行更新(例如,“1×的”计数器)。还要注意:
您不能将整个数组,你只能修改(例如用剪接删除元素)。
如果你想删除当前幻灯片 - 之前调用GOTO方法。
必须有至少一个滑动。
这种技术被用来服务响应图像。
原创文章请随便转载。愿和大家分享,并且一起进步。-- 江 coder