From b62b5d1967d990fe90a56c3d95004324429837dc Mon Sep 17 00:00:00 2001 From: gjTool <861366490@qq.com> Date: Fri, 1 Nov 2019 13:52:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dsvg=E6=A8=A1=E5=BC=8F?= =?UTF-8?q?=E6=A0=B7=E5=BC=8F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +- example/pdfh5-vue/package.json | 2 +- example/test/js/pdfh5.js | 1370 +++++++++++++++++++++++++++++++- js/pdfh5.js | 1370 +++++++++++++++++++++++++++++++- pdf.html | 3 +- 5 files changed, 2743 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 8fec937..5c417cc 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# pdfh5.js v1.2.20 +# pdfh5.js v1.2.21 [![npm version](https://img.shields.io/npm/v/pdfh5.svg)](https://www.npmjs.com/package/pdfh5) [![npm downloads](https://img.shields.io/npm/dt/pdfh5.svg)](https://www.npmjs.com/package/pdfh5) [![MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/gjTool/pdfh5/blob/master/LICENSE) [![GitHub issues](https://img.shields.io/github/issues/gjTool/pdfh5.svg)](https://github.com/gjTool/pdfh5/issues) [![GitHub stars](https://img.shields.io/github/stars/gjTool/pdfh5.svg?style=social)](https://github.com/gjTool/pdfh5/stargazers) [![GitHub forks](https://img.shields.io/github/forks/gjTool/pdfh5.svg?style=social)](https://github.com/gjTool/pdfh5/network/members) @@ -14,7 +14,7 @@ ## 更新信息 -- 2019.11.01 更新: 1. 修复svg模式下chrome等浏览器pdf只有一页时显示高度不正常。 +- 2019.11.01 更新: 1. 修复svg模式下chrome等浏览器显示高度不正常。 ### pdfh5在线预览 (建议使用谷歌浏览器F12手机模式打开预览) diff --git a/example/pdfh5-vue/package.json b/example/pdfh5-vue/package.json index c7f16c1..c9dacd6 100644 --- a/example/pdfh5-vue/package.json +++ b/example/pdfh5-vue/package.json @@ -10,7 +10,7 @@ "build": "node build/build.js" }, "dependencies": { - "pdfh5": "^1.2.20", + "pdfh5": "^1.2.21", "vue": "^2.5.2" }, "devDependencies": { diff --git a/example/test/js/pdfh5.js b/example/test/js/pdfh5.js index 1870207..3aece1d 100644 --- a/example/test/js/pdfh5.js +++ b/example/test/js/pdfh5.js @@ -1 +1,1369 @@ -!function(t,e){var i="1.2.20",n="2.1.266";console.log("The latest version and API of pdfh5 from: https://www.gjtool.cn (pdf.js: "+n+" , pdfh5.js: "+i+")"),"undefined"!=typeof require&&(void 0===t.$&&(t.$=require("./jquery-1.11.3.min.js")),t.pdfjsWorker=require("./pdf.worker.js"),t.pdfjsLib=require("./pdf.js"));var a=t.pdfjsLib,o=t.$,s=t.pdfjsWorker;"function"==typeof define&&define.amd?define(function(){return e(t,s,a,o,i)}):"undefined"!=typeof module&&module.exports?module.exports=e(t,s,a,o,i):t.Pdfh5=e(t,s,a,o,i)}("undefined"!=typeof window?window:this,function(t,e,i,n,a){"use strict";var o=function(e){var i=function(t,i,n){this.el=e(t),this.viewerContainer=n,this.zoomFactor=1,this.lastScale=1,this.offset={x:0,y:0},this.options=e.extend({},this.defaults,i),this.options.tapZoomFactor=isNaN(i.tapZoomFactor)?2:i.tapZoomFactor,this.options.zoomOutFactor=isNaN(i.zoomOutFactor)?1.2:i.zoomOutFactor,this.options.animationDuration=isNaN(i.animationDuration)?300:i.animationDuration,this.options.maxZoom=isNaN(i.maxZoom)?4:i.maxZoom,this.options.minZoom=isNaN(i.minZoom)?.8:i.minZoom,this.setupMarkup(),this.bindEvents(),this.update(),this.enable(),this.height=0,this.load=!1,this.direction=null,this.clientY=null,this.lastclientY=null},n=function(t,e){return t+e},a=function(t,e){return t>e-.01&&t1){var e=this.getTouches(t)[0];this.drag(e,this.lastDragPosition,t),this.offset=this.sanitizeOffset(this.offset),this.lastDragPosition=e}},handleDragEnd:function(){this.el.trigger(this.options.dragEndEventName),this.end()},handleZoomStart:function(t){this.el.trigger(this.options.zoomStartEventName),this.stopAnimation(),this.lastScale=1,this.nthZoom=0,this.lastZoomCenter=!1,this.hasInteraction=!0},handleZoom:function(t,e){var i=this.getTouchCenter(this.getTouches(t)),n=e/this.lastScale;this.lastScale=e,this.nthZoom+=1,this.nthZoom>3&&(this.scale(n,i),this.drag(i,this.lastZoomCenter)),this.lastZoomCenter=i},handleZoomEnd:function(){this.el.trigger(this.options.zoomEndEventName),this.end()},handleDoubleTap:function(t){var e=this.getTouches(t)[0],i=this.zoomFactor>1?1:this.options.tapZoomFactor,n=this.zoomFactor,a=function(t){this.scaleTo(n+t*(i-n),e)}.bind(this);this.hasInteraction||(n>i&&(e=this.getCurrentZoomCenter()),this.animate(this.options.animationDuration,a,this.swing),this.el.trigger(this.options.doubleTapEventName))},sanitizeOffset:function(t){var e=(this.zoomFactor-1)*this.getContainerX(),i=(this.zoomFactor-1)*this.getContainerY(),n=Math.max(e,0),a=Math.max(i,0),o=Math.min(e,0),s=Math.min(i,0),r=Math.min(Math.max(t.x,o),n),h=Math.min(Math.max(t.y,s),a);return{x:r,y:h}},scaleTo:function(t,e){this.scale(t/this.zoomFactor,e)},scale:function(t,e){t=this.scaleZoomFactor(t),this.addOffset({x:(t-1)*(e.x+this.offset.x),y:(t-1)*(e.y+this.offset.y)})},scaleZoomFactor:function(t){var e=this.zoomFactor;return this.zoomFactor*=t,this.zoomFactor=Math.min(this.options.maxZoom,Math.max(this.zoomFactor,this.options.minZoom)),this.zoomFactor/e},drag:function(t,e,i){e&&(this.options.lockDragAxis?Math.abs(t.x-e.x)>Math.abs(t.y-e.y)?this.addOffset({x:-(t.x-e.x),y:0}):this.addOffset({y:-(t.y-e.y),x:0}):(t.y-e.y<0?this.direction="down":t.y-e.y>10&&(this.direction="up"),this.addOffset({y:-(t.y-e.y),x:-(t.x-e.x)})))},getTouchCenter:function(t){return this.getVectorAvg(t)},getVectorAvg:function(t){return{x:t.map(function(t){return t.x}).reduce(n)/t.length,y:t.map(function(t){return t.y}).reduce(n)/t.length}},addOffset:function(t){this.offset={x:this.offset.x+t.x,y:this.offset.y+t.y}},sanitize:function(){this.zoomFactor=t?(e(1),n&&n(),this.update(),this.stopAnimation(),this.update()):(i&&(r=i(r)),e(r),this.update(),requestAnimationFrame(o))}}.bind(this);this.inAnimation=!0,requestAnimationFrame(o)},stopAnimation:function(){this.inAnimation=!1},swing:function(t){return-Math.cos(t*Math.PI)/2+.5},getContainerX:function(){return this.el[0]?this.el[0].offsetWidth:0},getContainerY:function(){return this.el[0].offsetHeight},setContainerY:function(t){return t=t.toFixed(2),this.container.height(t)},setupMarkup:function(){this.container=e('
'),this.el.before(this.container),this.container.append(this.el),this.container.css({position:"relative"}),this.el.css({"-webkit-transform-origin":"0% 0%","-moz-transform-origin":"0% 0%","-ms-transform-origin":"0% 0%","-o-transform-origin":"0% 0%","transform-origin":"0% 0%",position:"relative"})},end:function(){this.hasInteraction=!1,this.sanitize(),this.update()},bindEvents:function(){o(this.container.eq(0),this,this.viewerContainer),e(t).on("resize",this.update.bind(this)),e(this.el).find("img").on("load",this.update.bind(this))},update:function(){this.updatePlaned||(this.updatePlaned=!0,setTimeout(function(){this.updatePlaned=!1,this.updateAspectRatio();var t=this.getInitialZoomFactor()*this.zoomFactor,e=(-this.offset.x/t).toFixed(3),i=(-this.offset.y/t).toFixed(3);this.lastclientY=i;var n="scale3d("+t+", "+t+",1) translate3d("+e+"px,"+i+"px,0px)",a="scale("+t+", "+t+") translate("+e+"px,"+i+"px)";(function(){this.clone&&(this.clone.remove(),delete this.clone)}).bind(this);!this.options.use2d||this.hasInteraction||this.inAnimation?(this.is3d=!0,this.el.css({"-webkit-transform":n,"-o-transform":a,"-ms-transform":a,"-moz-transform":a,transform:n})):(this.el.css({"-webkit-transform":a,"-o-transform":a,"-ms-transform":a,"-moz-transform":a,transform:a}),this.is3d=!1),this.done&&this.done.call(this,t)}.bind(this),0))},enable:function(){this.enabled=!0},disable:function(){this.enabled=!1},destroy:function(){var t=this.el.clone(),e=this.container.parent();this.container.remove(),t.removeAttr("style"),e.append(t)}};var o=function(t,e,i){var n=null,a=0,o=null,s=null,r=null,h=null,c=0,l=0,d=function(t,i){if(n!==t){if(n&&!t)switch(n){case"zoom":e.handleZoomEnd(i);break;case"drag":e.handleDragEnd(i)}switch(t){case"zoom":e.handleZoomStart(i);break;case"drag":e.handleDragStart(i)}}n=t},f=function(t){2===a?d("zoom"):1===a&&e.canDrag()?d("drag",t):d(null,t)},u=function(t){return Array.prototype.slice.call(t).map(function(t){return{x:t.pageX,y:t.pageY}})},m=function(t,e){var i,n;return i=t.x-e.x,n=t.y-e.y,Math.sqrt(i*i+n*n)},p=function(t,e){var i=m(t[0],t[1]),n=m(e[0],e[1]);return n/i},g=function(t){t.stopPropagation(),t.preventDefault()},v=function(t){var i=(new Date).getTime(),s=t.changedTouches[0].pageY,h=w.scrollTop||0;if(a>1&&(o=null,r=null,g(t)),i-o<300&&Math.abs(s-r)<10&&Math.abs(l-h)<10)switch(g(t),e.handleDoubleTap(t),n){case"zoom":e.handleZoomEnd(t);break;case"drag":e.handleDragEnd(t)}1===a&&(o=i,r=s,l=h)},y=!0;if(i)var w=i[0];w&&(w.addEventListener("touchstart",function(t){e.enabled&&(y=!0,a=t.touches.length,v(t),h=t.changedTouches[0].clientY,a>1&&g(t))}),w.addEventListener("touchmove",function(t){if(e.enabled){if(c=t.changedTouches[0].clientY,y)f(t),s=u(t.touches);else{switch(n){case"zoom":e.handleZoom(t,p(s,u(t.touches)));break;case"drag":e.handleDrag(t)}n&&e.update(c)}a>1&&g(t),y=!1}}),w.addEventListener("touchend",function(t){e.enabled&&(a=t.touches.length,a>1&&g(t),f(t))}))};return i},s=o(n),r=function(t,e){this.version=a,this.container=n(t),this.thePDF=null,this.totalNum=null,this.pages=null,this.initTime=0,this.scale=1.3,this.currentNum=1,this.loadedCount=0,this.endTime=0,this.pinchZoom=null,this.timer=null,this.docWidth=document.documentElement.clientWidth,this.winWidth=n(window).width(),this.eventType={},this.cache={},this.cacheNum=1,this.options=e,this.resizeEvent=!1,this.init(e)};return r.prototype={init:function(e){function i(e){var i=new RegExp("(^|&)"+e+"=([^&]*)(&|$)"),n=t.location.search.substr(1).match(i);return null!=n?decodeURIComponent(n[2]):""}this.container[0].pdfLoaded&&this.destroy(),this.container[0].pdfLoaded=!1,this.container.addClass("pdfjs");var a=this;this.initTime=(new Date).getTime(),setTimeout(function(){var t=a.eventType.scroll;if(t&&t instanceof Array)for(var e=0;e=150?e.backTop&&a.backTop.show():e.backTop&&a.backTop.fadeOut(200),a.viewerContainer&&(a.pages=a.viewerContainer.find(".pageContainer")),clearTimeout(a.timer),e.pageNum&&a.pageNum&&a.pageNum.show();var i=h;if(a.pages&&a.pages.each(function(t,n){var o=n.getBoundingClientRect().top,s=n.getBoundingClientRect().bottom;o<=c&&s>c&&(e.pageNum&&a.pageNow.text(t+1),a.currentNum=t+1),o<=i&&s>i&&(a.cacheNum=t+1)}),t+a.container.height()>=a.viewer[0].offsetHeight&&a.pageNow.text(a.totalNum),0===t&&a.pageNow.text(1),a.timer=setTimeout(function(){e.pageNum&&a.pageNum&&a.pageNum.fadeOut(200)},1500),e.lazy){var n=Math.floor(100/a.totalNum).toFixed(2);if(!a.cache[a.cacheNum+""].loaded){var o=a.cache[a.cacheNum+""].page,s=a.cache[a.cacheNum+""].container,l=a.cacheNum;a.cache[l+""].loaded=!0;var d=a.cache[l+""].scaledViewport;"svg"===e.renderType?a.renderSvg(o,d,l,n,s,e):a.renderCanvas(o,d,l,n,s,e)}if(a.cache[a.totalNum-1+""].loaded&&!a.cache[a.totalNum+""].loaded){var o=a.cache[a.totalNum+""].page,s=a.cache[a.totalNum+""].container,l=a.totalNum;a.cache[l+""].loaded=!0;var d=a.cache[l+""].scaledViewport;"svg"===e.renderType?a.renderSvg(o,d,l,n,s,e):a.renderCanvas(o,d,l,n,s,e)}}var f=a.eventType.scroll;if(f&&f instanceof Array)for(var u=0;ud&&t.lazy))return"svg"===t.renderType?a.renderSvg(i,r,e,h,c,t,o):a.renderCanvas(i,r,e,h,c,t)})}.bind(null,o))}).catch(function(t){a.loading.hide();var e=(new Date).getTime();a.endTime=e-a.initTime;var i=a.eventType.complete;if(i&&i instanceof Array)for(var n=0;n expected - 0.01 && value < expected + 0.01; + }; + + PinchZoom.prototype = { + defaults: { + tapZoomFactor: 2, + zoomOutFactor: 1.2, + animationDuration: 300, + maxZoom: 4, + minZoom: 0.8, + lockDragAxis: false, + use2d: true, + zoomStartEventName: 'pz_zoomstart', + zoomEndEventName: 'pz_zoomend', + dragStartEventName: 'pz_dragstart', + dragEndEventName: 'pz_dragend', + doubleTapEventName: 'pz_doubletap' + }, + handleDragStart: function (event) { + this.el.trigger(this.options.dragStartEventName); + this.stopAnimation(); + this.lastDragPosition = false; + this.hasInteraction = true; + this.handleDrag(event); + }, + handleDrag: function (event) { + + if (this.zoomFactor > 1.0) { + var touch = this.getTouches(event)[0]; + this.drag(touch, this.lastDragPosition, event); + this.offset = this.sanitizeOffset(this.offset); + this.lastDragPosition = touch; + } + }, + + handleDragEnd: function () { + this.el.trigger(this.options.dragEndEventName); + this.end(); + }, + handleZoomStart: function (event) { + this.el.trigger(this.options.zoomStartEventName); + this.stopAnimation(); + this.lastScale = 1; + this.nthZoom = 0; + this.lastZoomCenter = false; + this.hasInteraction = true; + }, + handleZoom: function (event, newScale) { + var touchCenter = this.getTouchCenter(this.getTouches(event)), + scale = newScale / this.lastScale; + this.lastScale = newScale; + this.nthZoom += 1; + if (this.nthZoom > 3) { + + this.scale(scale, touchCenter); + this.drag(touchCenter, this.lastZoomCenter); + } + this.lastZoomCenter = touchCenter; + }, + + handleZoomEnd: function () { + this.el.trigger(this.options.zoomEndEventName); + this.end(); + }, + handleDoubleTap: function (event) { + var center = this.getTouches(event)[0], + zoomFactor = this.zoomFactor > 1 ? 1 : this.options.tapZoomFactor, + startZoomFactor = this.zoomFactor, + updateProgress = (function (progress) { + this.scaleTo(startZoomFactor + progress * (zoomFactor - startZoomFactor), center); + }).bind(this); + + if (this.hasInteraction) { + return; + } + if (startZoomFactor > zoomFactor) { + center = this.getCurrentZoomCenter(); + } + + this.animate(this.options.animationDuration, updateProgress, this.swing); + this.el.trigger(this.options.doubleTapEventName); + }, + sanitizeOffset: function (offset) { + var maxX = (this.zoomFactor - 1) * this.getContainerX(), + maxY = (this.zoomFactor - 1) * this.getContainerY(), + maxOffsetX = Math.max(maxX, 0), + maxOffsetY = Math.max(maxY, 0), + minOffsetX = Math.min(maxX, 0), + minOffsetY = Math.min(maxY, 0); + + var x = Math.min(Math.max(offset.x, minOffsetX), maxOffsetX), + y = Math.min(Math.max(offset.y, minOffsetY), maxOffsetY); + + + return { + x: x, + y: y + }; + }, + scaleTo: function (zoomFactor, center) { + this.scale(zoomFactor / this.zoomFactor, center); + }, + scale: function (scale, center) { + scale = this.scaleZoomFactor(scale); + this.addOffset({ + x: (scale - 1) * (center.x + this.offset.x), + y: (scale - 1) * (center.y + this.offset.y) + }); + }, + scaleZoomFactor: function (scale) { + var originalZoomFactor = this.zoomFactor; + this.zoomFactor *= scale; + this.zoomFactor = Math.min(this.options.maxZoom, Math.max(this.zoomFactor, this.options.minZoom)); + return this.zoomFactor / originalZoomFactor; + }, + drag: function (center, lastCenter, event) { + if (lastCenter) { + if (this.options.lockDragAxis) { + if (Math.abs(center.x - lastCenter.x) > Math.abs(center.y - lastCenter.y)) { + this.addOffset({ + x: -(center.x - lastCenter.x), + y: 0 + }); + } else { + this.addOffset({ + y: -(center.y - lastCenter.y), + x: 0 + }); + } + } else { + if (center.y - lastCenter.y < 0) { + this.direction = "down"; + } else if (center.y - lastCenter.y > 10) { + this.direction = "up"; + } + this.addOffset({ + y: -(center.y - lastCenter.y), + x: -(center.x - lastCenter.x) + }); + } + } + }, + getTouchCenter: function (touches) { + return this.getVectorAvg(touches); + }, + getVectorAvg: function (vectors) { + return { + x: vectors.map(function (v) { + return v.x; + }).reduce(sum) / vectors.length, + y: vectors.map(function (v) { + return v.y; + }).reduce(sum) / vectors.length + }; + }, + addOffset: function (offset) { + this.offset = { + x: this.offset.x + offset.x, + y: this.offset.y + offset.y + }; + }, + + sanitize: function () { + if (this.zoomFactor < this.options.zoomOutFactor) { + this.zoomOutAnimation(); + } else if (this.isInsaneOffset(this.offset)) { + this.sanitizeOffsetAnimation(); + } + }, + isInsaneOffset: function (offset) { + var sanitizedOffset = this.sanitizeOffset(offset); + return sanitizedOffset.x !== offset.x || + sanitizedOffset.y !== offset.y; + }, + sanitizeOffsetAnimation: function () { + var targetOffset = this.sanitizeOffset(this.offset), + startOffset = { + x: this.offset.x, + y: this.offset.y + }, + updateProgress = (function (progress) { + this.offset.x = startOffset.x + progress * (targetOffset.x - startOffset.x); + this.offset.y = startOffset.y + progress * (targetOffset.y - startOffset.y); + this.update(); + }).bind(this); + + this.animate( + this.options.animationDuration, + updateProgress, + this.swing + ); + }, + zoomOutAnimation: function () { + var startZoomFactor = this.zoomFactor, + zoomFactor = 1, + center = this.getCurrentZoomCenter(), + updateProgress = (function (progress) { + this.scaleTo(startZoomFactor + progress * (zoomFactor - startZoomFactor), center); + }).bind(this); + + this.animate( + this.options.animationDuration, + updateProgress, + this.swing + ); + }, + updateAspectRatio: function () { + this.setContainerY(this.getContainerX() / this.getAspectRatio()); + }, + getInitialZoomFactor: function () { + if (this.container[0] && this.el[0]) { + return this.container[0].offsetWidth / this.el[0].offsetWidth; + } else { + return 0 + } + }, + getAspectRatio: function () { + if (this.el[0]) { + var offsetHeight = this.el[0].offsetHeight; + return this.container[0].offsetWidth / offsetHeight; + } else { + return 0 + } + + }, + getCurrentZoomCenter: function () { + var length = this.container[0].offsetWidth * this.zoomFactor, + offsetLeft = this.offset.x, + offsetRight = length - offsetLeft - this.container[0].offsetWidth, + widthOffsetRatio = offsetLeft / offsetRight, + centerX = widthOffsetRatio * this.container[0].offsetWidth / (widthOffsetRatio + 1), + + height = this.container[0].offsetHeight * this.zoomFactor, + offsetTop = this.offset.y, + offsetBottom = height - offsetTop - this.container[0].offsetHeight, + heightOffsetRatio = offsetTop / offsetBottom, + centerY = heightOffsetRatio * this.container[0].offsetHeight / (heightOffsetRatio + 1); + + if (offsetRight === 0) { + centerX = this.container[0].offsetWidth; + } + if (offsetBottom === 0) { + centerY = this.container[0].offsetHeight; + } + + return { + x: centerX, + y: centerY + }; + }, + + canDrag: function () { + return !isCloseTo(this.zoomFactor, 1); + }, + + getTouches: function (event) { + var position = this.container.offset(); + return Array.prototype.slice.call(event.touches).map(function (touch) { + return { + x: touch.pageX - position.left, + y: touch.pageY - position.top + }; + }); + }, + animate: function (duration, framefn, timefn, callback) { + var startTime = new Date().getTime(), + renderFrame = (function () { + if (!this.inAnimation) { + return; + } + var frameTime = new Date().getTime() - startTime, + progress = frameTime / duration; + if (frameTime >= duration) { + framefn(1); + if (callback) { + callback(); + } + this.update(); + this.stopAnimation(); + this.update(); + } else { + if (timefn) { + progress = timefn(progress); + } + framefn(progress); + this.update(); + requestAnimationFrame(renderFrame); + } + }).bind(this); + this.inAnimation = true; + requestAnimationFrame(renderFrame); + }, + stopAnimation: function () { + this.inAnimation = false; + + }, + swing: function (p) { + return -Math.cos(p * Math.PI) / 2 + 0.5; + }, + + getContainerX: function () { + if (this.el[0]) { + return this.el[0].offsetWidth; + } else { + return 0; + } + }, + + getContainerY: function () { + return this.el[0].offsetHeight; + }, + setContainerY: function (y) { + y = y.toFixed(2); + return this.container.height(y); + }, + setupMarkup: function () { + this.container = $('
'); + this.el.before(this.container); + this.container.append(this.el); + + this.container.css({ + 'position': 'relative', + }); + + this.el.css({ + '-webkit-transform-origin': '0% 0%', + '-moz-transform-origin': '0% 0%', + '-ms-transform-origin': '0% 0%', + '-o-transform-origin': '0% 0%', + 'transform-origin': '0% 0%', + 'position': 'relative' + }); + + }, + + end: function () { + this.hasInteraction = false; + this.sanitize(); + this.update(); + + }, + bindEvents: function () { + detectGestures(this.container.eq(0), this, this.viewerContainer); + $(g).on('resize', this.update.bind(this)); + $(this.el).find('img').on('load', this.update.bind(this)); + + }, + update: function () { + + if (this.updatePlaned) { + return; + } + this.updatePlaned = true; + setTimeout((function () { + this.updatePlaned = false; + this.updateAspectRatio(); + var zoomFactor = this.getInitialZoomFactor() * this.zoomFactor, + offsetX = (-this.offset.x / zoomFactor).toFixed(3), + offsetY = (-this.offset.y / zoomFactor).toFixed(3); + this.lastclientY = offsetY; + + var transform3d = 'scale3d(' + zoomFactor + ', ' + zoomFactor + ',1) ' + + 'translate3d(' + offsetX + 'px,' + offsetY + 'px,0px)', + transform2d = 'scale(' + zoomFactor + ', ' + zoomFactor + ') ' + + 'translate(' + offsetX + 'px,' + offsetY + 'px)', + removeClone = (function () { + if (this.clone) { + this.clone.remove(); + delete this.clone; + } + }).bind(this); + if (!this.options.use2d || this.hasInteraction || this.inAnimation) { + this.is3d = true; + this.el.css({ + '-webkit-transform': transform3d, + '-o-transform': transform2d, + '-ms-transform': transform2d, + '-moz-transform': transform2d, + 'transform': transform3d + }); + } else { + this.el.css({ + '-webkit-transform': transform2d, + '-o-transform': transform2d, + '-ms-transform': transform2d, + '-moz-transform': transform2d, + 'transform': transform2d + }); + this.is3d = false; + } + this.done && this.done.call(this, zoomFactor) + }).bind(this), 0); + }, + enable: function () { + this.enabled = true; + }, + disable: function () { + this.enabled = false; + }, + destroy: function () { + var dom = this.el.clone(); + var p = this.container.parent(); + this.container.remove(); + dom.removeAttr('style'); + p.append(dom); + } + }; + + var detectGestures = function (el, target, viewerContainer) { + var interaction = null, + fingers = 0, + lastTouchStart = null, + startTouches = null, + lastTouchY = null, + clientY = null, + lastclientY = 0, + lastTop = 0, + setInteraction = function (newInteraction, event) { + if (interaction !== newInteraction) { + + if (interaction && !newInteraction) { + switch (interaction) { + case "zoom": + target.handleZoomEnd(event); + break; + case 'drag': + target.handleDragEnd(event); + break; + } + } + + switch (newInteraction) { + case 'zoom': + target.handleZoomStart(event); + break; + case 'drag': + target.handleDragStart(event); + break; + } + } + interaction = newInteraction; + }, + + updateInteraction = function (event) { + if (fingers === 2) { + setInteraction('zoom'); + } else if (fingers === 1 && target.canDrag()) { + setInteraction('drag', event); + } else { + setInteraction(null, event); + } + }, + + targetTouches = function (touches) { + return Array.prototype.slice.call(touches).map(function (touch) { + return { + x: touch.pageX, + y: touch.pageY + }; + }); + }, + + getDistance = function (a, b) { + var x, y; + x = a.x - b.x; + y = a.y - b.y; + return Math.sqrt(x * x + y * y); + }, + + calculateScale = function (startTouches, endTouches) { + var startDistance = getDistance(startTouches[0], startTouches[1]), + endDistance = getDistance(endTouches[0], endTouches[1]); + return endDistance / startDistance; + }, + + cancelEvent = function (event) { + event.stopPropagation(); + event.preventDefault(); + }, + + detectDoubleTap = function (event) { + var time = (new Date()).getTime(); + var pageY = event.changedTouches[0].pageY; + var top = parentNode.scrollTop || 0; + if (fingers > 1) { + lastTouchStart = null; + lastTouchY = null; + cancelEvent(event); + } + + if (time - lastTouchStart < 300 && Math.abs(pageY - lastTouchY) < 10 && Math.abs(lastTop - top) < 10) { + cancelEvent(event); + target.handleDoubleTap(event); + switch (interaction) { + case "zoom": + target.handleZoomEnd(event); + break; + case 'drag': + target.handleDragEnd(event); + break; + } + } + + if (fingers === 1) { + lastTouchStart = time; + lastTouchY = pageY; + lastTop = top; + } + }, + firstMove = true; + if (viewerContainer) { + var parentNode = viewerContainer[0]; + } + if (parentNode) { + parentNode.addEventListener('touchstart', function (event) { + if (target.enabled) { + firstMove = true; + fingers = event.touches.length; + detectDoubleTap(event); + clientY = event.changedTouches[0].clientY; + if (fingers > 1) { + cancelEvent(event); + } + } + }); + + parentNode.addEventListener('touchmove', function (event) { + if (target.enabled) { + lastclientY = event.changedTouches[0].clientY; + if (firstMove) { + updateInteraction(event); + startTouches = targetTouches(event.touches); + } else { + switch (interaction) { + case 'zoom': + target.handleZoom(event, calculateScale(startTouches, targetTouches(event.touches))); + break; + case 'drag': + target.handleDrag(event); + break; + } + if (interaction) { + target.update(lastclientY); + } + } + if (fingers > 1) { + cancelEvent(event); + } + firstMove = false; + } + }); + + parentNode.addEventListener('touchend', function (event) { + if (target.enabled) { + fingers = event.touches.length; + if (fingers > 1) { + cancelEvent(event); + } + updateInteraction(event); + } + }); + } + + }; + return PinchZoom; + }; + var PinchZoom = definePinchZoom($); + var Pdfh5 = function (dom, options) { + this.version = version; + this.container = $(dom); + this.thePDF = null; + this.totalNum = null; + this.pages = null; + this.initTime = 0; + this.scale = 1.3; + this.currentNum = 1; + this.loadedCount = 0; + this.endTime = 0; + this.pinchZoom = null; + this.timer = null; + this.docWidth = document.documentElement.clientWidth; + this.winWidth = $(window).width(); + this.eventType = {}; + this.cache = {}; + this.cacheNum = 1; + this.options = options; + this.resizeEvent = false; + this.init(options); + }; + Pdfh5.prototype = { + init: function (options) { + if (this.container[0].pdfLoaded) { + this.destroy(); + } + this.container[0].pdfLoaded = false; + this.container.addClass("pdfjs") + var self = this; + this.initTime = new Date().getTime(); + setTimeout(function () { + var arr1 = self.eventType["scroll"]; + if (arr1 && arr1 instanceof Array) { + for (var i = 0; i < arr1.length; i++) { + arr1[i] && arr1[i].call(self, self.initTime) + } + } + }, 0) + options = options ? options : {}; + options.pdfurl = options.pdfurl ? options.pdfurl : null; + options.data = options.data ? options.data : null; + options.scale = options.scale ? options.scale : self.scale; + options.zoomEnable = options.zoomEnable === false ? false : true; + options.scrollEnable = options.scrollEnable === false ? false : true; + options.loadingBar = options.loadingBar === false ? false : true; + options.pageNum = options.pageNum === false ? false : true; + options.backTop = options.backTop === false ? false : true; + options.URIenable = options.URIenable === false ? false : true; + options.fullscreen = options.fullscreen === false ? false : true; + options.lazy = options.lazy === true ? true : false; + options.renderType = options.renderType === "canvas" ? "canvas" : "svg"; + options.resize = options.resize === false ? false : true; + if (options.renderType === "canvas") { + options.scale = 2; + } + options.type = options.type === "ajax" ? "ajax" : "fetch"; + var html = '
' + + '
' + + '
' + + '
' + + '
' + + '
' + + '
' + + '
' + + '
' + + ' 1/' + + '1' + + '
' + + '
' + + '
' + + '
' + + '
'; + if (!this.container.find('.pageNum')[0]) { + this.container.append(html); + } + var viewer = document.createElement("div"); + viewer.className = 'pdfViewer'; + var viewerContainer = document.createElement("div"); + viewerContainer.className = 'viewerContainer'; + viewerContainer.appendChild(viewer); + this.container.append(viewerContainer); + this.viewer = $(viewer); + this.viewerContainer = $(viewerContainer); + this.pageNum = this.container.find('.pageNum'); + this.pageNow = this.pageNum.find('.pageNow'); + this.pageTotal = this.pageNum.find('.pageTotal'); + this.loadingBar = this.container.find('.loadingBar'); + this.progress = this.loadingBar.find('.progress'); + this.progressDom = this.progress[0]; + this.backTop = this.container.find('.backTop'); + this.loading = this.container.find('.loading'); + if (!options.loadingBar) { + this.loadingBar.hide() + } + var containerH = this.container.height(), + height = containerH * (1 / 3); + + if (!options.scrollEnable) { + this.viewerContainer.css({ + "overflow": "hidden" + }) + } else { + this.viewerContainer.css({ + "overflow": "auto" + }) + } + viewerContainer.addEventListener('scroll', function () { + var scrollTop = viewerContainer.scrollTop; + if (scrollTop >= 150) { + if (options.backTop) { + self.backTop.show(); + } + } else { + if (options.backTop) { + self.backTop.fadeOut(200); + } + } + if (self.viewerContainer) { + self.pages = self.viewerContainer.find('.pageContainer'); + } + clearTimeout(self.timer); + if (options.pageNum && self.pageNum) { + self.pageNum.show(); + } + var h = containerH; + if (self.pages) { + self.pages.each(function (index, obj) { + var top = obj.getBoundingClientRect().top; + var bottom = obj.getBoundingClientRect().bottom; + if (top <= height && bottom > height) { + if (options.pageNum) { + self.pageNow.text(index + 1) + } + self.currentNum = index + 1; + } + if (top <= h && bottom > h) { + self.cacheNum = index + 1; + } + }) + } + if (scrollTop + self.container.height() >= self.viewer[0].offsetHeight) { + self.pageNow.text(self.totalNum) + } + if (scrollTop === 0) { + self.pageNow.text(1) + } + self.timer = setTimeout(function () { + if (options.pageNum && self.pageNum) { + self.pageNum.fadeOut(200); + } + }, 1500) + if (options.lazy) { + var num = Math.floor(100 / self.totalNum).toFixed(2); + if (self.cache[self.cacheNum + ""] && !self.cache[self.cacheNum + ""].loaded) { + var page = self.cache[self.cacheNum + ""].page; + var container = self.cache[self.cacheNum + ""].container; + var pageNum = self.cacheNum; + self.cache[pageNum + ""].loaded = true; + var scaledViewport = self.cache[pageNum + ""].scaledViewport; + if (options.renderType === "svg") { + self.renderSvg(page, scaledViewport, pageNum, num, container, options) + } else { + self.renderCanvas(page, scaledViewport, pageNum, num, container, options) + } + } + if (self.cache[(self.totalNum - 1) + ""] && self.cache[(self.totalNum - 1) + ""].loaded && !self.cache[self.totalNum + ""].loaded) { + var page = self.cache[self.totalNum + ""].page; + var container = self.cache[self.totalNum + ""].container; + var pageNum = self.totalNum; + self.cache[pageNum + ""].loaded = true; + var scaledViewport = self.cache[pageNum + ""].scaledViewport; + if (options.renderType === "svg") { + self.renderSvg(page, scaledViewport, pageNum, num, container, options) + } else { + self.renderCanvas(page, scaledViewport, pageNum, num, container, options) + } + } + } + var arr1 = self.eventType["scroll"]; + if (arr1 && arr1 instanceof Array) { + for (var i = 0; i < arr1.length; i++) { + arr1[i] && arr1[i].call(self, scrollTop) + } + } + }) + this.backTop.on('click tap', function () { + var mart = self.viewer.css('transform'); + var arr = mart.replace(/[a-z\(\)\s]/g, '').split(','); + var s1 = arr[0]; + var s2 = arr[3]; + var x = arr[4] / 2; + var left = self.viewer[0].getBoundingClientRect().left; + if (left <= -self.docWidth * 2) { + x = -self.docWidth / 2 + } + self.viewer.css({ + transform: 'scale(' + s1 + ', ' + s2 + ') translate(' + x + 'px, 0px)' + }) + if (self.pinchZoom) { + self.pinchZoom.offset.y = 0; + self.pinchZoom.lastclientY = 0; + } + self.viewerContainer.animate({ + scrollTop: 0 + }, 300) + var arr1 = self.eventType["backTop"]; + if (arr1 && arr1 instanceof Array) { + for (var i = 0; i < arr1.length; i++) { + arr1[i] && arr1[i].call(self) + } + } + }) + //获取url带的参数地址 + function GetQueryString(name) { + var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); + var r = g.location.search.substr(1).match(reg); + if (r != null) return decodeURIComponent(r[2]); + return ""; + } + var pdfurl = GetQueryString("file"), url = ""; + if (pdfurl && options.URIenable) { + url = pdfurl + } else if (options.pdfurl) { + url = options.pdfurl + } + if (url) { + if (options.type === "ajax") { + $.ajax({ + type: "get", + mimeType: 'text/plain; charset=x-user-defined', + cache: false, + url: url, + success: function (data) { + var rawLength = data.length; + // var array = new Uint8Array(new ArrayBuffer(rawLength)); + // for (i = 0; i < rawLength; i++) { + // array[i] = data.charCodeAt(i) & 0xff; + // } + var array = []; + for (i = 0; i < rawLength; i++) { + array.push(data.charCodeAt(i) & 0xff); + } + self.renderPdf(options, { + data: array + }) + }, + error: function (err) { + self.loading.hide() + var time = new Date().getTime(); + self.endTime = time - self.initTime; + var arr1 = self.eventType["complete"]; + if (arr1 && arr1 instanceof Array) { + for (var i = 0; i < arr1.length; i++) { + arr1[i] && arr1[i].call(self, "error", err.statusText, self.endTime) + } + } + var arr2 = self.eventType["error"]; + if (arr2 && arr2 instanceof Array) { + for (var i = 0; i < arr2.length; i++) { + arr2[i] && arr2[i].call(self, err.statusText, self.endTime) + } + } + throw Error(err.statusText) + } + }); + } else { + self.renderPdf(options, { + url: url + }) + } + } else if (options.data) { + var data = options.data; + var rawLength = data.length; + if (typeof data === "string" && data != "") { + var array = []; + for (i = 0; i < rawLength; i++) { + array.push(data.charCodeAt(i) & 0xff); + } + self.renderPdf(options, { + data: array + }) + } else if (typeof data === "object") { + if (data[0] === 0) { + var time = new Date().getTime(); + self.endTime = time - self.initTime; + var arr1 = self.eventType["complete"]; + if (arr1 && arr1 instanceof Array) { + for (var i = 0; i < arr1.length; i++) { + arr1[i] && arr1[i].call(self, "error", "options.data is empty Array", self.endTime) + } + } + var arr2 = self.eventType["error"]; + if (arr2 && arr2 instanceof Array) { + for (var i = 0; i < arr2.length; i++) { + arr2[i] && arr2[i].call(self, "options.data is empty Array", self.endTime) + } + } + throw Error("options.data is empty Array") + } else { + self.renderPdf(options, { + data: data + }) + } + } + + } else { + var time = new Date().getTime(); + self.endTime = time - self.initTime; + var arr1 = self.eventType["complete"]; + if (arr1 && arr1 instanceof Array) { + for (var i = 0; i < arr1.length; i++) { + arr1[i] && arr1[i].call(self, "error", "Expect options.pdfurl or options.data!", self.endTime) + } + } + var arr2 = self.eventType["error"]; + if (arr2 && arr2 instanceof Array) { + for (var i = 0; i < arr2.length; i++) { + arr2[i] && arr2[i].call(self, "Expect options.pdfurl or options.data!", self.endTime) + } + } + throw Error("Expect options.pdfurl or options.data!") + } + }, + renderPdf: function (options, obj) { + this.container[0].pdfLoaded = true; + var self = this; + if (options.cMapUrl) { + obj.cMapUrl = options.cMapUrl; + } else { + obj.cMapUrl = './js/cmaps/'; + } + obj.cMapPacked = true; + self.loadingBar.show(); + self.progress.css({ + width: "3%" + }) + pdfjsLib.getDocument(obj).then(function (pdf) { + self.loading.hide() + self.thePDF = pdf; + self.totalNum = pdf.numPages; + self.pageTotal.text(self.totalNum) + var arr1 = self.eventType["ready"]; + if (arr1 && arr1 instanceof Array) { + for (var i = 0; i < arr1.length; i++) { + arr1[i] && arr1[i].call(self) + } + } + self.pinchZoom = new PinchZoom(self.viewer, { + tapZoomFactor: options.tapZoomFactor, + zoomOutFactor: options.zoomOutFactor, + animationDuration: options.animationDuration, + maxZoom: options.maxZoom, + minZoom: options.minZoom + }, self.viewerContainer); + self.pinchZoom.done = function (scale) { + if (scale == 1) { + if (self.viewerContainer) { + self.viewerContainer.css({ + '-webkit-overflow-scrolling': 'touch' + }) + } + + } else { + if (self.viewerContainer) { + self.viewerContainer.css({ + '-webkit-overflow-scrolling': 'auto' + }) + } + } + var arr1 = self.eventType["zoom"]; + if (arr1 && arr1 instanceof Array) { + for (var i = 0; i < arr1.length; i++) { + arr1[i] && arr1[i].call(self, scale) + } + } + } + if (options.zoomEnable) { + self.pinchZoom.enable() + } else { + self.pinchZoom.disable() + } + var promise = Promise.resolve(); + var num = Math.floor(100 / self.totalNum).toFixed(2); + for (var i = 1; i <= self.totalNum; i++) { + self.cache[i + ""] = { + page: null, + loaded: false, + container: null, + scaledViewport: null + }; + promise = promise.then(function (pageNum) { + return self.thePDF.getPage(pageNum).then(function (page) { + self.cache[pageNum + ""].page = page; + var viewport = page.getViewport(options.scale); + var scale = (self.docWidth / viewport.width).toFixed(2) + var scaledViewport = page.getViewport(parseFloat(scale)) + var container = document.createElement('div'); + container.id = 'pageContainer' + pageNum; + container.className = 'pageContainer'; + container.setAttribute('name', 'page=' + pageNum); + container.setAttribute('title', 'Page ' + pageNum); + var loadEffect = document.createElement('div'); + loadEffect.className = 'loadEffect'; + container.appendChild(loadEffect); + self.viewer[0].appendChild(container); + if (window.ActiveXObject || "ActiveXObject" in window) { + $(container).css({ + 'width': viewport.width + 'px', + "height": viewport.height + 'px' + }).attr("data-scale", viewport.width / viewport.height) + } else { + $(container).css({ + 'max-width': viewport.width, + "max-height": viewport.height, + "min-height": $(container).width() / (viewport.viewBox[2]/ viewport.viewBox[3]) + 'px' + }).attr("data-scale", viewport.width / viewport.height) + } + self.cache[pageNum + ""].container = container; + self.cache[pageNum + ""].scaledViewport = scaledViewport; + var sum = 0, containerH = self.container.height(); + self.pages = self.viewerContainer.find('.pageContainer'); + if (options.resize) { + self.resize() + } + if (self.pages && options.lazy) { + self.pages.each(function (index, obj) { + var top = obj.offsetTop; + if (top <= containerH) { + sum = index + 1; + self.cache[sum + ""].loaded = true; + } + }) + } + + if (pageNum > sum && options.lazy) { + return + } + if (options.renderType === "svg") { + return self.renderSvg(page, scaledViewport, pageNum, num, container, options, viewport) + } + return self.renderCanvas(page, scaledViewport, pageNum, num, container, options) + }); + }.bind(null, i)); + } + + }).catch(function (err) { + self.loading.hide(); + var time = new Date().getTime(); + self.endTime = time - self.initTime; + var arr1 = self.eventType["complete"]; + if (arr1 && arr1 instanceof Array) { + for (var i = 0; i < arr1.length; i++) { + arr1[i] && arr1[i].call(self, "error", err.message, self.endTime) + } + } + var arr2 = self.eventType["error"]; + if (arr2 && arr2 instanceof Array) { + for (var i = 0; i < arr2.length; i++) { + arr2[i] && arr2[i].call(self, err.message, self.endTime) + } + } + }) + }, + renderSvg: function (page, scaledViewport, pageNum, num, container, options, viewport) { + var self = this; + + return page.getOperatorList().then(function (opList) { + var svgGfx = new pdfjsLib.SVGGraphics(page.commonObjs, page.objs); + return svgGfx.getSVG(opList, scaledViewport).then(function (svg) { + self.loadedCount++; + container.children[0].style.display = "none"; + container.appendChild(svg); + svg.style.width = "100%"; + svg.style.height = "100%"; + self.progress.css({ + width: num * self.loadedCount + "%" + }) + if (window.ActiveXObject || "ActiveXObject" in window) { + + } else { + $(container).css({ + // "min-height": svg.getBBox().height + 'px' + }) + } + var time = new Date().getTime(); + var arr1 = self.eventType["render"]; + if (arr1 && arr1 instanceof Array) { + for (var i = 0; i < arr1.length; i++) { + arr1[i] && arr1[i].call(self, pageNum, time - self.initTime, container) + } + } + if (self.loadedCount === self.totalNum) { + self.finalRender(options) + } + }); + }); + }, + renderCanvas: function (page, viewport, pageNum, num, container, options) { + var self = this; + var viewport = page.getViewport(options.scale); + var scale = (self.docWidth / viewport.width).toFixed(2) + var canvas = document.createElement("canvas"); + var obj2 = { + 'Cheight': viewport.height * scale, + 'width': viewport.width, + 'height': viewport.height, + 'canvas': canvas, + 'index': self.loadedCount + } + var context = canvas.getContext('2d'); + canvas.height = viewport.height; + canvas.width = viewport.width; + + self.progress.css({ + width: num * self.loadedCount + "%" + }) + obj2.src = obj2.canvas.toDataURL("image/jpeg"); + //在canvas上绘制 + return page.render({ + canvasContext: context, + viewport: viewport + }).then(function () { + self.loadedCount++; + var img = new Image(); + var time = new Date().getTime(); + var time2 = 0; + if (self.renderTime == 0) { + time2 = time - self.startTime + } else { + time2 = time - self.renderTime + } + obj2.src = obj2.canvas.toDataURL("image/jpeg"); + img.src = obj2.src; + container.appendChild(img); + img.onload = function () { + $(container).css({ + "min-height": img.height + 'px' + }) + } + + var time = new Date().getTime(); + var arr1 = self.eventType["render"]; + if (arr1 && arr1 instanceof Array) { + for (var i = 0; i < arr1.length; i++) { + arr1[i] && arr1[i].call(self, pageNum, time - self.initTime, container) + } + } + if (self.loadedCount === self.totalNum) { + self.finalRender(options) + } + }) + }, + finalRender: function (options) { + var time = new Date().getTime(); + var self = this; + self.progress.css({ + width: "100%" + }); + setTimeout(function () { + self.loadingBar.hide(); + }, 300) + self.endTime = time - self.initTime; + if (options.renderType === "svg") { + if (self.totalNum !== 1) { + self.cache[(self.totalNum - 1) + ""].loaded = true; + } else { + self.cache["1"].loaded = true; + } + } + if (options.zoomEnable) { + if (self.pinchZoom) { + self.pinchZoom.enable() + } + } else { + if (self.pinchZoom) { + self.pinchZoom.disable() + } + } + var arr1 = self.eventType["complete"]; + if (arr1 && arr1 instanceof Array) { + for (var i = 0; i < arr1.length; i++) { + arr1[i] && arr1[i].call(self, "success", "pdf加载完成", self.endTime) + } + } + var arr2 = self.eventType["success"]; + if (arr2 && arr2 instanceof Array) { + for (var i = 0; i < arr2.length; i++) { + arr2[i] && arr2[i].call(self, self.endTime) + } + } + }, + resize: function () { + var self = this; + if (self.resizeEvent) { + return + } + self.resizeEvent = true; + var timer; + if (self.pages) { + self.getH() + $(window).on("resize", function () { + clearTimeout(timer) + timer = setTimeout(function () { + var winWidth = $(window).width(); + if (self.winWidth !== winWidth){ + self.pages.each(function (i, item) { + var w = $(item).width(), s = $(item).attr("data-scale"); + $(item).css({ + "min-height": w/s + 'px' + }) + }) + } + self.getH() + },300) + }) + } + }, + getH:function(){ + var self = this; + var num =0; + self.pages.each(function (i, item) { + var w = $(item).height(); + num + w + }) + $(".pinch-zoom-container").height(num) + }, + show: function (callback) { + this.container.show(); + callback && callback.call(this) + var arr = this.eventType["show"]; + if (arr && arr instanceof Array) { + for (var i = 0; i < arr.length; i++) { + arr[i] && arr[i].call(this) + } + } + }, + hide: function (callback) { + this.container.hide() + callback && callback.call(this) + var arr = this.eventType["hide"]; + if (arr && arr instanceof Array) { + for (var i = 0; i < arr.length; i++) { + arr[i] && arr[i].call(this) + } + } + }, + on: function (type, callback) { + if (this.eventType[type] && this.eventType[type] instanceof Array) { + this.eventType[type].push(callback) + } + this.eventType[type] = [callback] + }, + off: function (type) { + if (type !== undefined) { + this.eventType[type] = [null] + } else { + for (var i in this.eventType) { + this.eventType[i] = [null] + } + } + }, + scrollEnable: function (flag) { + if (flag === false) { + this.viewerContainer.css({ + "overflow": "hidden" + }) + } else { + this.viewerContainer.css({ + "overflow": "auto" + }) + } + var arr = this.eventType["scrollEnable"]; + if (arr && arr instanceof Array) { + for (var i = 0; i < arr.length; i++) { + arr[i] && arr[i].call(this, flag) + } + } + }, + zoomEnable: function (flag) { + if (flag === false) { + this.pinchZoom.disable() + } else { + this.pinchZoom.enable() + } + var arr = this.eventType["zoomEnable"]; + if (arr && arr instanceof Array) { + for (var i = 0; i < arr.length; i++) { + arr[i] && arr[i].call(this, flag) + } + } + }, + reset: function (callback) { + if (this.pinchZoom) { + this.pinchZoom.offset.y = 0; + this.pinchZoom.offset.x = 0; + this.pinchZoom.lastclientY = 0; + this.pinchZoom.zoomFactor = 1; + this.pinchZoom.update(); + } + if (this.viewerContainer) { + this.viewerContainer.scrollTop(0); + } + callback && callback.call(this) + var arr = this.eventType["reset"]; + if (arr && arr instanceof Array) { + for (var i = 0; i < arr.length; i++) { + arr[i] && arr[i].call(this) + } + } + }, + destroy: function (callback) { + this.reset(); + this.off(); + if (this.thePDF) { + this.thePDF.destroy(); + this.thePDF = null; + } + if (this.viewerContainer) { + this.viewerContainer.remove(); + this.viewerContainer = null; + } + if (this.container) { + this.container.html(''); + } + this.totalNum = null; + this.pages = null; + this.initTime = 0; + this.endTime = 0; + this.viewer = null; + this.pageNum = null; + this.pageNow = null; + this.pageTotal = null; + this.loadingBar = null; + this.progress = null; + this.loadedCount = 0; + this.timer = null; + this.show = null; + this.hide = null; + callback && callback.call(this) + var arr = this.eventType["destroy"]; + if (arr && arr instanceof Array) { + for (var i = 0; i < arr.length; i++) { + arr[i] && arr[i].call(this) + } + } + } + } + return Pdfh5; +}); \ No newline at end of file diff --git a/js/pdfh5.js b/js/pdfh5.js index 1870207..3aece1d 100644 --- a/js/pdfh5.js +++ b/js/pdfh5.js @@ -1 +1,1369 @@ -!function(t,e){var i="1.2.20",n="2.1.266";console.log("The latest version and API of pdfh5 from: https://www.gjtool.cn (pdf.js: "+n+" , pdfh5.js: "+i+")"),"undefined"!=typeof require&&(void 0===t.$&&(t.$=require("./jquery-1.11.3.min.js")),t.pdfjsWorker=require("./pdf.worker.js"),t.pdfjsLib=require("./pdf.js"));var a=t.pdfjsLib,o=t.$,s=t.pdfjsWorker;"function"==typeof define&&define.amd?define(function(){return e(t,s,a,o,i)}):"undefined"!=typeof module&&module.exports?module.exports=e(t,s,a,o,i):t.Pdfh5=e(t,s,a,o,i)}("undefined"!=typeof window?window:this,function(t,e,i,n,a){"use strict";var o=function(e){var i=function(t,i,n){this.el=e(t),this.viewerContainer=n,this.zoomFactor=1,this.lastScale=1,this.offset={x:0,y:0},this.options=e.extend({},this.defaults,i),this.options.tapZoomFactor=isNaN(i.tapZoomFactor)?2:i.tapZoomFactor,this.options.zoomOutFactor=isNaN(i.zoomOutFactor)?1.2:i.zoomOutFactor,this.options.animationDuration=isNaN(i.animationDuration)?300:i.animationDuration,this.options.maxZoom=isNaN(i.maxZoom)?4:i.maxZoom,this.options.minZoom=isNaN(i.minZoom)?.8:i.minZoom,this.setupMarkup(),this.bindEvents(),this.update(),this.enable(),this.height=0,this.load=!1,this.direction=null,this.clientY=null,this.lastclientY=null},n=function(t,e){return t+e},a=function(t,e){return t>e-.01&&t1){var e=this.getTouches(t)[0];this.drag(e,this.lastDragPosition,t),this.offset=this.sanitizeOffset(this.offset),this.lastDragPosition=e}},handleDragEnd:function(){this.el.trigger(this.options.dragEndEventName),this.end()},handleZoomStart:function(t){this.el.trigger(this.options.zoomStartEventName),this.stopAnimation(),this.lastScale=1,this.nthZoom=0,this.lastZoomCenter=!1,this.hasInteraction=!0},handleZoom:function(t,e){var i=this.getTouchCenter(this.getTouches(t)),n=e/this.lastScale;this.lastScale=e,this.nthZoom+=1,this.nthZoom>3&&(this.scale(n,i),this.drag(i,this.lastZoomCenter)),this.lastZoomCenter=i},handleZoomEnd:function(){this.el.trigger(this.options.zoomEndEventName),this.end()},handleDoubleTap:function(t){var e=this.getTouches(t)[0],i=this.zoomFactor>1?1:this.options.tapZoomFactor,n=this.zoomFactor,a=function(t){this.scaleTo(n+t*(i-n),e)}.bind(this);this.hasInteraction||(n>i&&(e=this.getCurrentZoomCenter()),this.animate(this.options.animationDuration,a,this.swing),this.el.trigger(this.options.doubleTapEventName))},sanitizeOffset:function(t){var e=(this.zoomFactor-1)*this.getContainerX(),i=(this.zoomFactor-1)*this.getContainerY(),n=Math.max(e,0),a=Math.max(i,0),o=Math.min(e,0),s=Math.min(i,0),r=Math.min(Math.max(t.x,o),n),h=Math.min(Math.max(t.y,s),a);return{x:r,y:h}},scaleTo:function(t,e){this.scale(t/this.zoomFactor,e)},scale:function(t,e){t=this.scaleZoomFactor(t),this.addOffset({x:(t-1)*(e.x+this.offset.x),y:(t-1)*(e.y+this.offset.y)})},scaleZoomFactor:function(t){var e=this.zoomFactor;return this.zoomFactor*=t,this.zoomFactor=Math.min(this.options.maxZoom,Math.max(this.zoomFactor,this.options.minZoom)),this.zoomFactor/e},drag:function(t,e,i){e&&(this.options.lockDragAxis?Math.abs(t.x-e.x)>Math.abs(t.y-e.y)?this.addOffset({x:-(t.x-e.x),y:0}):this.addOffset({y:-(t.y-e.y),x:0}):(t.y-e.y<0?this.direction="down":t.y-e.y>10&&(this.direction="up"),this.addOffset({y:-(t.y-e.y),x:-(t.x-e.x)})))},getTouchCenter:function(t){return this.getVectorAvg(t)},getVectorAvg:function(t){return{x:t.map(function(t){return t.x}).reduce(n)/t.length,y:t.map(function(t){return t.y}).reduce(n)/t.length}},addOffset:function(t){this.offset={x:this.offset.x+t.x,y:this.offset.y+t.y}},sanitize:function(){this.zoomFactor=t?(e(1),n&&n(),this.update(),this.stopAnimation(),this.update()):(i&&(r=i(r)),e(r),this.update(),requestAnimationFrame(o))}}.bind(this);this.inAnimation=!0,requestAnimationFrame(o)},stopAnimation:function(){this.inAnimation=!1},swing:function(t){return-Math.cos(t*Math.PI)/2+.5},getContainerX:function(){return this.el[0]?this.el[0].offsetWidth:0},getContainerY:function(){return this.el[0].offsetHeight},setContainerY:function(t){return t=t.toFixed(2),this.container.height(t)},setupMarkup:function(){this.container=e('
'),this.el.before(this.container),this.container.append(this.el),this.container.css({position:"relative"}),this.el.css({"-webkit-transform-origin":"0% 0%","-moz-transform-origin":"0% 0%","-ms-transform-origin":"0% 0%","-o-transform-origin":"0% 0%","transform-origin":"0% 0%",position:"relative"})},end:function(){this.hasInteraction=!1,this.sanitize(),this.update()},bindEvents:function(){o(this.container.eq(0),this,this.viewerContainer),e(t).on("resize",this.update.bind(this)),e(this.el).find("img").on("load",this.update.bind(this))},update:function(){this.updatePlaned||(this.updatePlaned=!0,setTimeout(function(){this.updatePlaned=!1,this.updateAspectRatio();var t=this.getInitialZoomFactor()*this.zoomFactor,e=(-this.offset.x/t).toFixed(3),i=(-this.offset.y/t).toFixed(3);this.lastclientY=i;var n="scale3d("+t+", "+t+",1) translate3d("+e+"px,"+i+"px,0px)",a="scale("+t+", "+t+") translate("+e+"px,"+i+"px)";(function(){this.clone&&(this.clone.remove(),delete this.clone)}).bind(this);!this.options.use2d||this.hasInteraction||this.inAnimation?(this.is3d=!0,this.el.css({"-webkit-transform":n,"-o-transform":a,"-ms-transform":a,"-moz-transform":a,transform:n})):(this.el.css({"-webkit-transform":a,"-o-transform":a,"-ms-transform":a,"-moz-transform":a,transform:a}),this.is3d=!1),this.done&&this.done.call(this,t)}.bind(this),0))},enable:function(){this.enabled=!0},disable:function(){this.enabled=!1},destroy:function(){var t=this.el.clone(),e=this.container.parent();this.container.remove(),t.removeAttr("style"),e.append(t)}};var o=function(t,e,i){var n=null,a=0,o=null,s=null,r=null,h=null,c=0,l=0,d=function(t,i){if(n!==t){if(n&&!t)switch(n){case"zoom":e.handleZoomEnd(i);break;case"drag":e.handleDragEnd(i)}switch(t){case"zoom":e.handleZoomStart(i);break;case"drag":e.handleDragStart(i)}}n=t},f=function(t){2===a?d("zoom"):1===a&&e.canDrag()?d("drag",t):d(null,t)},u=function(t){return Array.prototype.slice.call(t).map(function(t){return{x:t.pageX,y:t.pageY}})},m=function(t,e){var i,n;return i=t.x-e.x,n=t.y-e.y,Math.sqrt(i*i+n*n)},p=function(t,e){var i=m(t[0],t[1]),n=m(e[0],e[1]);return n/i},g=function(t){t.stopPropagation(),t.preventDefault()},v=function(t){var i=(new Date).getTime(),s=t.changedTouches[0].pageY,h=w.scrollTop||0;if(a>1&&(o=null,r=null,g(t)),i-o<300&&Math.abs(s-r)<10&&Math.abs(l-h)<10)switch(g(t),e.handleDoubleTap(t),n){case"zoom":e.handleZoomEnd(t);break;case"drag":e.handleDragEnd(t)}1===a&&(o=i,r=s,l=h)},y=!0;if(i)var w=i[0];w&&(w.addEventListener("touchstart",function(t){e.enabled&&(y=!0,a=t.touches.length,v(t),h=t.changedTouches[0].clientY,a>1&&g(t))}),w.addEventListener("touchmove",function(t){if(e.enabled){if(c=t.changedTouches[0].clientY,y)f(t),s=u(t.touches);else{switch(n){case"zoom":e.handleZoom(t,p(s,u(t.touches)));break;case"drag":e.handleDrag(t)}n&&e.update(c)}a>1&&g(t),y=!1}}),w.addEventListener("touchend",function(t){e.enabled&&(a=t.touches.length,a>1&&g(t),f(t))}))};return i},s=o(n),r=function(t,e){this.version=a,this.container=n(t),this.thePDF=null,this.totalNum=null,this.pages=null,this.initTime=0,this.scale=1.3,this.currentNum=1,this.loadedCount=0,this.endTime=0,this.pinchZoom=null,this.timer=null,this.docWidth=document.documentElement.clientWidth,this.winWidth=n(window).width(),this.eventType={},this.cache={},this.cacheNum=1,this.options=e,this.resizeEvent=!1,this.init(e)};return r.prototype={init:function(e){function i(e){var i=new RegExp("(^|&)"+e+"=([^&]*)(&|$)"),n=t.location.search.substr(1).match(i);return null!=n?decodeURIComponent(n[2]):""}this.container[0].pdfLoaded&&this.destroy(),this.container[0].pdfLoaded=!1,this.container.addClass("pdfjs");var a=this;this.initTime=(new Date).getTime(),setTimeout(function(){var t=a.eventType.scroll;if(t&&t instanceof Array)for(var e=0;e=150?e.backTop&&a.backTop.show():e.backTop&&a.backTop.fadeOut(200),a.viewerContainer&&(a.pages=a.viewerContainer.find(".pageContainer")),clearTimeout(a.timer),e.pageNum&&a.pageNum&&a.pageNum.show();var i=h;if(a.pages&&a.pages.each(function(t,n){var o=n.getBoundingClientRect().top,s=n.getBoundingClientRect().bottom;o<=c&&s>c&&(e.pageNum&&a.pageNow.text(t+1),a.currentNum=t+1),o<=i&&s>i&&(a.cacheNum=t+1)}),t+a.container.height()>=a.viewer[0].offsetHeight&&a.pageNow.text(a.totalNum),0===t&&a.pageNow.text(1),a.timer=setTimeout(function(){e.pageNum&&a.pageNum&&a.pageNum.fadeOut(200)},1500),e.lazy){var n=Math.floor(100/a.totalNum).toFixed(2);if(!a.cache[a.cacheNum+""].loaded){var o=a.cache[a.cacheNum+""].page,s=a.cache[a.cacheNum+""].container,l=a.cacheNum;a.cache[l+""].loaded=!0;var d=a.cache[l+""].scaledViewport;"svg"===e.renderType?a.renderSvg(o,d,l,n,s,e):a.renderCanvas(o,d,l,n,s,e)}if(a.cache[a.totalNum-1+""].loaded&&!a.cache[a.totalNum+""].loaded){var o=a.cache[a.totalNum+""].page,s=a.cache[a.totalNum+""].container,l=a.totalNum;a.cache[l+""].loaded=!0;var d=a.cache[l+""].scaledViewport;"svg"===e.renderType?a.renderSvg(o,d,l,n,s,e):a.renderCanvas(o,d,l,n,s,e)}}var f=a.eventType.scroll;if(f&&f instanceof Array)for(var u=0;ud&&t.lazy))return"svg"===t.renderType?a.renderSvg(i,r,e,h,c,t,o):a.renderCanvas(i,r,e,h,c,t)})}.bind(null,o))}).catch(function(t){a.loading.hide();var e=(new Date).getTime();a.endTime=e-a.initTime;var i=a.eventType.complete;if(i&&i instanceof Array)for(var n=0;n expected - 0.01 && value < expected + 0.01; + }; + + PinchZoom.prototype = { + defaults: { + tapZoomFactor: 2, + zoomOutFactor: 1.2, + animationDuration: 300, + maxZoom: 4, + minZoom: 0.8, + lockDragAxis: false, + use2d: true, + zoomStartEventName: 'pz_zoomstart', + zoomEndEventName: 'pz_zoomend', + dragStartEventName: 'pz_dragstart', + dragEndEventName: 'pz_dragend', + doubleTapEventName: 'pz_doubletap' + }, + handleDragStart: function (event) { + this.el.trigger(this.options.dragStartEventName); + this.stopAnimation(); + this.lastDragPosition = false; + this.hasInteraction = true; + this.handleDrag(event); + }, + handleDrag: function (event) { + + if (this.zoomFactor > 1.0) { + var touch = this.getTouches(event)[0]; + this.drag(touch, this.lastDragPosition, event); + this.offset = this.sanitizeOffset(this.offset); + this.lastDragPosition = touch; + } + }, + + handleDragEnd: function () { + this.el.trigger(this.options.dragEndEventName); + this.end(); + }, + handleZoomStart: function (event) { + this.el.trigger(this.options.zoomStartEventName); + this.stopAnimation(); + this.lastScale = 1; + this.nthZoom = 0; + this.lastZoomCenter = false; + this.hasInteraction = true; + }, + handleZoom: function (event, newScale) { + var touchCenter = this.getTouchCenter(this.getTouches(event)), + scale = newScale / this.lastScale; + this.lastScale = newScale; + this.nthZoom += 1; + if (this.nthZoom > 3) { + + this.scale(scale, touchCenter); + this.drag(touchCenter, this.lastZoomCenter); + } + this.lastZoomCenter = touchCenter; + }, + + handleZoomEnd: function () { + this.el.trigger(this.options.zoomEndEventName); + this.end(); + }, + handleDoubleTap: function (event) { + var center = this.getTouches(event)[0], + zoomFactor = this.zoomFactor > 1 ? 1 : this.options.tapZoomFactor, + startZoomFactor = this.zoomFactor, + updateProgress = (function (progress) { + this.scaleTo(startZoomFactor + progress * (zoomFactor - startZoomFactor), center); + }).bind(this); + + if (this.hasInteraction) { + return; + } + if (startZoomFactor > zoomFactor) { + center = this.getCurrentZoomCenter(); + } + + this.animate(this.options.animationDuration, updateProgress, this.swing); + this.el.trigger(this.options.doubleTapEventName); + }, + sanitizeOffset: function (offset) { + var maxX = (this.zoomFactor - 1) * this.getContainerX(), + maxY = (this.zoomFactor - 1) * this.getContainerY(), + maxOffsetX = Math.max(maxX, 0), + maxOffsetY = Math.max(maxY, 0), + minOffsetX = Math.min(maxX, 0), + minOffsetY = Math.min(maxY, 0); + + var x = Math.min(Math.max(offset.x, minOffsetX), maxOffsetX), + y = Math.min(Math.max(offset.y, minOffsetY), maxOffsetY); + + + return { + x: x, + y: y + }; + }, + scaleTo: function (zoomFactor, center) { + this.scale(zoomFactor / this.zoomFactor, center); + }, + scale: function (scale, center) { + scale = this.scaleZoomFactor(scale); + this.addOffset({ + x: (scale - 1) * (center.x + this.offset.x), + y: (scale - 1) * (center.y + this.offset.y) + }); + }, + scaleZoomFactor: function (scale) { + var originalZoomFactor = this.zoomFactor; + this.zoomFactor *= scale; + this.zoomFactor = Math.min(this.options.maxZoom, Math.max(this.zoomFactor, this.options.minZoom)); + return this.zoomFactor / originalZoomFactor; + }, + drag: function (center, lastCenter, event) { + if (lastCenter) { + if (this.options.lockDragAxis) { + if (Math.abs(center.x - lastCenter.x) > Math.abs(center.y - lastCenter.y)) { + this.addOffset({ + x: -(center.x - lastCenter.x), + y: 0 + }); + } else { + this.addOffset({ + y: -(center.y - lastCenter.y), + x: 0 + }); + } + } else { + if (center.y - lastCenter.y < 0) { + this.direction = "down"; + } else if (center.y - lastCenter.y > 10) { + this.direction = "up"; + } + this.addOffset({ + y: -(center.y - lastCenter.y), + x: -(center.x - lastCenter.x) + }); + } + } + }, + getTouchCenter: function (touches) { + return this.getVectorAvg(touches); + }, + getVectorAvg: function (vectors) { + return { + x: vectors.map(function (v) { + return v.x; + }).reduce(sum) / vectors.length, + y: vectors.map(function (v) { + return v.y; + }).reduce(sum) / vectors.length + }; + }, + addOffset: function (offset) { + this.offset = { + x: this.offset.x + offset.x, + y: this.offset.y + offset.y + }; + }, + + sanitize: function () { + if (this.zoomFactor < this.options.zoomOutFactor) { + this.zoomOutAnimation(); + } else if (this.isInsaneOffset(this.offset)) { + this.sanitizeOffsetAnimation(); + } + }, + isInsaneOffset: function (offset) { + var sanitizedOffset = this.sanitizeOffset(offset); + return sanitizedOffset.x !== offset.x || + sanitizedOffset.y !== offset.y; + }, + sanitizeOffsetAnimation: function () { + var targetOffset = this.sanitizeOffset(this.offset), + startOffset = { + x: this.offset.x, + y: this.offset.y + }, + updateProgress = (function (progress) { + this.offset.x = startOffset.x + progress * (targetOffset.x - startOffset.x); + this.offset.y = startOffset.y + progress * (targetOffset.y - startOffset.y); + this.update(); + }).bind(this); + + this.animate( + this.options.animationDuration, + updateProgress, + this.swing + ); + }, + zoomOutAnimation: function () { + var startZoomFactor = this.zoomFactor, + zoomFactor = 1, + center = this.getCurrentZoomCenter(), + updateProgress = (function (progress) { + this.scaleTo(startZoomFactor + progress * (zoomFactor - startZoomFactor), center); + }).bind(this); + + this.animate( + this.options.animationDuration, + updateProgress, + this.swing + ); + }, + updateAspectRatio: function () { + this.setContainerY(this.getContainerX() / this.getAspectRatio()); + }, + getInitialZoomFactor: function () { + if (this.container[0] && this.el[0]) { + return this.container[0].offsetWidth / this.el[0].offsetWidth; + } else { + return 0 + } + }, + getAspectRatio: function () { + if (this.el[0]) { + var offsetHeight = this.el[0].offsetHeight; + return this.container[0].offsetWidth / offsetHeight; + } else { + return 0 + } + + }, + getCurrentZoomCenter: function () { + var length = this.container[0].offsetWidth * this.zoomFactor, + offsetLeft = this.offset.x, + offsetRight = length - offsetLeft - this.container[0].offsetWidth, + widthOffsetRatio = offsetLeft / offsetRight, + centerX = widthOffsetRatio * this.container[0].offsetWidth / (widthOffsetRatio + 1), + + height = this.container[0].offsetHeight * this.zoomFactor, + offsetTop = this.offset.y, + offsetBottom = height - offsetTop - this.container[0].offsetHeight, + heightOffsetRatio = offsetTop / offsetBottom, + centerY = heightOffsetRatio * this.container[0].offsetHeight / (heightOffsetRatio + 1); + + if (offsetRight === 0) { + centerX = this.container[0].offsetWidth; + } + if (offsetBottom === 0) { + centerY = this.container[0].offsetHeight; + } + + return { + x: centerX, + y: centerY + }; + }, + + canDrag: function () { + return !isCloseTo(this.zoomFactor, 1); + }, + + getTouches: function (event) { + var position = this.container.offset(); + return Array.prototype.slice.call(event.touches).map(function (touch) { + return { + x: touch.pageX - position.left, + y: touch.pageY - position.top + }; + }); + }, + animate: function (duration, framefn, timefn, callback) { + var startTime = new Date().getTime(), + renderFrame = (function () { + if (!this.inAnimation) { + return; + } + var frameTime = new Date().getTime() - startTime, + progress = frameTime / duration; + if (frameTime >= duration) { + framefn(1); + if (callback) { + callback(); + } + this.update(); + this.stopAnimation(); + this.update(); + } else { + if (timefn) { + progress = timefn(progress); + } + framefn(progress); + this.update(); + requestAnimationFrame(renderFrame); + } + }).bind(this); + this.inAnimation = true; + requestAnimationFrame(renderFrame); + }, + stopAnimation: function () { + this.inAnimation = false; + + }, + swing: function (p) { + return -Math.cos(p * Math.PI) / 2 + 0.5; + }, + + getContainerX: function () { + if (this.el[0]) { + return this.el[0].offsetWidth; + } else { + return 0; + } + }, + + getContainerY: function () { + return this.el[0].offsetHeight; + }, + setContainerY: function (y) { + y = y.toFixed(2); + return this.container.height(y); + }, + setupMarkup: function () { + this.container = $('
'); + this.el.before(this.container); + this.container.append(this.el); + + this.container.css({ + 'position': 'relative', + }); + + this.el.css({ + '-webkit-transform-origin': '0% 0%', + '-moz-transform-origin': '0% 0%', + '-ms-transform-origin': '0% 0%', + '-o-transform-origin': '0% 0%', + 'transform-origin': '0% 0%', + 'position': 'relative' + }); + + }, + + end: function () { + this.hasInteraction = false; + this.sanitize(); + this.update(); + + }, + bindEvents: function () { + detectGestures(this.container.eq(0), this, this.viewerContainer); + $(g).on('resize', this.update.bind(this)); + $(this.el).find('img').on('load', this.update.bind(this)); + + }, + update: function () { + + if (this.updatePlaned) { + return; + } + this.updatePlaned = true; + setTimeout((function () { + this.updatePlaned = false; + this.updateAspectRatio(); + var zoomFactor = this.getInitialZoomFactor() * this.zoomFactor, + offsetX = (-this.offset.x / zoomFactor).toFixed(3), + offsetY = (-this.offset.y / zoomFactor).toFixed(3); + this.lastclientY = offsetY; + + var transform3d = 'scale3d(' + zoomFactor + ', ' + zoomFactor + ',1) ' + + 'translate3d(' + offsetX + 'px,' + offsetY + 'px,0px)', + transform2d = 'scale(' + zoomFactor + ', ' + zoomFactor + ') ' + + 'translate(' + offsetX + 'px,' + offsetY + 'px)', + removeClone = (function () { + if (this.clone) { + this.clone.remove(); + delete this.clone; + } + }).bind(this); + if (!this.options.use2d || this.hasInteraction || this.inAnimation) { + this.is3d = true; + this.el.css({ + '-webkit-transform': transform3d, + '-o-transform': transform2d, + '-ms-transform': transform2d, + '-moz-transform': transform2d, + 'transform': transform3d + }); + } else { + this.el.css({ + '-webkit-transform': transform2d, + '-o-transform': transform2d, + '-ms-transform': transform2d, + '-moz-transform': transform2d, + 'transform': transform2d + }); + this.is3d = false; + } + this.done && this.done.call(this, zoomFactor) + }).bind(this), 0); + }, + enable: function () { + this.enabled = true; + }, + disable: function () { + this.enabled = false; + }, + destroy: function () { + var dom = this.el.clone(); + var p = this.container.parent(); + this.container.remove(); + dom.removeAttr('style'); + p.append(dom); + } + }; + + var detectGestures = function (el, target, viewerContainer) { + var interaction = null, + fingers = 0, + lastTouchStart = null, + startTouches = null, + lastTouchY = null, + clientY = null, + lastclientY = 0, + lastTop = 0, + setInteraction = function (newInteraction, event) { + if (interaction !== newInteraction) { + + if (interaction && !newInteraction) { + switch (interaction) { + case "zoom": + target.handleZoomEnd(event); + break; + case 'drag': + target.handleDragEnd(event); + break; + } + } + + switch (newInteraction) { + case 'zoom': + target.handleZoomStart(event); + break; + case 'drag': + target.handleDragStart(event); + break; + } + } + interaction = newInteraction; + }, + + updateInteraction = function (event) { + if (fingers === 2) { + setInteraction('zoom'); + } else if (fingers === 1 && target.canDrag()) { + setInteraction('drag', event); + } else { + setInteraction(null, event); + } + }, + + targetTouches = function (touches) { + return Array.prototype.slice.call(touches).map(function (touch) { + return { + x: touch.pageX, + y: touch.pageY + }; + }); + }, + + getDistance = function (a, b) { + var x, y; + x = a.x - b.x; + y = a.y - b.y; + return Math.sqrt(x * x + y * y); + }, + + calculateScale = function (startTouches, endTouches) { + var startDistance = getDistance(startTouches[0], startTouches[1]), + endDistance = getDistance(endTouches[0], endTouches[1]); + return endDistance / startDistance; + }, + + cancelEvent = function (event) { + event.stopPropagation(); + event.preventDefault(); + }, + + detectDoubleTap = function (event) { + var time = (new Date()).getTime(); + var pageY = event.changedTouches[0].pageY; + var top = parentNode.scrollTop || 0; + if (fingers > 1) { + lastTouchStart = null; + lastTouchY = null; + cancelEvent(event); + } + + if (time - lastTouchStart < 300 && Math.abs(pageY - lastTouchY) < 10 && Math.abs(lastTop - top) < 10) { + cancelEvent(event); + target.handleDoubleTap(event); + switch (interaction) { + case "zoom": + target.handleZoomEnd(event); + break; + case 'drag': + target.handleDragEnd(event); + break; + } + } + + if (fingers === 1) { + lastTouchStart = time; + lastTouchY = pageY; + lastTop = top; + } + }, + firstMove = true; + if (viewerContainer) { + var parentNode = viewerContainer[0]; + } + if (parentNode) { + parentNode.addEventListener('touchstart', function (event) { + if (target.enabled) { + firstMove = true; + fingers = event.touches.length; + detectDoubleTap(event); + clientY = event.changedTouches[0].clientY; + if (fingers > 1) { + cancelEvent(event); + } + } + }); + + parentNode.addEventListener('touchmove', function (event) { + if (target.enabled) { + lastclientY = event.changedTouches[0].clientY; + if (firstMove) { + updateInteraction(event); + startTouches = targetTouches(event.touches); + } else { + switch (interaction) { + case 'zoom': + target.handleZoom(event, calculateScale(startTouches, targetTouches(event.touches))); + break; + case 'drag': + target.handleDrag(event); + break; + } + if (interaction) { + target.update(lastclientY); + } + } + if (fingers > 1) { + cancelEvent(event); + } + firstMove = false; + } + }); + + parentNode.addEventListener('touchend', function (event) { + if (target.enabled) { + fingers = event.touches.length; + if (fingers > 1) { + cancelEvent(event); + } + updateInteraction(event); + } + }); + } + + }; + return PinchZoom; + }; + var PinchZoom = definePinchZoom($); + var Pdfh5 = function (dom, options) { + this.version = version; + this.container = $(dom); + this.thePDF = null; + this.totalNum = null; + this.pages = null; + this.initTime = 0; + this.scale = 1.3; + this.currentNum = 1; + this.loadedCount = 0; + this.endTime = 0; + this.pinchZoom = null; + this.timer = null; + this.docWidth = document.documentElement.clientWidth; + this.winWidth = $(window).width(); + this.eventType = {}; + this.cache = {}; + this.cacheNum = 1; + this.options = options; + this.resizeEvent = false; + this.init(options); + }; + Pdfh5.prototype = { + init: function (options) { + if (this.container[0].pdfLoaded) { + this.destroy(); + } + this.container[0].pdfLoaded = false; + this.container.addClass("pdfjs") + var self = this; + this.initTime = new Date().getTime(); + setTimeout(function () { + var arr1 = self.eventType["scroll"]; + if (arr1 && arr1 instanceof Array) { + for (var i = 0; i < arr1.length; i++) { + arr1[i] && arr1[i].call(self, self.initTime) + } + } + }, 0) + options = options ? options : {}; + options.pdfurl = options.pdfurl ? options.pdfurl : null; + options.data = options.data ? options.data : null; + options.scale = options.scale ? options.scale : self.scale; + options.zoomEnable = options.zoomEnable === false ? false : true; + options.scrollEnable = options.scrollEnable === false ? false : true; + options.loadingBar = options.loadingBar === false ? false : true; + options.pageNum = options.pageNum === false ? false : true; + options.backTop = options.backTop === false ? false : true; + options.URIenable = options.URIenable === false ? false : true; + options.fullscreen = options.fullscreen === false ? false : true; + options.lazy = options.lazy === true ? true : false; + options.renderType = options.renderType === "canvas" ? "canvas" : "svg"; + options.resize = options.resize === false ? false : true; + if (options.renderType === "canvas") { + options.scale = 2; + } + options.type = options.type === "ajax" ? "ajax" : "fetch"; + var html = '
' + + '
' + + '
' + + '
' + + '
' + + '
' + + '
' + + '
' + + '
' + + ' 1/' + + '1' + + '
' + + '
' + + '
' + + '
' + + '
'; + if (!this.container.find('.pageNum')[0]) { + this.container.append(html); + } + var viewer = document.createElement("div"); + viewer.className = 'pdfViewer'; + var viewerContainer = document.createElement("div"); + viewerContainer.className = 'viewerContainer'; + viewerContainer.appendChild(viewer); + this.container.append(viewerContainer); + this.viewer = $(viewer); + this.viewerContainer = $(viewerContainer); + this.pageNum = this.container.find('.pageNum'); + this.pageNow = this.pageNum.find('.pageNow'); + this.pageTotal = this.pageNum.find('.pageTotal'); + this.loadingBar = this.container.find('.loadingBar'); + this.progress = this.loadingBar.find('.progress'); + this.progressDom = this.progress[0]; + this.backTop = this.container.find('.backTop'); + this.loading = this.container.find('.loading'); + if (!options.loadingBar) { + this.loadingBar.hide() + } + var containerH = this.container.height(), + height = containerH * (1 / 3); + + if (!options.scrollEnable) { + this.viewerContainer.css({ + "overflow": "hidden" + }) + } else { + this.viewerContainer.css({ + "overflow": "auto" + }) + } + viewerContainer.addEventListener('scroll', function () { + var scrollTop = viewerContainer.scrollTop; + if (scrollTop >= 150) { + if (options.backTop) { + self.backTop.show(); + } + } else { + if (options.backTop) { + self.backTop.fadeOut(200); + } + } + if (self.viewerContainer) { + self.pages = self.viewerContainer.find('.pageContainer'); + } + clearTimeout(self.timer); + if (options.pageNum && self.pageNum) { + self.pageNum.show(); + } + var h = containerH; + if (self.pages) { + self.pages.each(function (index, obj) { + var top = obj.getBoundingClientRect().top; + var bottom = obj.getBoundingClientRect().bottom; + if (top <= height && bottom > height) { + if (options.pageNum) { + self.pageNow.text(index + 1) + } + self.currentNum = index + 1; + } + if (top <= h && bottom > h) { + self.cacheNum = index + 1; + } + }) + } + if (scrollTop + self.container.height() >= self.viewer[0].offsetHeight) { + self.pageNow.text(self.totalNum) + } + if (scrollTop === 0) { + self.pageNow.text(1) + } + self.timer = setTimeout(function () { + if (options.pageNum && self.pageNum) { + self.pageNum.fadeOut(200); + } + }, 1500) + if (options.lazy) { + var num = Math.floor(100 / self.totalNum).toFixed(2); + if (self.cache[self.cacheNum + ""] && !self.cache[self.cacheNum + ""].loaded) { + var page = self.cache[self.cacheNum + ""].page; + var container = self.cache[self.cacheNum + ""].container; + var pageNum = self.cacheNum; + self.cache[pageNum + ""].loaded = true; + var scaledViewport = self.cache[pageNum + ""].scaledViewport; + if (options.renderType === "svg") { + self.renderSvg(page, scaledViewport, pageNum, num, container, options) + } else { + self.renderCanvas(page, scaledViewport, pageNum, num, container, options) + } + } + if (self.cache[(self.totalNum - 1) + ""] && self.cache[(self.totalNum - 1) + ""].loaded && !self.cache[self.totalNum + ""].loaded) { + var page = self.cache[self.totalNum + ""].page; + var container = self.cache[self.totalNum + ""].container; + var pageNum = self.totalNum; + self.cache[pageNum + ""].loaded = true; + var scaledViewport = self.cache[pageNum + ""].scaledViewport; + if (options.renderType === "svg") { + self.renderSvg(page, scaledViewport, pageNum, num, container, options) + } else { + self.renderCanvas(page, scaledViewport, pageNum, num, container, options) + } + } + } + var arr1 = self.eventType["scroll"]; + if (arr1 && arr1 instanceof Array) { + for (var i = 0; i < arr1.length; i++) { + arr1[i] && arr1[i].call(self, scrollTop) + } + } + }) + this.backTop.on('click tap', function () { + var mart = self.viewer.css('transform'); + var arr = mart.replace(/[a-z\(\)\s]/g, '').split(','); + var s1 = arr[0]; + var s2 = arr[3]; + var x = arr[4] / 2; + var left = self.viewer[0].getBoundingClientRect().left; + if (left <= -self.docWidth * 2) { + x = -self.docWidth / 2 + } + self.viewer.css({ + transform: 'scale(' + s1 + ', ' + s2 + ') translate(' + x + 'px, 0px)' + }) + if (self.pinchZoom) { + self.pinchZoom.offset.y = 0; + self.pinchZoom.lastclientY = 0; + } + self.viewerContainer.animate({ + scrollTop: 0 + }, 300) + var arr1 = self.eventType["backTop"]; + if (arr1 && arr1 instanceof Array) { + for (var i = 0; i < arr1.length; i++) { + arr1[i] && arr1[i].call(self) + } + } + }) + //获取url带的参数地址 + function GetQueryString(name) { + var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); + var r = g.location.search.substr(1).match(reg); + if (r != null) return decodeURIComponent(r[2]); + return ""; + } + var pdfurl = GetQueryString("file"), url = ""; + if (pdfurl && options.URIenable) { + url = pdfurl + } else if (options.pdfurl) { + url = options.pdfurl + } + if (url) { + if (options.type === "ajax") { + $.ajax({ + type: "get", + mimeType: 'text/plain; charset=x-user-defined', + cache: false, + url: url, + success: function (data) { + var rawLength = data.length; + // var array = new Uint8Array(new ArrayBuffer(rawLength)); + // for (i = 0; i < rawLength; i++) { + // array[i] = data.charCodeAt(i) & 0xff; + // } + var array = []; + for (i = 0; i < rawLength; i++) { + array.push(data.charCodeAt(i) & 0xff); + } + self.renderPdf(options, { + data: array + }) + }, + error: function (err) { + self.loading.hide() + var time = new Date().getTime(); + self.endTime = time - self.initTime; + var arr1 = self.eventType["complete"]; + if (arr1 && arr1 instanceof Array) { + for (var i = 0; i < arr1.length; i++) { + arr1[i] && arr1[i].call(self, "error", err.statusText, self.endTime) + } + } + var arr2 = self.eventType["error"]; + if (arr2 && arr2 instanceof Array) { + for (var i = 0; i < arr2.length; i++) { + arr2[i] && arr2[i].call(self, err.statusText, self.endTime) + } + } + throw Error(err.statusText) + } + }); + } else { + self.renderPdf(options, { + url: url + }) + } + } else if (options.data) { + var data = options.data; + var rawLength = data.length; + if (typeof data === "string" && data != "") { + var array = []; + for (i = 0; i < rawLength; i++) { + array.push(data.charCodeAt(i) & 0xff); + } + self.renderPdf(options, { + data: array + }) + } else if (typeof data === "object") { + if (data[0] === 0) { + var time = new Date().getTime(); + self.endTime = time - self.initTime; + var arr1 = self.eventType["complete"]; + if (arr1 && arr1 instanceof Array) { + for (var i = 0; i < arr1.length; i++) { + arr1[i] && arr1[i].call(self, "error", "options.data is empty Array", self.endTime) + } + } + var arr2 = self.eventType["error"]; + if (arr2 && arr2 instanceof Array) { + for (var i = 0; i < arr2.length; i++) { + arr2[i] && arr2[i].call(self, "options.data is empty Array", self.endTime) + } + } + throw Error("options.data is empty Array") + } else { + self.renderPdf(options, { + data: data + }) + } + } + + } else { + var time = new Date().getTime(); + self.endTime = time - self.initTime; + var arr1 = self.eventType["complete"]; + if (arr1 && arr1 instanceof Array) { + for (var i = 0; i < arr1.length; i++) { + arr1[i] && arr1[i].call(self, "error", "Expect options.pdfurl or options.data!", self.endTime) + } + } + var arr2 = self.eventType["error"]; + if (arr2 && arr2 instanceof Array) { + for (var i = 0; i < arr2.length; i++) { + arr2[i] && arr2[i].call(self, "Expect options.pdfurl or options.data!", self.endTime) + } + } + throw Error("Expect options.pdfurl or options.data!") + } + }, + renderPdf: function (options, obj) { + this.container[0].pdfLoaded = true; + var self = this; + if (options.cMapUrl) { + obj.cMapUrl = options.cMapUrl; + } else { + obj.cMapUrl = './js/cmaps/'; + } + obj.cMapPacked = true; + self.loadingBar.show(); + self.progress.css({ + width: "3%" + }) + pdfjsLib.getDocument(obj).then(function (pdf) { + self.loading.hide() + self.thePDF = pdf; + self.totalNum = pdf.numPages; + self.pageTotal.text(self.totalNum) + var arr1 = self.eventType["ready"]; + if (arr1 && arr1 instanceof Array) { + for (var i = 0; i < arr1.length; i++) { + arr1[i] && arr1[i].call(self) + } + } + self.pinchZoom = new PinchZoom(self.viewer, { + tapZoomFactor: options.tapZoomFactor, + zoomOutFactor: options.zoomOutFactor, + animationDuration: options.animationDuration, + maxZoom: options.maxZoom, + minZoom: options.minZoom + }, self.viewerContainer); + self.pinchZoom.done = function (scale) { + if (scale == 1) { + if (self.viewerContainer) { + self.viewerContainer.css({ + '-webkit-overflow-scrolling': 'touch' + }) + } + + } else { + if (self.viewerContainer) { + self.viewerContainer.css({ + '-webkit-overflow-scrolling': 'auto' + }) + } + } + var arr1 = self.eventType["zoom"]; + if (arr1 && arr1 instanceof Array) { + for (var i = 0; i < arr1.length; i++) { + arr1[i] && arr1[i].call(self, scale) + } + } + } + if (options.zoomEnable) { + self.pinchZoom.enable() + } else { + self.pinchZoom.disable() + } + var promise = Promise.resolve(); + var num = Math.floor(100 / self.totalNum).toFixed(2); + for (var i = 1; i <= self.totalNum; i++) { + self.cache[i + ""] = { + page: null, + loaded: false, + container: null, + scaledViewport: null + }; + promise = promise.then(function (pageNum) { + return self.thePDF.getPage(pageNum).then(function (page) { + self.cache[pageNum + ""].page = page; + var viewport = page.getViewport(options.scale); + var scale = (self.docWidth / viewport.width).toFixed(2) + var scaledViewport = page.getViewport(parseFloat(scale)) + var container = document.createElement('div'); + container.id = 'pageContainer' + pageNum; + container.className = 'pageContainer'; + container.setAttribute('name', 'page=' + pageNum); + container.setAttribute('title', 'Page ' + pageNum); + var loadEffect = document.createElement('div'); + loadEffect.className = 'loadEffect'; + container.appendChild(loadEffect); + self.viewer[0].appendChild(container); + if (window.ActiveXObject || "ActiveXObject" in window) { + $(container).css({ + 'width': viewport.width + 'px', + "height": viewport.height + 'px' + }).attr("data-scale", viewport.width / viewport.height) + } else { + $(container).css({ + 'max-width': viewport.width, + "max-height": viewport.height, + "min-height": $(container).width() / (viewport.viewBox[2]/ viewport.viewBox[3]) + 'px' + }).attr("data-scale", viewport.width / viewport.height) + } + self.cache[pageNum + ""].container = container; + self.cache[pageNum + ""].scaledViewport = scaledViewport; + var sum = 0, containerH = self.container.height(); + self.pages = self.viewerContainer.find('.pageContainer'); + if (options.resize) { + self.resize() + } + if (self.pages && options.lazy) { + self.pages.each(function (index, obj) { + var top = obj.offsetTop; + if (top <= containerH) { + sum = index + 1; + self.cache[sum + ""].loaded = true; + } + }) + } + + if (pageNum > sum && options.lazy) { + return + } + if (options.renderType === "svg") { + return self.renderSvg(page, scaledViewport, pageNum, num, container, options, viewport) + } + return self.renderCanvas(page, scaledViewport, pageNum, num, container, options) + }); + }.bind(null, i)); + } + + }).catch(function (err) { + self.loading.hide(); + var time = new Date().getTime(); + self.endTime = time - self.initTime; + var arr1 = self.eventType["complete"]; + if (arr1 && arr1 instanceof Array) { + for (var i = 0; i < arr1.length; i++) { + arr1[i] && arr1[i].call(self, "error", err.message, self.endTime) + } + } + var arr2 = self.eventType["error"]; + if (arr2 && arr2 instanceof Array) { + for (var i = 0; i < arr2.length; i++) { + arr2[i] && arr2[i].call(self, err.message, self.endTime) + } + } + }) + }, + renderSvg: function (page, scaledViewport, pageNum, num, container, options, viewport) { + var self = this; + + return page.getOperatorList().then(function (opList) { + var svgGfx = new pdfjsLib.SVGGraphics(page.commonObjs, page.objs); + return svgGfx.getSVG(opList, scaledViewport).then(function (svg) { + self.loadedCount++; + container.children[0].style.display = "none"; + container.appendChild(svg); + svg.style.width = "100%"; + svg.style.height = "100%"; + self.progress.css({ + width: num * self.loadedCount + "%" + }) + if (window.ActiveXObject || "ActiveXObject" in window) { + + } else { + $(container).css({ + // "min-height": svg.getBBox().height + 'px' + }) + } + var time = new Date().getTime(); + var arr1 = self.eventType["render"]; + if (arr1 && arr1 instanceof Array) { + for (var i = 0; i < arr1.length; i++) { + arr1[i] && arr1[i].call(self, pageNum, time - self.initTime, container) + } + } + if (self.loadedCount === self.totalNum) { + self.finalRender(options) + } + }); + }); + }, + renderCanvas: function (page, viewport, pageNum, num, container, options) { + var self = this; + var viewport = page.getViewport(options.scale); + var scale = (self.docWidth / viewport.width).toFixed(2) + var canvas = document.createElement("canvas"); + var obj2 = { + 'Cheight': viewport.height * scale, + 'width': viewport.width, + 'height': viewport.height, + 'canvas': canvas, + 'index': self.loadedCount + } + var context = canvas.getContext('2d'); + canvas.height = viewport.height; + canvas.width = viewport.width; + + self.progress.css({ + width: num * self.loadedCount + "%" + }) + obj2.src = obj2.canvas.toDataURL("image/jpeg"); + //在canvas上绘制 + return page.render({ + canvasContext: context, + viewport: viewport + }).then(function () { + self.loadedCount++; + var img = new Image(); + var time = new Date().getTime(); + var time2 = 0; + if (self.renderTime == 0) { + time2 = time - self.startTime + } else { + time2 = time - self.renderTime + } + obj2.src = obj2.canvas.toDataURL("image/jpeg"); + img.src = obj2.src; + container.appendChild(img); + img.onload = function () { + $(container).css({ + "min-height": img.height + 'px' + }) + } + + var time = new Date().getTime(); + var arr1 = self.eventType["render"]; + if (arr1 && arr1 instanceof Array) { + for (var i = 0; i < arr1.length; i++) { + arr1[i] && arr1[i].call(self, pageNum, time - self.initTime, container) + } + } + if (self.loadedCount === self.totalNum) { + self.finalRender(options) + } + }) + }, + finalRender: function (options) { + var time = new Date().getTime(); + var self = this; + self.progress.css({ + width: "100%" + }); + setTimeout(function () { + self.loadingBar.hide(); + }, 300) + self.endTime = time - self.initTime; + if (options.renderType === "svg") { + if (self.totalNum !== 1) { + self.cache[(self.totalNum - 1) + ""].loaded = true; + } else { + self.cache["1"].loaded = true; + } + } + if (options.zoomEnable) { + if (self.pinchZoom) { + self.pinchZoom.enable() + } + } else { + if (self.pinchZoom) { + self.pinchZoom.disable() + } + } + var arr1 = self.eventType["complete"]; + if (arr1 && arr1 instanceof Array) { + for (var i = 0; i < arr1.length; i++) { + arr1[i] && arr1[i].call(self, "success", "pdf加载完成", self.endTime) + } + } + var arr2 = self.eventType["success"]; + if (arr2 && arr2 instanceof Array) { + for (var i = 0; i < arr2.length; i++) { + arr2[i] && arr2[i].call(self, self.endTime) + } + } + }, + resize: function () { + var self = this; + if (self.resizeEvent) { + return + } + self.resizeEvent = true; + var timer; + if (self.pages) { + self.getH() + $(window).on("resize", function () { + clearTimeout(timer) + timer = setTimeout(function () { + var winWidth = $(window).width(); + if (self.winWidth !== winWidth){ + self.pages.each(function (i, item) { + var w = $(item).width(), s = $(item).attr("data-scale"); + $(item).css({ + "min-height": w/s + 'px' + }) + }) + } + self.getH() + },300) + }) + } + }, + getH:function(){ + var self = this; + var num =0; + self.pages.each(function (i, item) { + var w = $(item).height(); + num + w + }) + $(".pinch-zoom-container").height(num) + }, + show: function (callback) { + this.container.show(); + callback && callback.call(this) + var arr = this.eventType["show"]; + if (arr && arr instanceof Array) { + for (var i = 0; i < arr.length; i++) { + arr[i] && arr[i].call(this) + } + } + }, + hide: function (callback) { + this.container.hide() + callback && callback.call(this) + var arr = this.eventType["hide"]; + if (arr && arr instanceof Array) { + for (var i = 0; i < arr.length; i++) { + arr[i] && arr[i].call(this) + } + } + }, + on: function (type, callback) { + if (this.eventType[type] && this.eventType[type] instanceof Array) { + this.eventType[type].push(callback) + } + this.eventType[type] = [callback] + }, + off: function (type) { + if (type !== undefined) { + this.eventType[type] = [null] + } else { + for (var i in this.eventType) { + this.eventType[i] = [null] + } + } + }, + scrollEnable: function (flag) { + if (flag === false) { + this.viewerContainer.css({ + "overflow": "hidden" + }) + } else { + this.viewerContainer.css({ + "overflow": "auto" + }) + } + var arr = this.eventType["scrollEnable"]; + if (arr && arr instanceof Array) { + for (var i = 0; i < arr.length; i++) { + arr[i] && arr[i].call(this, flag) + } + } + }, + zoomEnable: function (flag) { + if (flag === false) { + this.pinchZoom.disable() + } else { + this.pinchZoom.enable() + } + var arr = this.eventType["zoomEnable"]; + if (arr && arr instanceof Array) { + for (var i = 0; i < arr.length; i++) { + arr[i] && arr[i].call(this, flag) + } + } + }, + reset: function (callback) { + if (this.pinchZoom) { + this.pinchZoom.offset.y = 0; + this.pinchZoom.offset.x = 0; + this.pinchZoom.lastclientY = 0; + this.pinchZoom.zoomFactor = 1; + this.pinchZoom.update(); + } + if (this.viewerContainer) { + this.viewerContainer.scrollTop(0); + } + callback && callback.call(this) + var arr = this.eventType["reset"]; + if (arr && arr instanceof Array) { + for (var i = 0; i < arr.length; i++) { + arr[i] && arr[i].call(this) + } + } + }, + destroy: function (callback) { + this.reset(); + this.off(); + if (this.thePDF) { + this.thePDF.destroy(); + this.thePDF = null; + } + if (this.viewerContainer) { + this.viewerContainer.remove(); + this.viewerContainer = null; + } + if (this.container) { + this.container.html(''); + } + this.totalNum = null; + this.pages = null; + this.initTime = 0; + this.endTime = 0; + this.viewer = null; + this.pageNum = null; + this.pageNow = null; + this.pageTotal = null; + this.loadingBar = null; + this.progress = null; + this.loadedCount = 0; + this.timer = null; + this.show = null; + this.hide = null; + callback && callback.call(this) + var arr = this.eventType["destroy"]; + if (arr && arr instanceof Array) { + for (var i = 0; i < arr.length; i++) { + arr[i] && arr[i].call(this) + } + } + } + } + return Pdfh5; +}); \ No newline at end of file diff --git a/pdf.html b/pdf.html index 2790dbf..7c760bf 100644 --- a/pdf.html +++ b/pdf.html @@ -71,7 +71,8 @@ //当前默认优先获取浏览器地址栏?file=后面的地址,如果地址栏没有,再拿配置项的pdfurl或者data来渲染pdf //优先顺序: ?file= > pdfurl > data var pdfh5 = new Pdfh5('#demo', { - pdfurl: "default.pdf", + pdfurl: "https://www.gjtool.cn/pdfh5/git.pdf", + // pdfurl: "testPDF/1.pdf", // renderType:"canvas", // type:"ajax", lazy: true