1 +function ($) { "use strict";
2
3 /**
4 * The zoom service
5 */
6 function ZoomService () {
7 this._activeZoom =
8 this._initialScrollPosition =
9 this._initialTouchPosition =
10 this._touchMoveListener = null
11
12 this._$document = $(document)
13 this._$window = $(window)
14 this._$body = $(document.body)
15
16 this._boundClick = $.proxy(this._clickHandler, this)
17 }
18
19 ZoomService.prototype.listen = function () {
20 this._$body.on('click', '[data-action="zoom"]', $.proxy(this._zoom, this))
21 }
22
23 ZoomService.prototype._zoom = function (e) {
24
25 var target = e.target
26
27 if (!target || target.tagName != 'IMG') return
28
29 if (this._$body.hasClass('zoom-overlay-open')) return
30
31 if (e.metaKey || e.ctrlKey) {
32 return window.open((e.target.getAttribute('data-original') || e.target.src), '_blank')
33 }
34
35 // if (target.width >= ($(window).width() - Zoom.OFFSET)) return
36
37 this._activeZoomClose(true)
38
39 this._activeZoom = new Zoom(target)
40 this._activeZoom.zoomImage()
41
42 // todo(fat): probably worth throttling this
43 this._$window.on('scroll.zoom', $.proxy(this._scrollHandler, this))
44
45 this._$document.on('keyup.zoom', $.proxy(this._keyHandler, this))
46 this._$document.on('touchstart.zoom', $.proxy(this._touchStart, this))
47
48 // we use a capturing phase here to prevent unintended js events
49 // sadly no useCapture in jquery api (http://bugs.jquery.com/ticket/14953)
50 if (document.addEventListener) {
51 document.addEventListener('click', this._boundClick, true)
52 } else {
53 document.attachEvent('onclick', this._boundClick, true)
54 }
55
56 if ('bubbles' in e) {
57 if (e.bubbles) e.stopPropagation()
58 } else {
59 // Internet Explorer before version 9
60 e.cancelBubble = true
61 }
62 }
63
64 ZoomService.prototype._activeZoomClose = function (forceDispose) {
65 if (!this._activeZoom) return
66
67 if (forceDispose) {
68 this._activeZoom.dispose()
69 } else {
70 this._activeZoom.close()
71 }
72
73 this._$window.off('.zoom')
74 this._$document.off('.zoom')
75
76 document.removeEventListener('click', this._boundClick, true)
77
78 this._activeZoom = null
79 }
80
81 ZoomService.prototype._scrollHandler = function (e) {
82 if (this._initialScrollPosition === null) this._initialScrollPosition = $(window).scrollTop()
83 var deltaY = this._initialScrollPosition - $(window).scrollTop()
84 if (Math.abs(deltaY) >= 40) this._activeZoomClose()
85 }
86
87 ZoomService.prototype._keyHandler = function (e) {
88 if (e.keyCode == 27) this._activeZoomClose()
89 }
90
91 ZoomService.prototype._clickHandler = function (e) {
92 if (e.preventDefault) e.preventDefault()
93 else event.returnValue = false
94
95 if ('bubbles' in e) {
96 if (e.bubbles) e.stopPropagation()
97 } else {
98 // Internet Explorer before version 9
99 e.cancelBubble = true
100 }
101
102 this._activeZoomClose()
103 }
104
105 ZoomService.prototype._touchStart = function (e) {
106 this._initialTouchPosition = e.touches[0].pageY
107 $(e.target).on('touchmove.zoom', $.proxy(this._touchMove, this))
108 }
109
110 ZoomService.prototype._touchMove = function (e) {
111 if (Math.abs(e.touches[0].pageY - this._initialTouchPosition) > 10) {
112 this._activeZoomClose()
113 $(e.target).off('touchmove.zoom')
114 }
115 }
116
117
118 /**
119 * The zoom object
120 */
121 function Zoom (img) {
122 this._fullHeight =
123 this._fullWidth =
124 this._overlay =
125 this._targetImageWrap = null
126
127 this._targetImage = img
128
129 this._$body = $(document.body)
130 }
131
132 Zoom.OFFSET = 40
133 Zoom._MAX_WIDTH = 2560
134 Zoom._MAX_HEIGHT = 4096
135
136 Zoom.prototype.zoomImage = function () {
137 var img = document.createElement('img')
138 img.onload = $.proxy(function () {
139 this._fullHeight = Number(img.height)
140 this._fullWidth = Number(img.width)
141 this._zoomOriginal()
142 }, this)
143 img.src = this._targetImage.src
144 }
145
146 Zoom.prototype._zoomOriginal = function () {
147 this._targetImageWrap = document.createElement('div')
148 this._targetImageWrap.className = 'zoom-img-wrap'
149
150 this._targetImage.parentNode.insertBefore(this._targetImageWrap, this._targetImage)
151 this._targetImageWrap.appendChild(this._targetImage)
152
153 $(this._targetImage)
154 .addClass('zoom-img')
155 .attr('data-action', 'zoom-out')
156
157 this._overlay = document.createElement('div')
158 this._overlay.className = 'zoom-overlay'
159
160 document.body.appendChild(this._overlay)
161
162 this._calculateZoom()
163 this._triggerAnimation()
164 }
165
166 Zoom.prototype._calculateZoom = function () {
167 this._targetImage.offsetWidth // repaint before animating
168
169 var originalFullImageWidth = this._fullWidth
170 var originalFullImageHeight = this._fullHeight
171
172 var scrollTop = $(window).scrollTop()
173
174 var maxScaleFactor = originalFullImageWidth / this._targetImage.width
175
176 var viewportHeight = ($(window).height() - Zoom.OFFSET)
177 var viewportWidth = ($(window).width() - Zoom.OFFSET)
178
179 var imageAspectRatio = originalFullImageWidth / originalFullImageHeight
180 var viewportAspectRatio = viewportWidth / viewportHeight
181
182 if (originalFullImageWidth < viewportWidth && originalFullImageHeight < viewportHeight) {
183 this._imgScaleFactor = maxScaleFactor
184
185 } else if (imageAspectRatio < viewportAspectRatio) {
186 this._imgScaleFactor = (viewportHeight / originalFullImageHeight) * maxScaleFactor
187
188 } else {
189 this._imgScaleFactor = (viewportWidth / originalFullImageWidth) * maxScaleFactor
190 }
191 }
192
193 Zoom.prototype._triggerAnimation = function () {
194 console.log('放大的时候触发')
195 this._targetImage.offsetWidth // repaint before animating
196
197 var imageOffset = $(this._targetImage).offset()
198 var scrollTop = $(window).scrollTop()
199
200 var viewportY = scrollTop + ($(window).height() / 2)
201 var viewportX = ($(window).width() / 2)
202
203 var imageCenterY = imageOffset.top + (this._targetImage.height / 2)
204 var imageCenterX = imageOffset.left + (this._targetImage.width / 2)
205
206 this._translateY = viewportY - imageCenterY
207 this._translateX = viewportX - imageCenterX
208
209 var targetTransform = 'scale(' + this._imgScaleFactor + ')'
210 var imageWrapTransform = 'translate(' + this._translateX + 'px, ' + this._translateY + 'px)'
211
212 if ($.support.transition) {
213 imageWrapTransform += ' translateZ(0)'
214 }
215
216 $(this._targetImage)
217 .css({
218 '-webkit-transform': targetTransform,
219 '-ms-transform': targetTransform,
220 'transform': targetTransform
221 })
222
223 $(this._targetImageWrap)
224 .css({
225 '-webkit-transform': imageWrapTransform,
226 '-ms-transform': imageWrapTransform,
227 'transform': imageWrapTransform
228 })
229
230 this._$body.addClass('zoom-overlay-open')
231 }
232
233 Zoom.prototype.close = function () {
234 console.log('缩回的时候出发1');
235 this._$body
236 .removeClass('zoom-overlay-open')
237 .addClass('zoom-overlay-transitioning')
238
239 // we use setStyle here so that the correct vender prefix for transform is used
240 $(this._targetImage)
241 .css({
242 '-webkit-transform': '',
243 '-ms-transform': '',
244 'transform': ''
245 })
246
247 $(this._targetImageWrap)
248 .css({
249 '-webkit-transform': '',
250 '-ms-transform': '',
251 'transform': ''
252 })
253
254 if (!$.support.transition) {
255 return this.dispose()
256 }
257
258 $(this._targetImage)
259 .one($.support.transition.end, $.proxy(this.dispose, this))
260 .emulateTransitionEnd(300)
261 }
262
263 Zoom.prototype.dispose = function () {
264 console.log('缩回的时候出发2')
265 if (this._targetImageWrap && this._targetImageWrap.parentNode) {
266 $(this._targetImage)
267 .removeClass('zoom-img')
268 .attr('data-action', 'zoom')
269
270 this._targetImageWrap.parentNode.replaceChild(this._targetImage, this._targetImageWrap)
271 this._overlay.parentNode.removeChild(this._overlay)
272
273 this._$body.removeClass('zoom-overlay-transitioning')
274 }
275 }
276
277 // wait for dom ready (incase script included before body)
278 $(function () {
279 new ZoomService().listen()
280 })
281
282 }(jQuery)

图片放大缩小的zoom.js的更多相关文章

  1. 图片放大缩小插件 zoom.js 怎么用

    代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf ...

  2. vue项目 一行js代码搞定点击图片放大缩小

    一行js代码搞定xue项目需要点击图片放大缩小,其实主要用的是用到了vue:class的动态切换,内容比较简单.一开始我把维护的需求想得太复杂了,和测试小姐姐聊了一下才反应过来. 两个月不到跟了四个项 ...

  3. 鼠标滚轮图片放大缩小功能,使用layer弹框后不起作用

    今天在项目中遇到的一个问题:点击按钮使用layer弹框弹出一张图片,需要加一个鼠标滚轮放大缩小,图片也跟着放大缩小的功能.于是在网上找了一个demo. DEMO: <!DOCTYPE html ...

  4. javascript仿新浪微博图片放大缩小及旋转效果

    javascript仿新浪微博图片放大缩小及旋转效果 经常看到新浪微博里有图片放大缩小旋转效果,感觉效果还不错,所以就想试着做一个类似的demo出来,至于旋转对于IE可以用滤镜来解决,标准的浏览器可以 ...

  5. hammer使用: 代码:捏合、捏开、图片放大 的一个手机图片“放大缩小可拖动”的小效果

    hammer.js 的使用. (手机手势插件) 捏合.捏开.图片放大 的一个手机图片“放大缩小可拖动”的小效果. 相关js 到 http://www.bootcdn.cn/  查找和下载. hamme ...

  6. 自定义mousewheel事件,实现图片放大缩小功能实现

    本文是承接 上一篇的<自定义鼠标滚动事件>  的基础上实现的,建议大家先看一下上一篇的mousewheel的实现,再浏览下文: 上篇中我们介绍到: $element.mousewheel( ...

  7. imageView图片放大缩小及旋转

    imageView图片放大缩小及旋转 一.简介 二.方法 1)设置图片放大缩小效果 第一步:将<ImageView>标签中的android:scaleType设置为"fitCen ...

  8. wpf下的图片放大缩小

    WPF下实现图片的放大缩小移动   在windows 7里面有自带的图片查看器,这个软件可以打开一张图片然后以鼠标在图片中的焦点为原点来进行缩放,并且放大后可以随意拖动.下面我们在WPF中实现这个功能 ...

  9. 41.Android之图片放大缩小学习

    生活中经常会用到图片放大和缩小,今天简单学习下. 思路:1.添加一个操作图片放大和缩小类;  2. 布局文件中引用这个自定义控件;  3. 主Activity一些修改. 代码如下: 增加图片操作类: ...

随机推荐

  1. 使用pdfFactory隐藏文档中的隐私信息

    分享PDF文档时,文档中可能会存在一些隐私信息,比如用户名.用户的邮件地址.电话号码等信息.为了更好地保护原有文档内容的完整性,大家可以在生成PDF时,使用pdfFactory的隐藏信息功能,删除或遮 ...

  2. MAC系统网页链接如何下载

    Folx 5 是一款简单易用.功能强大的Mac OS系统的下载管理器.Folx界面简洁,下载管理方便,支持网页链接下载.BT下载和YouTube下载,而且还可以设置计划任务.搜索BT种子以及添加Tra ...

  3. JVM垃圾回收器、内存分配与回收策略

    新生代垃圾收集器 1. Serial收集器 serial收集器即串行收集器,是一个单线程收集器. 串行收集器在进行垃圾回收时只使用一个CPU或一条收集线程去完成垃圾回收工作,并且会暂停其他的工作线程( ...

  4. zabbix 监控ssh 登入与报警!!!!

    配置自定义键值 vim /etc/zabbix/zabbix_agentd.d/sanguo_check_ssh.conf 添加自定义键值 UserParameter=sanguo.check.ssh ...

  5. CSS3 学习笔记(中)

    七.文档流 文档流(normal flow)--网页的基础(最底下的一层),我们所创建的元素默认都是在文档流中进行排列. 对于元素有两个状态:在文档流 或 脱离文档流. 元素在文档流的特点: 块元素: ...

  6. rest-framework:权限组件

    一 权限简介 只用超级用户才能访问指定的数据,普通用户不能访问,所以就要有权限组件对其限制 二 局部使用 from rest_framework.permissions import BasePerm ...

  7. 去除openwrite.cn博客验证码限制

    相信有的小伙伴肯定遇到过如下这种情况,但是作为老白嫖党肯定是 「下次一定」 了,所以今天我们来看看如何不关注公众号实现 「阅读原文」. 如何解决呢? 1.通过 F12 打开控制台,切换至 Elemen ...

  8. WPF 学习笔记(一)

    一.概述 WPF(Windows Presentation Foundation)是微软推出的基于Windows 的用户界面框架,随着.NET Framework 3.0发布第一个版本.它提供了统一的 ...

  9. badboy下载

    最近新接触了badboy软件,以下是百度网盘链接,有需要可以下载. 链接:https://pan.baidu.com/s/1O4oIhx-twcaMA_fDzRQPHg提取码:7i44 二维码:

  10. 排序--MergeSort 归并排序?

    MergeSort 's implementation MergetSort 的中心思想就是分治思想,通过解决每一个小问题来解决大问题 假设你有2个已经排好序的数组 数组[ 4 ][ 8 ] 和 数组 ...