nvue.js 1.2 KB

12345678910111213141516171819202122232425262728
  1. // 引入bindingx,此库类似于微信小程序wxs,目的是让js运行在视图层,减少视图层和逻辑层的通信折损
  2. const BindingX = uni.requireNativePlugin('bindingx')
  3. import { os } from '../../libs/function/index';
  4. export default {
  5. methods: {
  6. // 此处不写注释,请自行体会
  7. nvueScrollHandler(e) {
  8. const anchor = this.$refs['u-scroll-list__scroll-view'].ref
  9. const element = this.$refs['u-scroll-list__indicator__line__bar'].ref
  10. const scrollLeft = e.contentOffset.x
  11. const contentSize = e.contentSize.width
  12. const { scrollWidth } = this
  13. const barAllMoveWidth = this.indicatorWidth - this.indicatorBarWidth
  14. // 在安卓和iOS上,需要除的倍数不一样,iOS需要除以2
  15. const actionNum = os() === 'ios' ? 2 : 1
  16. const expression = `(x / ${actionNum}) / ${contentSize - scrollWidth} * ${barAllMoveWidth}`
  17. BindingX.bind({
  18. anchor,
  19. eventType: 'scroll',
  20. props: [{
  21. element,
  22. property: 'transform.translateX',
  23. expression
  24. }]
  25. })
  26. }
  27. }
  28. }