123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- <template>
- <view class="u-dropdown">
- <view class="u-dropdown__menu" :style="{
- height: $u.addUnit(height)
- }" :class="{
- 'u-border-bottom': borderBottom
- }">
- <view class="u-dropdown__menu__item" v-for="(item, index) in menuList" :key="index" @tap.stop="menuClick(index)">
- <view class="u-flex">
- <text class="u-dropdown__menu__item__text" :style="{
- color: item.disabled ? '#c0c4cc' : (index === current || highlightIndex == index) ? activeColor : inactiveColor,
- fontSize: $u.addUnit(titleSize)
- }">{{item.title}}</text>
- <view class="u-dropdown__menu__item__arrow" :class="{
- 'u-dropdown__menu__item__arrow--rotate': index === current
- }">
- <u-icon :custom-style="{display: 'flex'}" :name="menuIcon" :size="$u.addUnit(menuIconSize)" :color="index === current || highlightIndex == index ? activeColor : '#c0c4cc'"></u-icon>
- </view>
- </view>
- </view>
- </view>
- <view class="u-dropdown__content" :style="[contentStyle, {
- transition: `opacity ${duration / 1000}s linear`,
- top: $u.addUnit(height),
- height: contentHeight + 'px'
- }]"
- @tap="maskClick" @touchmove.stop.prevent>
- <view @tap.stop.prevent class="u-dropdown__content__popup" :style="[popupStyle]">
- <slot></slot>
- </view>
- <view class="u-dropdown__content__mask"></view>
- </view>
- </view>
- </template>
- <script>
-
- export default {
- name: 'u-dropdown',
- props: {
-
- activeColor: {
- type: String,
- default: '#2979ff'
- },
-
- inactiveColor: {
- type: String,
- default: '#606266'
- },
-
- closeOnClickMask: {
- type: Boolean,
- default: true
- },
-
- closeOnClickSelf: {
- type: Boolean,
- default: true
- },
-
- duration: {
- type: [Number, String],
- default: 300
- },
-
- height: {
- type: [Number, String],
- default: 80
- },
-
- borderBottom: {
- type: Boolean,
- default: false
- },
-
- titleSize: {
- type: [Number, String],
- default: 28
- },
-
- borderRadius: {
- type: [Number, String],
- default: 0
- },
-
- menuIcon: {
- type: String,
- default: 'arrow-down'
- },
-
- menuIconSize: {
- type: [Number, String],
- default: 26
- }
- },
- data() {
- return {
- showDropdown: true,
- menuList: [],
- active: false,
-
-
- current: 99999,
-
- contentStyle: {
- zIndex: -1,
- opacity: 0
- },
-
- highlightIndex: 99999,
- contentHeight: 0
- }
- },
- computed: {
-
- popupStyle() {
- let style = {};
-
- style.transform = `translateY(${this.active ? 0 : '-100%'})`
- style['transition-duration'] = this.duration / 1000 + 's';
- style.borderRadius = `0 0 ${this.$u.addUnit(this.borderRadius)} ${this.$u.addUnit(this.borderRadius)}`;
- return style;
- }
- },
- created() {
-
- this.children = [];
- },
- mounted() {
- this.getContentHeight();
- },
- methods: {
- init() {
-
-
- this.menuList = [];
- this.children.map(child => {
- child.init();
- })
- },
-
- menuClick(index) {
-
- if (this.menuList[index].disabled) return;
-
- if (index === this.current && this.closeOnClickSelf) {
- this.close();
-
- setTimeout(() => {
- this.children[index].active = false;
- }, this.duration)
- return;
- }
- this.open(index);
- },
-
- open(index) {
-
-
-
- this.contentStyle = {
- zIndex: 11,
- }
-
- this.active = true;
- this.current = index;
-
-
- this.children.map((val, idx) => {
- val.active = index == idx ? true : false;
- })
- this.$emit('open', this.current);
- },
-
- close() {
- this.$emit('close', this.current);
-
- this.active = false;
- this.current = 99999;
-
- this.contentStyle = {
- zIndex: -1,
- opacity: 0
- }
- },
-
- maskClick() {
-
- if (!this.closeOnClickMask) return;
- this.close();
- },
-
- highlight(index = undefined) {
- this.highlightIndex = index !== undefined ? index : 99999;
- },
-
- getContentHeight() {
-
-
-
- let windowHeight = this.$u.sys().windowHeight;
- this.$uGetRect('.u-dropdown__menu').then(res => {
-
-
-
-
- this.contentHeight = windowHeight - res.bottom;
- })
- }
- }
- }
- </script>
- <style scoped lang="scss">
- @import "../../libs/css/style.components.scss";
- .u-dropdown {
- flex: 1;
- width: 100%;
- position: relative;
- &__menu {
- @include vue-flex;
- position: relative;
- z-index: 11;
- height: 80rpx;
- &__item {
- flex: 1;
- @include vue-flex;
- justify-content: center;
- align-items: center;
- &__text {
- font-size: 28rpx;
- color: $u-content-color;
- }
- &__arrow {
- margin-left: 6rpx;
- transition: transform .3s;
- align-items: center;
- @include vue-flex;
- &--rotate {
- transform: rotate(180deg);
- }
- }
- }
- }
- &__content {
- position: absolute;
- z-index: 8;
- width: 100%;
- left: 0px;
- bottom: 0;
- overflow: hidden;
-
- &__mask {
- position: absolute;
- z-index: 9;
- background: rgba(0, 0, 0, .3);
- width: 100%;
- left: 0;
- top: 0;
- bottom: 0;
- }
- &__popup {
- position: relative;
- z-index: 10;
- transition: all 0.3s;
- transform: translate3D(0, -100%, 0);
- overflow: hidden;
- }
- }
- }
- </style>
|