index-list.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <template>
  2. <view class="container">
  3. <view class="indexPosition">
  4. <view :class="[currentKey==item.name?'activeKey on':'activeKey']" v-for="(item,index) in keyList" @click="activeKeyHandle(item.name)">{{item.name}}
  5. </view>
  6. </view>
  7. <view v-if="ifShowKeyTip" :animation="animationData" class="pop">{{tipText}}</view>
  8. <scroll-view scroll-y="true" class="scroll-Y" :scroll-top="top" scroll-with-animation="true" @scroll="scroll">
  9. <view :class="[`box ${item.letter}`]" v-for="item in options">
  10. <view class="letter">{{item.letter}}</view>
  11. <view class="listWrap">
  12. <view class="list" v-for="val in item.data" @click="listClickHandle(val)">
  13. <view :class="[isSelectAll||checkedListIds.includes(val.id)?'iconWrap':'iconWrap on']">
  14. <image v-if="isSelectAll||checkedListIds.includes(val.id)" class="checkedIcon"
  15. src="../../static/check-checkbox.png" mode=""></image>
  16. </view>
  17. <text class="mainText">{{val.main}}</text>
  18. <text class="subText">{{val.sub}}</text>
  19. </view>
  20. </view>
  21. </view>
  22. </scroll-view>
  23. </view>
  24. </template>
  25. <script>
  26. export default {
  27. name: "index-list",
  28. props: {
  29. checkedResponsibleList: Array,
  30. options: Array,
  31. padding:Number,
  32. },
  33. data() {
  34. return {
  35. checkedList: [],
  36. checkedListIds: [],
  37. isSelectAll: false,
  38. keyList: [],
  39. top:0,
  40. scrollTop:0,
  41. currentKey:'',
  42. animationData:{},
  43. tipText:'',
  44. ifShowKeyTip:false
  45. };
  46. },
  47. computed: {
  48. },
  49. watch: {
  50. checkedResponsibleList: function(newVal, oldVal) {
  51. // console.log({newVal,oldVal});
  52. if (newVal.length != oldVal.length) {
  53. this.checkedListIds = JSON.parse(JSON.stringify(newVal)).map(item => item.id);
  54. this.checkedList = JSON.parse(JSON.stringify(newVal));
  55. this.$emit("listClick", this.checkedList);
  56. }
  57. },
  58. options:function(){
  59. this.initKeyIndex();
  60. this.top=0;
  61. // this.checkedList=[];
  62. // this.checkedListIds = JSON.parse(JSON.stringify(this.checkedResponsibleList)).map(item => item.id);
  63. // this.checkedList = JSON.parse(JSON.stringify(this.checkedResponsibleList));
  64. },
  65. },
  66. mounted() {
  67. this.checkedListIds = JSON.parse(JSON.stringify(this.checkedResponsibleList)).map(item => item.id);
  68. this.checkedList = JSON.parse(JSON.stringify(this.checkedResponsibleList));
  69. this.initKeyIndex();
  70. },
  71. methods: {
  72. showKeyTip(){
  73. var animation = uni.createAnimation({
  74. duration: 1000,
  75. timingFunction: 'ease',
  76. })
  77. this.animation = animation
  78. animation.opacity(1).step();
  79. this.animationData = animation.export();
  80. },
  81. initKeyIndex(){
  82. this.keyList = [];
  83. this.currentKey='';
  84. this.options.map(item=>{
  85. // console.log('item.letter',item.letter);
  86. if(!item.letter)return null;
  87. this.getDescBox(`.${item.letter}`).then(res=>{
  88. // console.log(res);
  89. this.keyList.push({
  90. name:item.letter,
  91. num:res
  92. });
  93. })
  94. });
  95. },
  96. scroll(event){
  97. const padding = this.padding;
  98. const {scrollLeft, scrollTop, scrollHeight, scrollWidth, deltaX, deltaY} = event.detail;
  99. this.scrollTop = scrollTop;
  100. // console.log(this.keyList);
  101. const tempArr = this.keyList.filter(item=>scrollTop>=item.num-padding);
  102. const needArr = tempArr.pop();
  103. // console.log({tempArr});
  104. this.currentKey = needArr?needArr.name:'';
  105. },
  106. activeKeyHandle(key) {
  107. const padding = this.padding;
  108. this.ifShowKeyTip=true;
  109. this.tipText = key.toUpperCase();
  110. this.getDescBox(`.${key.toLowerCase()}`).then(res=>{
  111. // console.log({res});
  112. // console.log(`.${key.toLowerCase()}`);
  113. if(!res||Math.abs(res)-padding<=0||res == this.top - padding)return;
  114. this.top = Math.abs(res+this.scrollTop-padding);
  115. });
  116. this.showKeyTip();
  117. setTimeout(()=>this.ifShowKeyTip = false,1000);
  118. },
  119. // 获取元素距离顶部的高度
  120. getDescBox(className) {
  121. let that = this;
  122. let count = 0;
  123. return new Promise(resolve => {
  124. getHeight();
  125. function getHeight() {
  126. uni
  127. .createSelectorQuery()
  128. .in(that)
  129. .select(className)
  130. .boundingClientRect()
  131. .exec(res => {
  132. if (res[0]) {
  133. resolve(res[0].top);
  134. } else {
  135. that.$nextTick(() => {
  136. ++count <= 8 ? getHeight() : resolve(null);
  137. });
  138. }
  139. });
  140. }
  141. });
  142. },
  143. listClickHandle(val) {
  144. const tempIdsArr = JSON.parse(JSON.stringify(this.checkedListIds));
  145. const tempArr = JSON.parse(JSON.stringify(this.checkedList));
  146. const data = JSON.parse(JSON.stringify(val));
  147. // console.log({tempIdsArr,tempArr});
  148. // console.log(tempIdsArr.includes(data.id));
  149. if (tempIdsArr.includes(data.id)) {
  150. this.isSelectAll = false;
  151. tempIdsArr.splice(tempIdsArr.indexOf(data.id), 1);
  152. this.checkedList = tempArr.filter(item => item.id != data.id);
  153. this.checkedListIds = tempIdsArr;
  154. } else {
  155. this.checkedList.push(data);
  156. this.checkedListIds.push(data.id);
  157. // console.log(this.checkedListIds);
  158. }
  159. this.$emit("listClick", this.checkedList);
  160. }
  161. }
  162. }
  163. </script>
  164. <style lang="less">
  165. .container {
  166. position: relative;
  167. z-index: 1;
  168. height: 100%;
  169. overflow: scroll;
  170. .indexPosition {
  171. display: flex;
  172. flex-direction: column;
  173. justify-content: center;
  174. align-items: center;
  175. position: absolute;
  176. z-index: 10;
  177. right: 12.5rpx;
  178. width: 50rpx;
  179. height: 100%;
  180. .activeKey {
  181. display: flex;
  182. justify-content: center;
  183. align-items: center;
  184. flex: 1;
  185. width: 100%;
  186. font-size: 17.5rpx;
  187. font-family: SourceHanSansCN-Normal, SourceHanSansCN;
  188. font-weight: 400;
  189. color: #7B8599;
  190. &.on {
  191. color: #3377FF;
  192. }
  193. }
  194. }
  195. .pop {
  196. position: fixed;
  197. z-index: 100;
  198. left:50%;
  199. top:50%;
  200. width: 125rpx;
  201. height: 125rpx;
  202. margin-left: -62.5rpx;
  203. margin-top: -62.5rpx;
  204. text-align: center;
  205. line-height:125rpx;
  206. font-size: 35rpx;
  207. color: #FFFFFF;
  208. border-radius: 50%;
  209. background: rgba(0,0,0,0.6);
  210. }
  211. .scroll-Y {
  212. height: 100%;
  213. .box {
  214. .letter {
  215. height: 62.5rpx;
  216. line-height: 62.5rpx;
  217. font-size: 22.5rpx;
  218. font-family: SourceHanSansCN-Normal, SourceHanSansCN;
  219. font-weight: 400;
  220. color: #666F80;
  221. padding: 0 25rpx;
  222. }
  223. .listWrap {
  224. background-color: #FFFFFF;
  225. .list {
  226. position: relative;
  227. display: flex;
  228. flex-direction: row;
  229. justify-content: flex-start;
  230. align-items: center;
  231. height: 87.5rpx;
  232. padding: 0 25rpx;
  233. .iconWrap {
  234. width: 25rpx;
  235. height: 25rpx;
  236. margin-right: 25rpx;
  237. .checkedIcon {
  238. width: 25rpx;
  239. height: 25rpx;
  240. }
  241. &.on {
  242. border-radius: 50%;
  243. border: 2.5rpx solid #C3CAD9;
  244. }
  245. }
  246. .mainText {
  247. display: inline-block;
  248. font-size: 22.5rpx;
  249. font-family: SourceHanSansCN-Normal, SourceHanSansCN;
  250. font-weight: 400;
  251. color: #292C33;
  252. margin-right: 50rpx;
  253. }
  254. .subText {
  255. font-size: 22.5rpx;
  256. font-family: SourceHanSansCN-Normal, SourceHanSansCN;
  257. font-weight: 400;
  258. color: #7A8499;
  259. }
  260. &::after {
  261. position: absolute;
  262. right: 0;
  263. bottom: 0;
  264. display: block;
  265. content: '';
  266. width: 90%;
  267. border-bottom: 1px solid #DADEE6;
  268. }
  269. &:last-child {
  270. &::after {
  271. display: none;
  272. }
  273. }
  274. }
  275. }
  276. }
  277. }
  278. }
  279. </style>