forked from someoneoylp/vueDirect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.vue
160 lines (156 loc) · 5.29 KB
/
demo.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<template>
<div id="app">
<div class="modal" :style="{ width: modalWidth+'px', height: modalHeight + 'px' }">
<img :src="bigPic" class="modal_content"
v-touch:scaleTouch = "{func: scalePic, param: ''}"
v-touch:slideTouch = "{func: movePic, param: ''}"
:class="{'width80': bigPic ==='image_2.png'}"
/>
</div>
</div>
</template>
<script>
export default {
name: 'app',
data:function () {
return{
bigPic: '',
isShowBigpic: false,
modalWidth: 0,
modalHeight: 0,
baseLeft : 0,
baseTop: 0,
bodyWidth: document.body.clientWidth,
ele: null, // 不能在这里设置, dom还没有生成
}
},
methods:{
handleShowPic: function(picSrc){ // 显示图片
this.modalWidth = document.documentElement.clientWidth;
this.modalHeight = document.documentElement.clientHeight;
this.bigPic = picSrc;
this.isShowBigpic = true;
this.ele = document.getElementsByClassName('modal_content')[0];
document.addEventListener('touchmove', this.preHandler, {passive:false});
document.addEventListener('touchstart', this.preHandler, {passive:false});
},
handleCloseBigpic: function(){ // 恢复原状
this.isShowBigpic = false;
document.removeEventListener('touchmove',this.preHandler, {passive:false});
document.removeEventListener('touchstart',this.preHandler, {passive:false});
this.ele.style.margin = '0px';
this.ele.style.transform = 'translate(-50%, -50%) scale(1,1)';
},
scalePic: function(param, is_endScale){
var nodeStyle = this.ele.style.transform;
var changeSize = nodeStyle ? parseFloat(nodeStyle.slice(nodeStyle.indexOf('scale')+6)) : 0;
if(is_endScale){
// 缩放图片结束,要重新计算定位
this.setMaxdisp(changeSize,parseInt(this.ele.style.marginLeft), parseInt(this.ele.style.marginTop), 'scale')
return
}
if(nodeStyle){
// 如果存在的话,就说明已经设置了,scale已经改变了
var currScaleSize = changeSize + param;
currScaleSize > 3 ? currScaleSize = 3 : null
currScaleSize < 1 ? currScaleSize = 1 : null
this.ele.style.transform = 'translate(-50%, -50%) scale('+currScaleSize+','+currScaleSize+')';
}else{ // 如果不存在,就说明是第一次设置
var scale = param + 1
this.ele.style.marginLeft = '0px';
this.ele.style.marginTop = '0px';
this.ele.style.transform = 'translate(-50%, -50%) scale('+scale+','+scale+')';
}
},
movePic: function(param){
if(param.is_endMove){ // 每次移动松开手指的时候要下次移动的基础坐标
this.baseLeft = parseInt(this.ele.style.marginLeft.slice(0, -2));
this.baseTop = parseInt(this.ele.style.marginTop.slice(0, -2));
}else{
var nodeStyle = this.ele.style.transform
if(nodeStyle){ // 有这个就表示应该是移动
var currScaleSize = parseFloat(nodeStyle.slice(nodeStyle.indexOf('scale')+6))
this.setMaxdisp(currScaleSize,this.baseLeft+ param.x, this.baseTop + param.y, 'move')
}
}
},
setMaxdisp:function(changeSize, changeX, changeY, type){
// 计算最大位移
var picHeight = this.bodyWidth / (this.ele.naturalWidth / this.ele.naturalHeight);
var maxTop = ( picHeight * changeSize - window.innerHeight) /2
var maxLeft = this.bodyWidth / 2 * (changeSize - 1)
if(changeX >= maxLeft){
this.ele.style.marginLeft = maxLeft + 'px'
}else if(changeX < -maxLeft){
this.ele.style.marginLeft = -maxLeft + 'px'
}else if(type==='move'){
this.ele.style.marginLeft =changeX +'px';
}
// 如果图片当前尺寸大于屏幕尺寸,可以移动
if(maxTop > 0){
if(changeY >= maxTop){
this.ele.style.marginTop = maxTop + 'px';
}else if(changeY < -maxTop){
this.ele.style.marginTop = -maxTop + 'px'
}else if(type==='move'){
this.ele.style.marginTop = changeY+'px';
}
}else if(type==='move'){
this.ele.style.marginTop = 0 +'px';
}
},
preHandler:function(e){
e.preventDefault();
}
}
}
</script>
<style lang="scss" rel="stylesheet/scss">
@function count_font($n){
@return $n/2;
}
@function count_space($n){
@return $n/100;
}
#app{
position: relative;
margin: 0 count_space(40rem) count_space(59rem);
}
// 放大图片样式
.modal{
position: fixed;
top: 0;
left: 0;
overflow: hidden;
background: rgba(0, 0, 0, 1);
text-align: center;
.modal_content{
position: absolute;
width: 100%;
width: 88%;
top: 50%;
transform: translate(-50%, -50%);
margin-left: 0;
margin-top: 0;
}
.close_modal{
color: #ffffff;
font-size: 12px;
font-style: normal;
background: rgba(0, 0, 0, 0.6);
height: count_space(70rem);
z-index: 100;
position: relative;
text-align: right;
padding: 0 10px 0 0;
.close_icon{
border-radius: 50%;
width: count_space(40rem);
padding: count_space(16rem);
}
}
}
.width80{
width: 100% !important;
}
</style>