chapterCatalog.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <template>
  2. <uni-popup @maskClick="emits('Close')" :animation="false" mask-background-color="rgba(0,0,0,0)" ref="catalog" background-color="#fff" @change="onChange">
  3. <view
  4. :class="{ container0: background === 1, container1: background === 2 }"
  5. @touchend.stop>
  6. <view>
  7. <!-- <movable-area>
  8. <movable-view class="action-bar-box" direction="vertical" @change="change" :y="y" :animation="false">
  9. <view style="border-bottom: #000 solid 2px;width: 100%;"></view>
  10. <view style="border-bottom: #000 solid 2px;width: 100%;"></view>
  11. </movable-view>
  12. </movable-area> -->
  13. <scroll-view
  14. scroll-y="true"
  15. :style="{
  16. height: scrollHeight + 'px',
  17. position: 'relative',
  18. zIndex: 1
  19. }"
  20. @scroll="handleScroll"
  21. :scroll-top="scrollTop"
  22. :show-scrollbar="false"
  23. >
  24. <!-- <view
  25. class="scroll-bar"
  26. :style="{
  27. height: localHeight + 'px'
  28. }"
  29. ></view> -->
  30. <view
  31. class="list"
  32. :style="{
  33. transform: `translateY(${offset}px)`
  34. }"
  35. >
  36. <view class="item-wrap" v-for="(item, index) in visibleData" :key="item.id">
  37. <!-- <slot :item="item" :active="active"></slot> -->
  38. <view class="directory-listItem" @click="clickChar(item)">{{ item.name }}</view>
  39. </view>
  40. </view>
  41. </scroll-view>
  42. </view>
  43. </view>
  44. </uni-popup>
  45. </template>
  46. <script setup lang="ts">
  47. import { ref,onMounted,computed} from 'vue';
  48. // const {items,remain,size,active,scrollHeight} = defineProps(['items','remain','size','active','scrollHeight'])
  49. const {items,remain,size,scrollHeight} =defineProps({
  50. items: {
  51. type: Array,
  52. required: true,
  53. default: []
  54. },
  55. remain: {
  56. type: Number,
  57. required: true,
  58. default: 0
  59. },
  60. size: {
  61. type: Number,
  62. required: true,
  63. default: 0
  64. },
  65. // active: {
  66. // type: Number,
  67. // required: true,
  68. // default: 0
  69. // },
  70. scrollHeight: {
  71. type: Number,
  72. required: true,
  73. default: 0
  74. },
  75. });
  76. const emits = defineEmits(['clickChar','Close'])
  77. let start = ref(0)
  78. let end = ref(0)
  79. let offset = ref(0)
  80. let scrollTop = ref(0)
  81. let y = ref(0)
  82. let background = ref(1)
  83. const catalog = ref(null)
  84. onMounted(()=>{
  85. const type = 'bottom'
  86. // open 方法传入参数 等同在 uni-popup 组件上绑定 type属性
  87. catalog.value.open(type)
  88. })
  89. let preCount = computed(()=>{
  90. return Math.min(start.value, remain);
  91. })
  92. let nextCount = computed(()=>{
  93. return Math.min(items.length - end.value, remain);
  94. })
  95. let visibleData = computed(()=>{
  96. const t_start = start.value - preCount.value;
  97. const t_end = end.value + nextCount.value;
  98. return items.slice(t_start, t_end);
  99. })
  100. let localHeight = computed(()=>{
  101. return items.length * size;
  102. })
  103. function clickChar(item){
  104. emits('clickChar',item)
  105. }
  106. function change(e){
  107. if (e.detail.source !== 'touch') {
  108. return;
  109. }
  110. let y = e.detail.y;
  111. let scroll = (y / (scrollHeight - 40)) * (localHeight.value - scrollHeight);
  112. scroll = scroll < 0 ? 0 : scroll;
  113. scrollTop.value = scroll;
  114. }
  115. function handleScroll(ev){
  116. const scrollTop = ev.detail.scrollTop;
  117. y.value = (scrollTop / (localHeight.value - scrollHeight)) * (scrollHeight - 40);
  118. // 开始位置
  119. const t_start = Math.floor(scrollTop / size);
  120. start.value = start.value < 0 ? 0 : t_start;
  121. // 结束位置
  122. end.value = t_start +remain;
  123. // 计算偏移
  124. const t_offset = scrollTop - (scrollTop % size) - preCount.value * size;
  125. offset.value = t_offset < 0 ? 0 : t_offset;
  126. }
  127. //
  128. function onChange(e){
  129. if(e.show==false){
  130. emits('Close')
  131. }
  132. }
  133. </script>
  134. <style scoped>
  135. .list {
  136. position: absolute;
  137. top: 0;
  138. left: 0;
  139. width: 100%;
  140. }
  141. .action-bar-box {
  142. padding: 3px;
  143. display: flex;
  144. flex-flow: column;
  145. justify-content: space-around;
  146. align-items: center;
  147. position: absolute;
  148. right: 0;
  149. background-color: transparent;
  150. border-radius: 10rpx;
  151. box-shadow: 0 0 5px #000;
  152. width: 20px;
  153. height: 40px;
  154. z-index: 2;
  155. }
  156. .directory-listItem {
  157. display: flex;
  158. align-items: center;
  159. padding-left: 10px;
  160. height: 40px;
  161. font-size: 14px;
  162. border-bottom: #eee solid 1px;
  163. }
  164. .active {
  165. color: red;
  166. }
  167. .container0 {
  168. background: url('../../static/imgs/read/background1.jpg');
  169. background-color: #fff;
  170. background-size: 100% 100%;
  171. }
  172. .container1 {
  173. background-color: #000;
  174. }
  175. /* .directory {
  176. position: fixed;
  177. display: flex;
  178. flex-flow: column;
  179. width: 100%;
  180. height: 30%;
  181. } */
  182. .active {
  183. color: red;
  184. }
  185. .scroll-view {
  186. scrollbar-width: none; /* 对于微信小程序等平台,可以使用scrollbar-width设置为none */
  187. }
  188. .scroll-view {
  189. over-scroll: false;
  190. }
  191. .scroll-view::-webkit-scrollbar {
  192. display: none; /* 隐藏滚动条 */
  193. }
  194. </style>