gui-scrollitems.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <template>
  2. <view :style="{width:width+'rpx', overflow:'hidden'}">
  3. <gui-touch @thStart="thStart" @thMove="thMove" @thEnd="thEnd">
  4. <view class="gui-flex gui-rows gui-nowrap"
  5. :style="{
  6. width:wrapWidth+'px',
  7. transform:'translateX('+scLeft+'px)'
  8. }">
  9. <view class="gui-scrollitems gui-img-in"
  10. hover-class="gui-tap"
  11. :style="{
  12. width:itemWidth+'rpx',
  13. height:itemHeight+'rpx',
  14. marginRight:itemMargin+'rpx'}"
  15. v-for="(item, idx) in itemsIn" :key="idx"
  16. @tap="tapme(idx)">
  17. <image class="gui-scroll-image"
  18. :style="{
  19. width:itemWidth+'rpx',
  20. height:itemHeight+'rpx'}"
  21. :src="item.img"></image>
  22. <text
  23. class="gui-scrollitems-title gui-block-text gui-bg-black-opacity3 gui-border-box"
  24. :style="{width:width+'rpx'}">{{item.title}}</text>
  25. </view>
  26. </view>
  27. </gui-touch>
  28. </view>
  29. </template>
  30. <script>
  31. export default{
  32. name : "gui-scrollitems",
  33. props : {
  34. width : {type : Number, default:690},
  35. itemWidth : {type : Number, default:345},
  36. itemHeight : {type : Number, default:200},
  37. itemMargin : {type : Number, default:10},
  38. items : {type : Array, default:function(){return [];}},
  39. duration : {type : Number, default:25}
  40. },
  41. data() {
  42. return {
  43. scLeft : 0,
  44. resetWidth : 0,
  45. itemsIn : [],
  46. timer : null,
  47. timer2 : null,
  48. speed : 1,
  49. timer3 : null,
  50. wrapWidth : 5000,
  51. oX : 0
  52. }
  53. },
  54. created:function(){
  55. var len = this.items.length;
  56. this.itemsIn = this.items;
  57. this.resetWidth = len * (this.itemWidth + this.itemMargin);
  58. this.resetWidth = uni.upx2px(this.resetWidth);
  59. this.resetWidth *= -1;
  60. this.itemsIn = this.itemsIn.concat(this.items);
  61. this.wrapWidth = len * (this.itemWidth + this.itemMargin) * 2 + 80;
  62. this.scrollAnimate();
  63. },
  64. methods:{
  65. scrollAnimate : function () {
  66. if(this.scLeft <= this.resetWidth){
  67. this.scLeft = 0;
  68. this.timer = setTimeout(()=>{this.scrollAnimate()}, this.duration+200);
  69. }else{
  70. this.scLeft -= this.speed
  71. this.timer = setTimeout(()=>{this.scrollAnimate()}, this.duration);
  72. }
  73. },
  74. thStart : function(e){
  75. clearTimeout(this.timer);
  76. this.timer = null;
  77. this.oX = this.scLeft;
  78. },
  79. thMove : function (e){
  80. clearTimeout(this.timer);
  81. var tmpleft = this.oX + e[0][0];
  82. if(tmpleft < this.resetWidth){ tmpleft = this.resetWidth;}
  83. if(tmpleft > 0){tmpleft = 0}
  84. this.scLeft = tmpleft;
  85. },
  86. thEnd : function(e){
  87. this.timer = null
  88. if(this.timer3 != null){clearTimeout(this.timer3);}
  89. this.timer3 = setTimeout(()=>{
  90. this.scrollAnimate();
  91. }, 500);
  92. },
  93. tapme : function(idx){
  94. this.$emit('itemTap', this.itemsIn[idx]);
  95. }
  96. }
  97. }
  98. </script>
  99. <style scoped>
  100. .gui-scrollitems{overflow:hidden; position:relative;}
  101. .gui-scrollitems-title{position:absolute; z-index:1; left:0; bottom:0; height:44rpx; line-height:44rpx; padding:0 15rpx; overflow:hidden; font-size:22rpx; color:#FFFFFF;}
  102. </style>