uni-pagination.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <template>
  2. <view class="uni-pagination">
  3. <view class="uni-pagination__btns">
  4. <view :class="['uni-pagination__btn',{'uni-pagination--disabled':currentIndex === 1}]" :hover-class="currentIndex === 1 ? '' : 'uni-pagination--hover'" :hover-start-time="20" :hover-stay-time="70" @click="clickLeft">
  5. <template v-if="showIcon">
  6. <uni-icon color="#000" size="20" type="arrowleft" />
  7. </template>
  8. <template v-else>
  9. {{ prevText }}
  10. </template>
  11. </view>
  12. <view :class="['uni-pagination__btn',{'uni-pagination--disabled':currentIndex === maxPage}]" :hover-class="currentIndex === maxPage ? '' : 'uni-pagination--hover'" :hover-start-time="20" :hover-stay-time="70" @click="clickRight">
  13. <template v-if="showIcon">
  14. <uni-icon color="#000" size="20" type="arrowright" />
  15. </template>
  16. <template v-else>
  17. {{ nextText }}
  18. </template>
  19. </view>
  20. </view>
  21. <view class="uni-pagination__num">
  22. <text class="uni-pagination__num-current">{{ currentIndex }}</text>/{{ maxPage }}
  23. </view>
  24. </view>
  25. </template>
  26. <script>
  27. import uniIcon from '../uni-icon/uni-icon.vue'
  28. export default {
  29. name: 'UniPagination',
  30. components: {
  31. uniIcon
  32. },
  33. props: {
  34. prevText: {
  35. type: String,
  36. default: '上一页'
  37. },
  38. nextText: {
  39. type: String,
  40. default: '下一页'
  41. },
  42. current: {
  43. type: [Number, String],
  44. default: 1
  45. },
  46. total: { // 数据总量
  47. type: [Number, String],
  48. default: 0
  49. },
  50. pageSize: { // 每页数据量
  51. type: [Number, String],
  52. default: 10
  53. },
  54. showIcon: { // 是否以 icon 形式展示按钮
  55. type: Boolean,
  56. default: false
  57. }
  58. },
  59. data() {
  60. return {
  61. currentIndex: 1
  62. }
  63. },
  64. computed: {
  65. maxPage() {
  66. let maxPage = 1
  67. let total = Number(this.total)
  68. let pageSize = Number(this.pageSize)
  69. if (total && pageSize) {
  70. maxPage = Math.ceil(total / pageSize)
  71. }
  72. return maxPage
  73. }
  74. },
  75. watch: {
  76. current(val) {
  77. this.currentIndex = +val
  78. }
  79. },
  80. created() {
  81. this.currentIndex = +this.current
  82. },
  83. methods: {
  84. clickLeft() {
  85. if (Number(this.currentIndex) === 1) {
  86. return
  87. }
  88. this.currentIndex -= 1
  89. this.change('prev')
  90. },
  91. clickRight() {
  92. if (Number(this.currentIndex) === this.maxPage) {
  93. return
  94. }
  95. this.currentIndex += 1
  96. this.change('next')
  97. },
  98. change(e) {
  99. this.$emit('change', {
  100. type: e,
  101. current: this.currentIndex
  102. })
  103. }
  104. }
  105. }
  106. </script>
  107. <style>
  108. @charset "UTF-8";
  109. .uni-pagination {
  110. width: 100%;
  111. box-sizing: border-box;
  112. padding: 0 40upx;
  113. position: relative;
  114. overflow: hidden;
  115. display: flex;
  116. flex-direction: row
  117. }
  118. .uni-pagination__btns {
  119. flex: 1;
  120. display: flex;
  121. justify-content: space-between;
  122. align-items: center;
  123. flex-direction: row
  124. }
  125. .uni-pagination__btn {
  126. width: 120upx;
  127. height: 60upx;
  128. padding: 0 16upx;
  129. line-height: 60upx;
  130. font-size: 28upx;
  131. box-sizing: border-box;
  132. position: relative;
  133. background-color: #f8f8f8;
  134. display: flex;
  135. flex-direction: row;
  136. justify-content: center;
  137. align-items: center
  138. }
  139. .uni-pagination__btn:after {
  140. content: "";
  141. width: 200%;
  142. height: 200%;
  143. position: absolute;
  144. top: 0;
  145. left: 0;
  146. border: 1px solid #c8c7cc;
  147. transform: scale(.5);
  148. transform-origin: 0 0;
  149. box-sizing: border-box;
  150. border-radius: 12upx
  151. }
  152. .uni-pagination__num {
  153. width: 100upx;
  154. height: 60upx;
  155. line-height: 60upx;
  156. font-size: 28upx;
  157. color: #333;
  158. position: absolute;
  159. left: 50%;
  160. top: 0;
  161. transform: translateX(-50%)
  162. }
  163. .uni-pagination__num-current {
  164. color: #007aff
  165. }
  166. .uni-pagination--disabled {
  167. opacity: .3
  168. }
  169. .uni-pagination--hover {
  170. color: rgba(0, 0, 0, .6);
  171. background-color: #f1f1f1
  172. }
  173. </style>