gui-scrollitems.vue 3.0 KB

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