swiper.uvue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view class="page-scroll-view">
  4. <!-- #endif -->
  5. <view class="uni-common-mb uni-common-pb">
  6. <page-head title="swiper,可滑动视图"></page-head>
  7. <view>
  8. <!-- 微信小程序自身Bug,autoplay为false时更新interval会导致swiper启用自动播放 -->
  9. <swiper id="swiper-view" class="swiper" :vertical="verticalSelect" :indicator-dots="dotsSelect"
  10. :autoplay="autoplaySelect" :bounces="reboundSelect" :interval="intervalSelect" :circular="circularSelect"
  11. :duration="durationSelect" :indicator-color="indicatorColor" :indicator-active-color="indicatorColorActive"
  12. :disable-touch="disableTouchSelect" :current="currentVal" :current-item-id="currentItemIdVal"
  13. @change="swiperChange" @transition="swiperTransition" @animationfinish="swiperAnimationfinish"
  14. @touchstart="swipertouchStart">
  15. <swiper-item item-id="A">
  16. <view class="swiper-item uni-bg-red"><text class="swiper-item-Text" @touchstart="viewtouchStart">A</text>
  17. </view>
  18. </swiper-item>
  19. <swiper-item item-id="B">
  20. <view class="swiper-item uni-bg-green"><text class="swiper-item-Text">B</text></view>
  21. </swiper-item>
  22. <swiper-item item-id="C">
  23. <view class="swiper-item uni-bg-blue"><text class="swiper-item-Text">C</text></view>
  24. </swiper-item>
  25. </swiper>
  26. </view>
  27. <view class="uni-list">
  28. <view class="uni-list-cell uni-list-cell-padding">
  29. <view class="uni-list-cell-db">显示面板指示点</view>
  30. <switch :checked="dotsSelect" @change="dotsChange" />
  31. </view>
  32. <view class="uni-list-cell uni-list-cell-padding">
  33. <view class="uni-list-cell-db">定制指示器颜色</view>
  34. <switch :checked="indicatorColorSelect" @change="indicatorColorChange" />
  35. </view>
  36. <view class="uni-list-cell uni-list-cell-padding">
  37. <view class="uni-list-cell-db">禁止 touch 操作</view>
  38. <switch :checked="disableTouchSelect" @change="disableTouchChange" />
  39. </view>
  40. <view class="uni-list-cell uni-list-cell-padding">
  41. <view class="uni-list-cell-db">是否自动切换</view>
  42. <switch :checked="autoplaySelect" @change="autoplayChange" />
  43. </view>
  44. <view class="uni-list-cell uni-list-cell-padding">
  45. <view class="uni-list-cell-db">是否衔接滑动</view>
  46. <switch :checked="circularSelect" @change="circularChange" />
  47. </view>
  48. <view class="uni-title uni-list-cell-padding">间隔时间(毫秒)</view>
  49. <view class="uni-padding-wrap">
  50. <slider @change="sliderChange" :value="2000" :min="500" :max="5000" :show-value="true" />
  51. </view>
  52. <view class="uni-title uni-list-cell-padding">动画时长(毫秒)</view>
  53. <view class="uni-padding-wrap">
  54. <slider @change="durationSliderChange" :value="500" :min="50" :max="2000" :show-value="true" />
  55. </view>
  56. <view class="uni-list-cell uni-list-cell-padding">
  57. <view class="uni-list-cell-db">是否纵向滑动</view>
  58. <switch :checked="verticalSelect" @change="verticalChange" />
  59. </view>
  60. <view class="uni-list-cell uni-list-cell-padding">
  61. <view class="uni-list-cell-db">是否回弹效果</view>
  62. <!-- 仅 android ios harmony 支持,web 微信小程序 bounces 为 true -->
  63. <switch :checked="reboundSelect" @change="reboundSelectChange" />
  64. </view>
  65. <view class="uni-list-cell uni-list-cell-padding">
  66. <view class="uni-list-cell-db">指定current为最后一个元素</view>
  67. <switch :checked="currentSelect" @change="currentChange" />
  68. </view>
  69. <view class="uni-list-cell uni-list-cell-padding">
  70. <view class="uni-list-cell-db">指定current-item-id为最后一个元素</view>
  71. <switch :checked="currentItemIdSelect" @change="currentItemIdChange" />
  72. </view>
  73. <view class="uni-list-cell uni-list-cell-padding">
  74. <view class="uni-list-cell-db">打印 swiperChange 日志</view>
  75. <switch :checked="swiperChangeSelect" @change="swiperChangeChange" />
  76. </view>
  77. <view class="uni-list-cell uni-list-cell-padding">
  78. <view class="uni-list-cell-db">打印 swiperTransition 日志</view>
  79. <switch :checked="swiperTransitionSelect" @change="swiperTransitionChange" />
  80. </view>
  81. <view class="uni-list-cell uni-list-cell-padding">
  82. <view class="uni-list-cell-db">打印 swiperAnimationfinish 日志</view>
  83. <switch :checked="swiperAnimationfinishSelect" @change="swiperAnimationfinishChange" />
  84. </view>
  85. <view class="uni-list-cell-padding">测试 swiper 默认行为</view>
  86. <swiper class="swiper" :autoplay="autoplayForDefault" :circular="circularForDefault">
  87. <swiper-item item-id="A">
  88. <view class="swiper-item uni-bg-red"><text class="swiper-item-Text">A</text></view>
  89. </swiper-item>
  90. <swiper-item item-id="B">
  91. <view class="swiper-item uni-bg-green"><text class="swiper-item-Text">B</text></view>
  92. </swiper-item>
  93. <swiper-item item-id="C">
  94. <view class="swiper-item uni-bg-blue"><text class="swiper-item-Text">C</text></view>
  95. </swiper-item>
  96. </swiper>
  97. <view class="uni-list-cell uni-list-cell-padding">
  98. <view class="uni-list-cell-db">是否自动切换</view>
  99. <switch :checked="autoplayForDefault" @change="() => {autoplayForDefault = !autoplayForDefault}" />
  100. </view>
  101. <view class="uni-list-cell uni-list-cell-padding">
  102. <view class="uni-list-cell-db">是否衔接滑动</view>
  103. <switch :checked="circularForDefault" @change="() => {circularForDefault = !circularForDefault}" />
  104. </view>
  105. <!-- #ifndef MP -->
  106. <navigator url="/pages/component/swiper/swiper-list-view">
  107. <button type="primary">
  108. swiper 嵌套 list-view 测试
  109. </button>
  110. </navigator>
  111. <navigator url="/pages/component/swiper/swiper-anim" style="margin-top: 10px;">
  112. <button type="primary">
  113. swiper 动画测试
  114. </button>
  115. </navigator>
  116. <!-- #endif -->
  117. </view>
  118. </view>
  119. <!-- #ifdef APP -->
  120. </scroll-view>
  121. <!-- #endif -->
  122. </template>
  123. <script>
  124. type SwiperEventTest = {
  125. type : string;
  126. target : UniElement | null;
  127. currentTarget : UniElement | null;
  128. }
  129. export default {
  130. data() {
  131. return {
  132. background: ['color1', 'color2', 'color3'],
  133. dotsSelect: false,
  134. reboundSelect: false,
  135. autoplaySelect: false,
  136. circularSelect: false,
  137. indicatorColorSelect: false,
  138. verticalSelect: false,
  139. currentSelect: false,
  140. currentItemIdSelect: false,
  141. intervalSelect: 2000,
  142. durationSelect: 500,
  143. indicatorColor: "",
  144. indicatorColorActive: "",
  145. currentVal: 0,
  146. currentItemIdVal: "",
  147. disableTouchSelect: false,
  148. swiperTransitionSelect: false,
  149. swiperAnimationfinishSelect: false,
  150. swiperChangeSelect: false,
  151. currentValChange: 0,
  152. autoplayForDefault: false,
  153. circularForDefault: false,
  154. // 自动化测试
  155. changeDetailTest: null as UniSwiperChangeEventDetail | null,
  156. transitionDetailTest: null as UniSwiperTransitionEventDetail | null,
  157. animationfinishDetailTest: null as UniSwiperAnimationFinishEventDetail | null,
  158. isChangeTest: '',
  159. isTransitionTest: '',
  160. isAnimationfinishTest: '',
  161. swipeX: 0,
  162. swipeY: 0
  163. }
  164. },
  165. onReady() {
  166. // #ifndef MP
  167. // 获取模拟滑动手势的起始点
  168. let ele = uni.getElementById("swiper-view")
  169. let eleRect = ele?.getBoundingClientRect()
  170. if (eleRect != null) {
  171. // 避开右侧边界,避免滑动行为响应为侧滑
  172. this.swipeX = eleRect.width - 40
  173. this.swipeY += eleRect.y + uni.getSystemInfoSync().safeArea.top + 44 + 35
  174. }
  175. // #endif
  176. },
  177. methods: {
  178. swipertouchStart(e : UniTouchEvent) {
  179. console.log("swiper touchstart")
  180. },
  181. viewtouchStart(e : UniTouchEvent) {
  182. console.log("view touchstart:")
  183. },
  184. swiperChange: function (e : UniSwiperChangeEvent) {
  185. this.changeDetailTest = e.detail
  186. this.checkEventTest({
  187. type: e.type,
  188. target: e.target,
  189. currentTarget: e.currentTarget
  190. } as SwiperEventTest, 'change')
  191. this.currentValChange = e.detail.current
  192. console.log(this.currentValChange)
  193. if (this.swiperChangeSelect) {
  194. console.log("swiperChange", e)
  195. }
  196. },
  197. swiperTransition: function (e : UniSwiperTransitionEvent) {
  198. this.transitionDetailTest = e.detail
  199. this.checkEventTest({
  200. type: e.type,
  201. target: e.target,
  202. currentTarget: e.currentTarget
  203. } as SwiperEventTest, 'transition')
  204. if (this.swiperTransitionSelect) {
  205. console.log("swiperTransition", e)
  206. }
  207. },
  208. swiperAnimationfinish: function (e : UniSwiperAnimationFinishEvent) {
  209. this.animationfinishDetailTest = e.detail
  210. this.checkEventTest({
  211. type: e.type,
  212. target: e.target,
  213. currentTarget: e.currentTarget
  214. } as SwiperEventTest, 'animationfinish')
  215. if (this.swiperAnimationfinishSelect) {
  216. console.log("swiperAnimationfinish", e)
  217. }
  218. },
  219. //自动化测试例专用
  220. jest_getSystemInfo() : GetSystemInfoResult {
  221. return uni.getSystemInfoSync();
  222. },
  223. // 自动化测试专用(由于事件event参数对象中存在循环引用,在ios端JSON.stringify报错,自动化测试无法page.data获取)
  224. checkEventTest(e : SwiperEventTest, eventName : String) {
  225. // #ifndef MP
  226. const isPass = e.type === eventName && e.target instanceof UniElement && e.currentTarget instanceof UniElement;
  227. // #endif
  228. // #ifdef MP
  229. const isPass = true;
  230. // #endif
  231. const result = isPass ? `${eventName}:Success` : `${eventName}:Fail`;
  232. switch (eventName) {
  233. case 'change':
  234. this.isChangeTest = result
  235. break;
  236. case 'transition':
  237. this.isTransitionTest = result
  238. break;
  239. case 'animationfinish':
  240. this.isAnimationfinishTest = result
  241. break;
  242. default:
  243. break;
  244. }
  245. },
  246. dotsChange: function (e : UniSwitchChangeEvent) {
  247. this.dotsSelect = e.detail.value
  248. },
  249. swiperTransitionChange: function (e : UniSwitchChangeEvent) {
  250. this.swiperTransitionSelect = e.detail.value
  251. },
  252. swiperChangeChange: function (e : UniSwitchChangeEvent) {
  253. this.swiperChangeSelect = e.detail.value
  254. },
  255. swiperAnimationfinishChange: function (e : UniSwitchChangeEvent) {
  256. this.swiperAnimationfinishSelect = e.detail.value
  257. },
  258. autoplayChange: function (e : UniSwitchChangeEvent) {
  259. this.autoplaySelect = e.detail.value
  260. },
  261. verticalChange: function (e : UniSwitchChangeEvent) {
  262. this.verticalSelect = e.detail.value
  263. },
  264. disableTouchChange: function (e : UniSwitchChangeEvent) {
  265. this.disableTouchSelect = e.detail.value
  266. },
  267. currentItemIdChange: function (e : UniSwitchChangeEvent) {
  268. this.currentItemIdSelect = e.detail.value
  269. if (this.currentItemIdSelect) {
  270. this.currentItemIdVal = 'C'
  271. } else {
  272. this.currentItemIdVal = 'A'
  273. }
  274. },
  275. currentChange: function (e : UniSwitchChangeEvent) {
  276. this.currentSelect = e.detail.value
  277. if (this.currentSelect) {
  278. this.currentVal = 2
  279. } else {
  280. this.currentVal = 0
  281. }
  282. },
  283. circularChange: function (e : UniSwitchChangeEvent) {
  284. this.circularSelect = e.detail.value
  285. console.log(this.circularSelect)
  286. },
  287. reboundSelectChange: function (e : UniSwitchChangeEvent) {
  288. this.reboundSelect = e.detail.value
  289. console.log(this.reboundSelect)
  290. },
  291. sliderChange(e : UniSliderChangeEvent) {
  292. this.intervalSelect = e.detail.value
  293. },
  294. durationSliderChange(e : UniSliderChangeEvent) {
  295. this.durationSelect = e.detail.value
  296. },
  297. indicatorColorChange(e : UniSwitchChangeEvent) {
  298. this.indicatorColorSelect = e.detail.value
  299. if (this.indicatorColorSelect) {
  300. // 选择了定制指示器颜色
  301. this.indicatorColor = "#ff00ff"
  302. this.indicatorColorActive = "#0000ff"
  303. } else {
  304. // 没有选择颜色
  305. this.indicatorColor = ""
  306. this.indicatorColorActive = ""
  307. }
  308. }
  309. }
  310. }
  311. </script>
  312. <style>
  313. .swiper {
  314. height: 150px;
  315. }
  316. .swiper-item {
  317. width: 100%;
  318. height: 150px;
  319. }
  320. .swiper-item-Text {
  321. width: 100%;
  322. text-align: center;
  323. line-height: 150px;
  324. }
  325. </style>