mosowe-swiper.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <template>
  2. <view class="mosowe-swiper" style="'height:240rpx'">
  3. <swiper
  4. class="uni-swiper"
  5. :indicator-dots="indicator === 'dots'"
  6. :indicator-color="indicatorColor"
  7. :indicator-active-color="indicatorActiveColor"
  8. :autoplay="autoplay"
  9. :current="active"
  10. :interval="interval"
  11. :duration="duration"
  12. :vertical="vertical"
  13. :disable-touch="touchable"
  14. :previous-margin="canPyramid ? pyramidMargin : ''"
  15. :next-margin="canPyramid ? pyramidMargin : ''"
  16. :display-multiple-items="canPyramid ? 1 : itemNums"
  17. circular
  18. @change="bannerChange"
  19. >
  20. <!-- 非微信小程序 -->
  21. <!-- #ifndef MP-WEIXIN -->
  22. <swiper-item
  23. class="item"
  24. v-for="(item,index) of lists"
  25. :key="index"
  26. :class="canPyramid && active !== index ? 'swiper-pyramid' : canPyramid && active === index ? 'swiper-active' : ''"
  27. >
  28. <!-- 图文左右轮播 -->
  29. <view class="imageTextLine">
  30. <view class="user">
  31. <image class="avatar" :src="item[imageKey]" />
  32. <text class="content">{{item[textKey]}}<span class="l">{{item[time]}}前</span>预览了</text>
  33. </view>
  34. <view class="shop">
  35. <image class="avatar" :src="item[imageshop]" />
  36. <text class="content">{{item[name]}}</text>
  37. </view>
  38. </view>
  39. </swiper-item>
  40. <!-- #endif -->
  41. </swiper>
  42. </view>
  43. </template>
  44. <script>
  45. /*
  46. 待增加:
  47. 1. dot自定义功能,加入slot插槽;
  48. 2. 考虑下nvue
  49. */
  50. export default {
  51. name: 'mosowe-swiper',
  52. components: {},
  53. props: {
  54. current: { // 当前展示的索引
  55. type: Number,
  56. default: 0
  57. },
  58. autoplay: { // 是否自动切换
  59. type: Boolean,
  60. default: true
  61. },
  62. interval: { // 自动切换时间间隔
  63. type: Number,
  64. default: 3000
  65. },
  66. duration: { // 切换动画时长
  67. type: Number,
  68. default: 500
  69. },
  70. vertical: { // 滑动方向是否为纵向
  71. type: Boolean,
  72. default: false
  73. },
  74. indicator: { // 指示点样式:dots点,number数字右下角,空则不会显示
  75. type: String,
  76. default: ''
  77. },
  78. indicatorColor: { // dot点样式:默认颜色
  79. type: String,
  80. default: 'rgba(255, 255, 255, 0.5)'
  81. },
  82. indicatorActiveColor: { // dot点选中样式:高亮颜色
  83. type: String,
  84. default: '#ffffff'
  85. },
  86. scene: { // 场景值
  87. type: String,
  88. default: ''
  89. },
  90. touchable: { // 是否禁用手动滑动
  91. type: Boolean,
  92. default: false
  93. },
  94. lists: { // 轮播列表
  95. type: Array,
  96. default: () => {
  97. return [];
  98. }
  99. },
  100. swiperType: { // 轮播类型:image图片轮播,imageTextLine图文一行轮播,text文本轮播
  101. type: String,
  102. default: 'image'
  103. },
  104. previewImage: { // 开启图片预览
  105. type: Boolean,
  106. default: false
  107. },
  108. imageKey: { // 图片的key值,重复使用的组件可能遇到不同的key,此处传图片的key
  109. type: String,
  110. default: ''
  111. },
  112. imageshop: { // 商品图片
  113. type: String,
  114. default: ''
  115. },
  116. name: { // 商品名字
  117. type: String,
  118. default: ''
  119. },
  120. textKey: { // 文本的key值,重复使用的组件可能遇到不同的key,此处传文本的key
  121. type: String,
  122. default: ''
  123. },
  124. time: {//时间
  125. type: String,
  126. default: ''
  127. },
  128. height: { // 轮播区的高度,单位rpx
  129. type: Number,
  130. default: 300
  131. },
  132. pyramid: { // 金字塔式,横向且纯图模式有效,开启金字塔模式时,active初始化最少为1,最大为this.lists.length -2
  133. type: Boolean,
  134. default: false
  135. },
  136. pyramidMargin: { // 金字塔式,前后露出的距离,单位rpx,px
  137. type: String,
  138. default: '30rpx'
  139. },
  140. itemNums: { // 同时展示个数,开启金字塔模式时, itemNums = 1
  141. type: String,
  142. default: '1'
  143. }
  144. },
  145. data () {
  146. return {
  147. active: 0,
  148. activePrev: -1,
  149. activeNext: -1,
  150. canPyramid: false,
  151. touchStartTime: 0, // 微信小程序端:触摸事件判断点击或滑动
  152. };
  153. },
  154. created () {
  155. this.active = this.current;
  156. if (this.pyramid && this.swiperType === 'image' && !this.vertical) {
  157. this.canPyramid = true;
  158. if (this.active === 0 || this.active < 0 || this.active > this.lists.length - 1) {
  159. this.lists.length ? '' : this.active = 1;
  160. } else if (this.active === this.lists.length - 1) {
  161. this.active = this.lists.length -2;
  162. }
  163. }
  164. },
  165. methods: {
  166. // 微信小程序:banner触摸时,禁止手动滑动的时候触发
  167. WXAPP_bannerTouch () {
  168. if(this.previewImage) {
  169. this.touchStartTime = new Date().getTime();
  170. }
  171. },
  172. // 微信小程序:触摸完
  173. WXAPP_bannerTouchEnd (item) {
  174. let t = new Date().getTime();
  175. if (t-this.touchStartTime <= 200) { // 点击
  176. this.bannerClick(item);
  177. } else {
  178. if (this.touchable) {
  179. return false;
  180. }
  181. }
  182. },
  183. // banner轮播时
  184. bannerChange (e) {
  185. this.active = e.detail.current;
  186. this.$emit('change', e.detail.current);
  187. },
  188. // banner点击时
  189. bannerClick (item) {
  190. console.log(item);
  191. if (this.swiperType === 'image' && this.previewImage) { // 纯图片模式下,开启预览模式
  192. let urls = [];
  193. if (this.imageKey) {
  194. for (let image of this.lists) {
  195. urls.push(image[this.imageKey]);
  196. }
  197. } else {
  198. urls = this.lists;
  199. }
  200. console.log(urls);
  201. uni.previewImage({
  202. current: item.index,
  203. urls: urls
  204. });
  205. }
  206. this.$emit('bclick', item);
  207. },
  208. }
  209. };
  210. </script>
  211. <style lang="scss">
  212. .shop{
  213. display: flex;
  214. }
  215. .user .content{
  216. margin-bottom:10rpx !important;
  217. }
  218. .shop .content{
  219. margin-top: 18rpx;
  220. }
  221. .imageTextLine{
  222. margin-left: 40rpx;
  223. }
  224. .imageTextLine image{
  225. width: 50rpx;
  226. }
  227. .l{
  228. color:#d8ad77;
  229. }
  230. .mosowe-swiper{
  231. width: 100%;
  232. position: relative;
  233. .uni-swiper {
  234. height: inherit;
  235. .uni-swiper-dot{
  236. width: 20px !important;
  237. height: 8px !important;
  238. border-radius: 4px;
  239. }
  240. .item {
  241. box-sizing: border-box;
  242. .image{
  243. width: 100%;
  244. height: 100%;
  245. }
  246. // 支付宝
  247. /* #ifdef MP-ALIPAY */
  248. &.swiper-pyramid{
  249. padding: 0 30rpx;
  250. }
  251. /* #endif */
  252. // 非支付宝
  253. /* #ifndef MP-ALIPAY */
  254. &.swiper-pyramid{
  255. padding: 30rpx;
  256. }
  257. &.swiper-prev {
  258. animation: prev .5s forwards;
  259. }
  260. &.swiper-next {
  261. animation: next .5s forwards;
  262. }
  263. &.swiper-active {
  264. animation: actives .5s forwards;
  265. }
  266. /* #endif */
  267. }
  268. }
  269. // 纯图
  270. .image{
  271. width: 100%;
  272. height: 100%;
  273. }
  274. // 纯文
  275. .text{
  276. overflow: hidden;
  277. text-overflow: ellipsis;
  278. white-space: nowrap;
  279. width: 100%;
  280. display: block;
  281. }
  282. // 图文一行
  283. .imageTextLine {
  284. height: 240rpx !important;
  285. overflow: hidden;
  286. width: 100%;
  287. align-items: center;
  288. .avatar{
  289. flex: 0 0 50rpx;
  290. height: 50rpx;
  291. border-radius: 50%;
  292. margin-right: 10rpx;
  293. }
  294. .content{
  295. width: 260rpx !important;
  296. // flex: 1;
  297. white-space: nowrap;
  298. text-overflow: ellipsis;
  299. overflow: hidden;
  300. }
  301. }
  302. .custom-indicator{
  303. position: absolute;
  304. right: 30rpx;
  305. bottom: 30rpx;
  306. background: rgba(0, 0, 0, 0.2);
  307. width: 80rpx;
  308. height: 40rpx;
  309. line-height: 40rpx;
  310. font-size: 24rpx;
  311. color: #fff;
  312. border-radius: 30rpx;
  313. text-align: center;
  314. }
  315. }
  316. @keyframes prev {
  317. 0%{
  318. padding: 0;
  319. }
  320. 100% {
  321. padding: 30rpx;
  322. }
  323. }
  324. @keyframes next {
  325. 0%{
  326. padding: 0;
  327. }
  328. 100% {
  329. padding: 30rpx;
  330. }
  331. }
  332. @keyframes actives {
  333. 0%{
  334. padding: 30rpx;
  335. }
  336. 100% {
  337. padding: 0;
  338. }
  339. }
  340. </style>