生活随笔
收集整理的這篇文章主要介紹了
点击图片放大,实现移动端双指缩放,单指拖拽功能
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
記錄一下最近項(xiàng)目中用到的點(diǎn)擊圖片出現(xiàn)一個(gè)遮罩層,同時(shí)放大圖片顯示,移動(dòng)端可雙指縮放圖片并單指拖拽圖片.再次點(diǎn)擊遮罩層透明區(qū)域,遮罩層消失
一.實(shí)現(xiàn)效果:
頁面原圖展示:
點(diǎn)擊圖片后:
二.代碼實(shí)現(xiàn):
1.html部分:
下面貼出主要 代碼片段。
<div id="outerdiv" style="position: fixed; top: 0; left: 0; background: rgba(0, 0, 0, 0.7); z-index:2; width:100%;height:100%;display: none;"><div id="innerdiv" style="position: absolute;"><img id="bigimg" style="border: 5px solid #fff;" src="" /></div>
</div>
2.js部分(用到j(luò)Query,所以記得引入哦):
這部分代碼判斷是移動(dòng)端還是PC端,返回false為移動(dòng)端,true為PC端.用它返回的值來區(qū)別是否移動(dòng)端然后執(zhí)行之后移動(dòng)端事件.
下面展示一些 代碼片段。
$(function() {$(".main-newsdate img").click(function() {var _this
= $(this);imgShow("#outerdiv", "#innerdiv", "#bigimg", _this
);
var eleImg
= document
.querySelector('#innerdiv');var store
= {scale
: 1};var position_top
, position_left
,pageX
,pageY
;eleImg
.addEventListener('touchstart', function(event
) { event
.preventDefault();var touches
= event
.touches
;var events
= touches
[0];var events2
= touches
[1];if (touches
.length
== 1) { pageX
= Number(events
.pageX
);pageY
= Number(events
.pageY
);store
.moveable
= true;var _obj
= $('#innerdiv');position_left
= parseFloat(_obj
.css('left').split('px'));position_top
= parseFloat(_obj
.css('top').split('px'));} else {store
.pageX
= events
.pageX
;store
.pageY
= events
.pageY
;store
.moveable
= true;if (events2
) {store
.pageX2
= events2
.pageX
;store
.pageY2
= events2
.pageY
;}store
.originScale
= store
.scale
|| 1;}},{ passive
: false }); document
.addEventListener('touchmove', function(event
) {event
.preventDefault();if (!store
.moveable
) {return;}var touches
= event
.touches
;var events
= touches
[0];var events2
= touches
[1];if (touches
.length
== 1) {var pageX2
= Number(events
.pageX
);var pageY2
= Number(events
.pageY
);$('#innerdiv').css({'top': position_top
+ pageY2
- pageY
+ 'px',"left": position_left
+ pageX2
- pageX
+ 'px'})} else {if (events2
) {if (!store
.pageX2
) {store
.pageX2
= events2
.pageX
;}if (!store
.pageY2
) {store
.pageY2
= events2
.pageY
;}var getDistance = function(start
, stop
) {return Math
.hypot(stop
.x
- start
.x
,stop
.y
- start
.y
);};var zoom
= getDistance({x
: events
.pageX
,y
: events
.pageY
}, {x
: events2
.pageX
,y
: events2
.pageY
}) / getDistance({x
: store
.pageX
,y
: store
.pageY
}, {x
: store
.pageX2
,y
: store
.pageY2
});var newScale
= store
.originScale
* zoom
;if (newScale
> 3) {newScale
= 3;}store
.scale
= newScale
;eleImg
.style
.transform
= 'scale('+ newScale
+ ')';}}},{ passive
: false });document
.addEventListener('touchend', function() {store
.moveable
= false;delete store
.pageX2
;delete store
.pageY2
;});document
.addEventListener('touchcancel', function() {store
.moveable
= false;delete store
.pageX2
;delete store
.pageY2
;});});});function imgShow(outerdiv
, innerdiv
, bigimg
, _this
) {var flag
= IsPC();console
.log(flag
);var src
= _this
.attr("src");$(bigimg
).attr("src", src
);$("<img/>").attr("src", src
).load(function() {var windowW
= $(window
).width();var windowH
= $(window
).height();var realWidth
= this.width
;var realHeight
= this.height
;var imgWidth
, imgHeight
;var scale
= 0.8;if (realHeight
> windowH
* scale
) {imgHeight
= windowH
* scale
;imgWidth
= imgHeight
/ realHeight
* realWidth
;if (imgWidth
> windowW
* scale
) {imgWidth
= windowW
* scale
;}} else if (realWidth
> windowW
* scale
) {imgWidth
= windowW
* scale
;imgHeight
= imgWidth
/ realWidth
* realHeight
;} else {if (flag
== false) {imgWidth
= realWidth
;imgHeight
= realHeight
;} else if(realWidth
>=1000){ imgWidth
= realWidth
;imgHeight
= realHeight
;}else{imgWidth
= realWidth
*2 ;imgHeight
= realHeight
*2;}}$(bigimg
).css("width", imgWidth
);var w
= (windowW
- imgWidth
) / 2;var h
= (windowH
- imgHeight
) / 2;$(innerdiv
).css({"top" : h
,"left" : w
});$(outerdiv
).fadeIn("fast");});$(outerdiv
).click(function() {$(this).fadeOut("fast");});};
注意點(diǎn):
1.阻止默認(rèn)行為是配合passive使用,否則控制臺(tái)報(bào)錯(cuò)
默認(rèn)使用passive:true提高滾動(dòng)性能并減少崩潰,passive即順從的,是指它順從瀏覽器的默認(rèn)行為。設(shè)置該屬性的目的主要是為了在阻止事件默認(rèn)行為導(dǎo)致的卡頓。如果我們阻止了這一默認(rèn)行為,瀏覽器是無法預(yù)先知道的,必須等待事件監(jiān)聽器執(zhí)行完成后,才知道要去阻止默認(rèn)行為。等待監(jiān)聽器的執(zhí)行是耗時(shí)的,,有些甚至耗時(shí)很明顯,這樣就會(huì)導(dǎo)致頁面卡頓。即便監(jiān)聽器是個(gè)空函數(shù),也會(huì)產(chǎn)生一定的卡頓,畢竟空函數(shù)的執(zhí)行也會(huì)耗時(shí)。所以就有了passive屬性,如果要阻止默認(rèn)事件可以設(shè)置passive:false.
2…h(huán)eight()方法和.css(“height”)的區(qū)別:
返回值不同:.height()方法返回的是數(shù)字類型(20); .css(“height”)返回的是字符串類型(20px);
本文移動(dòng)端手指縮放方法方法主要參考大佬的博文:
https://www.zhangxinxu.com/wordpress/2020/06/mobile-event-touches-zoom-sacle/
總結(jié)
以上是生活随笔為你收集整理的点击图片放大,实现移动端双指缩放,单指拖拽功能的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。