swiperjs旋转处理

通过劫持swiper中touches对象,根据方向改变里面x和y值,完整的测试示例,如下:

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
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Swiper demo - default</title>
<meta
name="viewport"
content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1"
/>
<!-- Link Swiper's CSS -->
<link
rel="stylesheet"
href="https://unpkg.com/swiper@11.1.4/swiper-bundle.min.css"
/>
<!-- <link rel="stylesheet" href="style.css" /> -->
<style>
#rotateBtn {
position: absolute;
top: 0;
left: 0;
padding: 15px;
background: red;
z-index: 100;
}

.rotate-90 {
transform: rotate(90deg);
}
</style>
</head>

<body>
<!-- Swiper -->
<div id="chart">
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide">Resize me 1</div>
<div class="swiper-slide">Resize me 2</div>
<div class="swiper-slide">Resize me 3</div>
<div class="swiper-slide">Resize me 4</div>
<div class="swiper-slide">Resize me 5</div>
<div class="swiper-slide">Resize me 6</div>
<div class="swiper-slide">Resize me 7</div>
<div class="swiper-slide">Resize me 8</div>
<div class="swiper-slide">Resize me 9</div>
<div class="swiper-slide">Resize me 10</div>
</div>
<div class="swiper-button-next"></div>
<div class="swiper-button-prev"></div>
</div>
</div>
<div id="rotateBtn">Rotate 90</div>

<!-- Swiper JS -->
<script src="https://unpkg.com/swiper@11.1.4/swiper-bundle.min.js"></script>

<!-- Initialize Swiper -->
<script>
var swiper0 = new Swiper(".swiper-container", {
slidesPerView: 3,
direction: getDirection(),
navigation: {
nextEl: ".swiper-button-next",
prevEl: ".swiper-button-prev",
},
on: {
resize: function () {
swiper0.changeDirection(getDirection());
},
},
});

function getDirection() {
var windowWidth = window.innerWidth;
var direction = window.innerWidth <= 760 ? "vertical" : "horizontal";

return direction;
}

var rotateBtn = document.getElementById("rotateBtn");
rotateBtn.addEventListener("click", function f(params) {
console.log("rotateBtn click");
var chartRef = document.getElementById("chart");
chartRef.classList.add("rotate-90");
setRotate90(true);
});

var test = {
name: "huang",
};
var __test = {};
Object.defineProperty(test, "name", {
set: function (v) {
__test.name = v;
},
get: function () {
return __test.name;
},
});
console.log("test v", test.name);
test.name = "hzz";
console.log("test v", test.name);

var rotate90Swiper = function (swiper, optIsRotate90) {
var isRotate90 = !!optIsRotate90;
swiper.___touches = {};
swiper.___touches.currentX = swiper.touches.currentX;
Object.defineProperty(swiper.touches, "currentX", {
set: function (v) {
if (!isRotate90) {
swiper.___touches.currentX = v;
} else {
swiper.___touches.currentY = v;
}
},
get: function () {
return swiper.___touches.currentX;
},
});
swiper.___touches.currentY = swiper.touches.currentY;
Object.defineProperty(swiper.touches, "currentY", {
set: function (v) {
if (!isRotate90) {
swiper.___touches.currentY = v;
} else {
swiper.___touches.currentX = v;
}

console.log(
"set currentY",
swiper.touches,
swiper.___touches,
isRotate90
);
},
get: function () {
console.log(
"get currentY",
swiper.touches,
swiper.___touches,
isRotate90
);
return swiper.___touches.currentY;
},
});
return function (b) {
isRotate90 = b;
};
};

var setRotate90 = rotate90Swiper(swiper0, false);
</script>
<!-- <script src="script.js"></script> -->
</body>
</html>

发布于

2024-06-24

更新于

2024-06-24

许可协议

评论