25d218a0a23ff93bd8df69a10c8a189bed40cff6.svn-base 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <template>
  2. <el-scrollbar ref="scrollContainer" :vertical="false" class="scroll-container" @wheel.native.prevent="handleScroll">
  3. <slot />
  4. </el-scrollbar>
  5. </template>
  6. <script>
  7. const tagAndTagSpacing = 4 // tagAndTagSpacing
  8. export default {
  9. name: 'ScrollPane',
  10. data() {
  11. return {
  12. left: 0
  13. }
  14. },
  15. computed: {
  16. scrollWrapper() {
  17. return this.$refs.scrollContainer.$refs.wrap
  18. }
  19. },
  20. methods: {
  21. handleScroll(e) {
  22. const eventDelta = e.wheelDelta || -e.deltaY * 40
  23. const $scrollWrapper = this.scrollWrapper
  24. $scrollWrapper.scrollLeft = $scrollWrapper.scrollLeft + eventDelta / 4
  25. },
  26. moveToTarget(currentTag) {
  27. const $container = this.$refs.scrollContainer.$el
  28. const $containerWidth = $container.offsetWidth
  29. const $scrollWrapper = this.scrollWrapper
  30. const tagList = this.$parent.$refs.tag
  31. let firstTag = null
  32. let lastTag = null
  33. // find first tag and last tag
  34. if (tagList.length > 0) {
  35. firstTag = tagList[0]
  36. lastTag = tagList[tagList.length - 1]
  37. }
  38. if (firstTag === currentTag) {
  39. $scrollWrapper.scrollLeft = 0
  40. } else if (lastTag === currentTag) {
  41. $scrollWrapper.scrollLeft = $scrollWrapper.scrollWidth - $containerWidth
  42. } else {
  43. // find preTag and nextTag
  44. const currentIndex = tagList.findIndex(item => item === currentTag)
  45. const prevTag = tagList[currentIndex - 1]
  46. const nextTag = tagList[currentIndex + 1]
  47. // the tag's offsetLeft after of nextTag
  48. const afterNextTagOffsetLeft = nextTag.$el.offsetLeft + nextTag.$el.offsetWidth + tagAndTagSpacing
  49. // the tag's offsetLeft before of prevTag
  50. const beforePrevTagOffsetLeft = prevTag.$el.offsetLeft - tagAndTagSpacing
  51. if (afterNextTagOffsetLeft > $scrollWrapper.scrollLeft + $containerWidth) {
  52. $scrollWrapper.scrollLeft = afterNextTagOffsetLeft - $containerWidth
  53. } else if (beforePrevTagOffsetLeft < $scrollWrapper.scrollLeft) {
  54. $scrollWrapper.scrollLeft = beforePrevTagOffsetLeft
  55. }
  56. }
  57. }
  58. }
  59. }
  60. </script>
  61. <style lang="scss" scoped>
  62. .scroll-container {
  63. white-space: nowrap;
  64. position: relative;
  65. overflow: hidden;
  66. width: 100%;
  67. /deep/ {
  68. .el-scrollbar__bar {
  69. bottom: 0px;
  70. }
  71. .el-scrollbar__wrap {
  72. height: 49px;
  73. }
  74. }
  75. }
  76. </style>