actionSheet.vue 7.9 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3
<template>
  <uni-actionsheet @touchmove.prevent>
    <transition name="uni-fade">
4 5
      <div
        v-show="visible"
6 7
        class="uni-mask uni-actionsheet__mask"
        @click="_close(-1)"
fxy060608's avatar
fxy060608 已提交
8
      />
fxy060608's avatar
fxy060608 已提交
9
    </transition>
10
    <div
11
      :class="{ 'uni-actionsheet_toggle': visible }"
12
      :style="popupStyle.content"
13 14
      class="uni-actionsheet"
    >
15 16 17 18 19 20 21 22 23 24 25
      <div
        ref="main"
        class="uni-actionsheet__menu"
        @wheel="_handleWheel"
      >
        <!-- title占位 -->
        <div
          v-if="title"
          class="uni-actionsheet__cell"
          :style="{height:`${titleHeight}px`}"
        />
26 27 28
        <div
          v-if="title"
          class="uni-actionsheet__title"
29 30
        >
          {{ title }}
fxy060608's avatar
fxy060608 已提交
31
        </div>
32
        <div
33
          :style="{maxHeight:`${HEIGHT}px`,overflow:'hidden'}"
34
        >
35 36 37 38 39 40 41 42 43 44 45 46 47
          <div
            ref="content"
          >
            <div
              v-for="(itemTitle, index) in itemList"
              :key="index"
              :style="{ color: itemColor }"
              class="uni-actionsheet__cell"
              @click="_close(index)"
            >
              {{ itemTitle }}
            </div>
          </div>
fxy060608's avatar
fxy060608 已提交
48
        </div>
fxy060608's avatar
fxy060608 已提交
49 50
      </div>
      <div class="uni-actionsheet__action">
51
        <div
52
          :style="{ color: itemColor }"
53
          class="uni-actionsheet__cell"
54 55
          @click="_close(-1)"
        >
fxy060608's avatar
fxy060608 已提交
56
          {{ $$t('uni.showActionSheet.cancel') }}
fxy060608's avatar
fxy060608 已提交
57
        </div>
fxy060608's avatar
fxy060608 已提交
58
      </div>
59
      <div :style="popupStyle.triangle" />
fxy060608's avatar
fxy060608 已提交
60
    </div>
61 62 63
    <keypress
      :disable="!visible"
      @esc="_close(-1)"
64
    />
fxy060608's avatar
fxy060608 已提交
65 66 67
  </uni-actionsheet>
</template>
<script>
68
import popup from './mixins/popup'
fxy060608's avatar
fxy060608 已提交
69 70
import keypress from '../../../helpers/keypress'
import i18n from 'uni-mixins/i18n'
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
import touchtrack from 'uni-mixins/touchtrack'
import scroller from 'uni-mixins/scroller/index'
import {
  Friction
} from 'uni-mixins/scroller/Friction'
import {
  Spring
} from 'uni-mixins/scroller/Spring'
import {
  initScrollBounce,
  disableScrollBounce
} from 'uni-platform/helpers/scroll'

// 由于模拟滚动阻止了点击,使用自定义事件来触发点击事件
function initClick (dom) {
  const MAX_MOVE = 20
  let x = 0
  let y = 0
  dom.addEventListener('touchstart', (event) => {
    const info = event.changedTouches[0]
    x = info.clientX
    y = info.clientY
  })
  dom.addEventListener('touchend', (event) => {
    const info = event.changedTouches[0]
    if (Math.abs(info.clientX - x) < MAX_MOVE && Math.abs(info.clientY - y) < MAX_MOVE) {
      const customEvent = new CustomEvent('click', {
        bubbles: true,
        cancelable: true,
        target: event.target,
        currentTarget: event.currentTarget
      });
      ['screenX', 'screenY', 'clientX', 'clientY', 'pageX', 'pageY'].forEach(key => {
        customEvent[key] = info[key]
      })
      event.target.dispatchEvent(customEvent)
    }
  })
}
110

fxy060608's avatar
fxy060608 已提交
111 112
export default {
  name: 'ActionSheet',
113
  components: { keypress },
fxy060608's avatar
fxy060608 已提交
114
  mixins: [i18n, popup, touchtrack, scroller],
Q
qiang 已提交
115 116 117 118
  props: {
    title: {
      type: String,
      default: ''
119
    },
fxy060608's avatar
fxy060608 已提交
120 121 122 123 124 125 126 127 128 129
    itemList: {
      type: Array,
      default () {
        return []
      }
    },
    itemColor: {
      type: String,
      default: '#000000'
    },
130 131 132 133
    popover: {
      type: Object,
      default: null
    },
fxy060608's avatar
fxy060608 已提交
134 135 136 137 138
    visible: {
      type: Boolean,
      default: false
    }
  },
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
  data () {
    return {
      HEIGHT: 260,
      contentHeight: 0,
      titleHeight: 0,
      deltaY: 0,
      scrollTop: 0
    }
  },
  watch: {
    visible (newValue) {
      if (newValue) {
        this.$nextTick(() => {
          // title 占位
          if (this.title) { this.titleHeight = document.querySelector('.uni-actionsheet__title').offsetHeight }
          // 滚动条更新
          this._scroller.update()
          // 获取contentHeight 滚动时使用
          this.contentHeight = this.$refs.content.clientHeight - this.HEIGHT
          // 给每一个项添加点击事件
          document.querySelectorAll('.uni-actionsheet__cell').forEach(item => {
            initClick(item)
          })
        })
      }
    }
  },
  mounted () {
    // 模拟滚动使用
    this.touchtrack(this.$refs.content, '_handleTrack', true)
    this.$nextTick(() => {
      this.initScroller(this.$refs.content, {
        enableY: true,
        friction: new Friction(0.0001),
        spring: new Spring(2, 90, 20),
        onScroll: (e) => {
          this.scrollTop = e.target.scrollTop
        }
      })
    })
    initScrollBounce()
  },
fxy060608's avatar
fxy060608 已提交
181 182 183
  methods: {
    _close (tapIndex) {
      this.$emit('close', tapIndex)
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
    },
    _handleTrack: function (e) {
      if (this._scroller) {
        switch (e.detail.state) {
          case 'start':
            this._handleTouchStart(e)
            disableScrollBounce({
              disable: true
            })
            break
          case 'move':
            this._handleTouchMove(e)
            break
          case 'end':
          case 'cancel':
            this._handleTouchEnd(e)
            disableScrollBounce({
              disable: false
            })
        }
      }
    },
    _handleWheel ($event) {
      const deltaY = this.deltaY + $event.deltaY
      if (Math.abs(deltaY) > 10) {
        this.scrollTop += deltaY / 3
        this.scrollTop = this.scrollTop >= this.contentHeight
          ? this.contentHeight
          : this.scrollTop <= 0
            ? 0
            : this.scrollTop
        this._scroller.scrollTo(this.scrollTop)
      } else {
        this.deltaY = deltaY
      }
      $event.preventDefault()
fxy060608's avatar
fxy060608 已提交
220 221 222 223 224
    }
  }
}
</script>
<style>
225 226 227 228
uni-actionsheet {
  display: block;
  box-sizing: border-box;
}
fxy060608's avatar
fxy060608 已提交
229

230 231 232 233 234 235 236 237 238 239 240
uni-actionsheet .uni-actionsheet {
  position: fixed;
  left: 6px;
  right: 6px;
  bottom: 6px;
  transform: translate(0, 100%);
  backface-visibility: hidden;
  z-index: 999;
  visibility: hidden;
  transition: transform 0.3s, visibility 0.3s;
}
fxy060608's avatar
fxy060608 已提交
241

242 243 244 245
uni-actionsheet .uni-actionsheet.uni-actionsheet_toggle {
  visibility: visible;
  transform: translate(0, 0);
}
fxy060608's avatar
fxy060608 已提交
246

247 248 249
uni-actionsheet .uni-actionsheet * {
  box-sizing: border-box;
}
fxy060608's avatar
fxy060608 已提交
250

251 252 253 254 255
uni-actionsheet .uni-actionsheet__menu,
uni-actionsheet .uni-actionsheet__action {
  border-radius: 5px;
  background-color: #fcfcfd;
}
fxy060608's avatar
fxy060608 已提交
256

257 258 259 260 261 262 263 264 265 266 267 268
uni-actionsheet .uni-actionsheet__action {
  margin-top: 6px;
}

uni-actionsheet .uni-actionsheet__cell,
uni-actionsheet .uni-actionsheet__title {
  position: relative;
  padding: 10px 6px;
  text-align: center;
  font-size: 18px;
  text-overflow: ellipsis;
  overflow: hidden;
269
  cursor: pointer;
270
}
fxy060608's avatar
fxy060608 已提交
271

272 273 274 275 276 277 278 279 280 281 282
uni-actionsheet .uni-actionsheet__title{
    position: absolute;
    top: 0;
    right: 0;
    left: 0;
    z-index: 1;
    background-color: #fff;
    border-radius: 5px 5px 0 0;
    border-bottom: 1px solid #e5e5e5;
}

283 284 285 286 287 288 289 290 291 292 293 294
uni-actionsheet .uni-actionsheet__cell:before {
  content: " ";
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  height: 1px;
  border-top: 1px solid #e5e5e5;
  color: #e5e5e5;
  transform-origin: 0 0;
  transform: scaleY(0.5);
}
fxy060608's avatar
fxy060608 已提交
295

296 297 298 299 300 301 302
uni-actionsheet .uni-actionsheet__cell:active {
  background-color: #ececec;
}

uni-actionsheet .uni-actionsheet__cell:first-child:before {
  display: none;
}
fxy060608's avatar
fxy060608 已提交
303

304
@media screen and (min-width: 500px) and (min-height: 500px) {
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
  .uni-mask.uni-actionsheet__mask {
    background: none;
  }
  uni-actionsheet .uni-actionsheet {
    width: 300px;
    left: 50%;
    right: auto;
    top: 50%;
    bottom: auto;
    transform: translate(-50%, -50%);
    opacity: 0;
    transition: opacity 0.3s, visibility 0.3s;
  }
  uni-actionsheet .uni-actionsheet.uni-actionsheet_toggle {
    opacity: 1;
    transform: translate(-50%, -50%);
  }
  uni-actionsheet .uni-actionsheet__menu {
    box-shadow: 0px 0 20px 5px rgba(0, 0, 0, 0.3);
  }
  uni-actionsheet .uni-actionsheet__action {
    display: none;
  }
}
329
</style>