123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- <template>
- <view class="tn-scroll-list-class tn-scroll-list">
- <scroll-view
- class="tn-scroll-list__scroll-view"
- scroll-x
- :upper-threshold="0"
- :lower-threshold="0"
- @scroll="scrollHandler"
- @scrolltoupper="scrollToUpperHandler"
- @scrolltolower="scrollToLowerHandler"
- >
- <view class="tn-scroll-list__scroll-view__content">
- <slot></slot>
- </view>
- </scroll-view>
-
-
- <view
- v-if="indicator"
- class="tn-scroll-list__indicator"
- :style="[indicatorStyle]"
- >
- <view class="tn-scroll-list__indicator__line" :style="[lineStyle]">
- <view class="tn-scroll-list__indicator__line__bar" :style="[barStyle]"></view>
- </view>
- </view>
- </view>
- </template>
- <script>
- export default {
- name: 'tn-scroll-list',
- props: {
-
- indicator: {
- type: Boolean,
- default: true
- },
-
- indicatorWidth: {
- type: [String, Number],
- default: 50
- },
-
- indicatorBarWidth: {
- type: [String, Number],
- default: 20
- },
-
- indicatorColor: {
- type: String,
- default: '#E6E6E6'
- },
-
- indicatorActiveColor: {
- type: String,
- default: '#01BEFF'
- },
-
- indicatorStyle: {
- type: Object,
- default() {
- return {}
- }
- }
- },
- computed: {
-
- barStyle() {
- let style = {}
-
-
- const scrollLeft = this.scrollInfo.scrollLeft,
- scrollWidth = this.scrollInfo.scrollWidth,
- barAllMoveWidth = uni.upx2px(this.indicatorWidth) - uni.upx2px(this.indicatorBarWidth);
- const x = scrollLeft / (scrollWidth - this.scrollWidth) * barAllMoveWidth
- style.transform = `translateX(${x}px)`
-
- style.width = `${this.indicatorBarWidth}rpx`
- style.backgroundColor = this.indicatorActiveColor
- return style
- },
-
- lineStyle() {
- let style = {}
- style.width = `${this.indicatorWidth}rpx`
- style.backgroundColor = this.indicatorColor
- return style
- }
- },
- data() {
- return {
-
- scrollInfo: {
- scrollLeft: 0,
- scrollWidth: 0
- },
-
- scrollWidth: 0
- }
- },
- mounted() {
- this.$nextTick(() => {
- this.init()
- })
- },
- methods: {
-
- init() {
- this.getComponentWidth()
- },
-
- scrollHandler(event) {
- this.scrollInfo = event.detail
- },
-
- scrollToUpperHandler() {
- this.$emit('left')
- this.scrollInfo.scrollLeft = 0
- },
-
- scrollToLowerHandler() {
- this.$emit('right')
-
- },
-
- getComponentWidth() {
- this._tGetRect('.tn-scroll-list').then(res => {
- if (!res) {
- setTimeout(() => {
- this.getComponentWidth()
- }, 10)
- return
- }
- this.scrollWidth = res.width
- })
- }
- }
- }
- </script>
- <style lang="scss" scoped>
-
- .tn-scroll-list {
- padding-bottom: 20rpx;
-
- &__scroll-view {
- display: flex;
- flex-direction: row;
-
- &__content {
- display: flex;
- flex-direction: row;
- }
- }
-
- &__indicator {
- display: flex;
- flex-direction: row;
- justify-content: center;
- margin-top: 30rpx;
-
- &__line {
- width: 120rpx;
- height: 8rpx;
- border-radius: 200rpx;
- overflow: hidden;
-
- &__bar {
- width: 40rpx;
- height: 8rpx;
- border-radius: 200rpx;
- }
- }
- }
- }
- </style>
|