uniapp 仿照小米有品 轮播图组件(堆叠轮播组件)

短命女 2021-07-26 00:47 1583阅读 0赞

仿小米有品 轮播图组件

在这里插入图片描述
公司UI提供的精钢区的轮播图样式如上图所示:

本着简单高效的编程习惯,我打开了百度……

由于公司现在使用的是 uniapp,所以先从DCLOUD 插件市场查找。

找到一个堆叠轮播组件:

在这里插入图片描述
dcloud堆叠轮播组件:https://ext.dcloud.net.cn/plugin?id=1250

最终样式调整之后展示的效果如下图所示:
在这里插入图片描述
代码如下:

  1. //组件导入:
  2. import blackmonthSwiper from "@/components/blackmonth-swiper/index.vue"
  3. //组件注册:
  4. components:{
  5. blackmonthSwiper
  6. },
  7. //组件数据处理:
  8. swiperList: [{
  9. type: 'image',
  10. url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big84000.jpg'
  11. }, {
  12. type: 'image',
  13. url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big37006.jpg',
  14. }, {
  15. type: 'image',
  16. url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big39000.jpg'
  17. }, {
  18. type: 'image',
  19. url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big10001.jpg'
  20. }, {
  21. type: 'image',
  22. url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big25011.jpg'
  23. }, {
  24. type: 'image',
  25. url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big21016.jpg'
  26. }, {
  27. type: 'image',
  28. url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big99008.jpg'
  29. }]

轮播组件包含手动切换与自动轮播两种形式

  1. <template>
  2. <view>
  3. <view class="swiperPanel" ref="swiperPanel" @touchstart="startMove" @touchend="endMove">
  4. <view class="swiperItem" v-for="(item, index) in swiperList" :key="index" :style="{transform: itemStyle[index].transform, zIndex: itemStyle[index].zIndex, opacity: itemStyle[index].opacity}">
  5. <view class="children">
  6. <image class="pic" :src="item.url"></image>
  7. <view class="right">
  8. 撒发射点发射点发射点发射点发射点发射点
  9. </view>
  10. </view>
  11. </view>
  12. </view>
  13. </view>
  14. </template>
  15. <script>
  16. var timer;
  17. export default {
  18. props: {
  19. swiperList: {
  20. type: Array,
  21. default: []
  22. }
  23. },
  24. data() {
  25. return {
  26. slideNote: {
  27. x: 0,
  28. y: 0
  29. },
  30. screenWidth: 0,
  31. itemStyle: []
  32. };
  33. },
  34. created() {
  35. var macInfo = uni.getSystemInfoSync();
  36. this.screenWidth = macInfo.screenWidth;
  37. // 计算swiper样式
  38. this.swiperList.forEach((item, index) => {
  39. this.itemStyle.push(this.getStyle(index))
  40. })
  41. timer = setInterval(()=>{
  42. this.rightSlider();
  43. },3000)
  44. },
  45. methods: {
  46. rightSlider(){
  47. var newList = JSON.parse(JSON.stringify(this.itemStyle));
  48. var last = [newList.pop()]
  49. newList = last.concat(newList)
  50. this.itemStyle = newList;
  51. },
  52. getStyle(e) {
  53. if (e > this.swiperList.length / 2) {
  54. var right = this.swiperList.length - e
  55. return {
  56. transform: 'scale(' + (1 - right / 10) + ') translate(-' + (right * 9) + '%,0px)',
  57. zIndex: 9999 - right,
  58. opacity: 0.5 / right
  59. }
  60. } else {
  61. return {
  62. transform: 'scale(' + (1 - e / 10) + ') translate(' + (e * 9) + '%,0px)',
  63. zIndex: 9999 - e,
  64. opacity: 0.5 / e
  65. }
  66. }
  67. },
  68. startMove(e) {
  69. clearInterval(timer);
  70. this.slideNote.x = e.changedTouches[0] ? e.changedTouches[0].pageX : 0;
  71. this.slideNote.y = e.changedTouches[0] ? e.changedTouches[0].pageY : 0;
  72. },
  73. endMove(e) {
  74. timer = setInterval(()=>{
  75. this.rightSlider();
  76. },3000)
  77. var newList = JSON.parse(JSON.stringify(this.itemStyle));
  78. if ((e.changedTouches[0].pageX - this.slideNote.x) < 0) {
  79. // 向左滑动
  80. var last = [newList.pop()]
  81. newList = last.concat(newList)
  82. } else {
  83. // 向右滑动
  84. newList.push(newList[0])
  85. newList.splice(0, 1)
  86. }
  87. this.itemStyle = newList
  88. }
  89. }
  90. }
  91. </script>
  92. <style lang="scss">
  93. .swiperPanel {
  94. margin: 20rpx 0;
  95. height: 344rpx;
  96. width: 686upx;
  97. overflow: hidden;
  98. position: relative;
  99. margin-left:80upx;
  100. .swiperItem {
  101. height: 100%;
  102. width: 100%;
  103. position: absolute;
  104. top: 0;
  105. left: 0upx;
  106. transition: all .5s;
  107. .children {
  108. height: 100%;
  109. width: 570upx;
  110. margin: 2upx 160upx 2upx 0;
  111. position: relative;
  112. .right{
  113. position: absolute;
  114. right:0;
  115. top:0;
  116. width:50%;
  117. height:100%;
  118. background:#C29462;
  119. color:#fff;
  120. font-size:32upx;
  121. padding:10upx;
  122. box-sizing: border-box;
  123. border-radius: 0 10upx 10upx 0;
  124. }
  125. .pic {
  126. height: 100%;
  127. width: 100%;
  128. border-radius: 10upx;
  129. }
  130. }
  131. }
  132. }
  133. </style>

发表评论

表情:
评论列表 (有 0 条评论,1583人围观)

还没有评论,来说两句吧...

相关阅读

    相关 vue写组件

    写轮播组件的思路: 1、确定传入的参数:轮播图的数据(图片地址,跳转href,图片ID,标题等基础数据)、轮询时间 2、写好轮播图的基本样式,父组件传入参数,动态加载进

    相关 Vue——图片组件

    Notices: 这是我一个项目中的一个子组件,要展示的数据、图片地址等的都在父组件data中。所以后面的讲述都是基于从父组件获取的参数进行处理。(如需将这个SlideShow