index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*!
  2. * x-scrollbar 自定义滚动条插件
  3. * 版本: v3.1.0
  4. * 作者: 清晨的阳光(QQ:765550360)
  5. * 许可: MIT
  6. * https://gitee.com/xujz520/x-scrollbar
  7. */
  8. import './index.css'
  9. class XScrollbar {
  10. constructor(dom, options) {
  11. this.$dom = dom
  12. if (this.$dom.classList.contains('x-scrollbar')) return
  13. this.$dom.classList.add('x-scrollbar')
  14. // 移动端检测
  15. this.isMobile = window.navigator.userAgent.toLowerCase().indexOf('mobile') != -1
  16. // 合并配置
  17. let defaultOptions = {
  18. // 响应容器和内容大小改变(自动更新滚动条)
  19. autoUpdate: true,
  20. // 阻止向上传递滚动事件
  21. preventDefault: true,
  22. // 仅水平滚动(拨动鼠标滚轮时将作用于X轴)
  23. onlyHorizontal: false,
  24. // 自动隐藏
  25. autoHide: true,
  26. }
  27. let defaultStyle = {
  28. // 滑块大小
  29. thumbSize: '5px',
  30. // 轨道颜色
  31. trackBackground: '#ddd',
  32. // 滑块颜色
  33. thumbBackground: '#5f5f5f',
  34. // 滑块圆角大小
  35. thumbRadius: '5px',
  36. }
  37. Object.assign(this, defaultOptions, defaultStyle, options)
  38. // 构造dom
  39. let scrollLeft = this.$dom.scrollLeft
  40. let scrollTop = this.$dom.scrollTop
  41. this.$container = this.html2dom('<div class="x-scrollbar__container"></div>')
  42. this.$content = this.html2dom('<div class="x-scrollbar__content"></div>')
  43. this.$trackX = this.html2dom('<div class="x-scrollbar__track-x"></div>')
  44. this.$trackY = this.html2dom('<div class="x-scrollbar__track-y"></div>')
  45. this.$thumbX = this.html2dom('<div class="x-scrollbar__thumb-x"></div>')
  46. this.$thumbY = this.html2dom('<div class="x-scrollbar__thumb-y"></div>')
  47. this.$trackX.appendChild(this.$thumbX)
  48. this.$trackY.appendChild(this.$thumbY)
  49. let childNodes = []
  50. Array.prototype.forEach.call(this.$dom.childNodes, function (node) {
  51. childNodes.push(node)
  52. })
  53. childNodes.forEach(
  54. function (node) {
  55. this.$content.appendChild(node)
  56. }.bind(this)
  57. )
  58. this.$container.appendChild(this.$content)
  59. this.$dom.appendChild(this.$container)
  60. // 处理内边距
  61. let styleObj = getComputedStyle(this.$dom)
  62. let padding = `${styleObj.paddingTop} ${styleObj.paddingRight} ${styleObj.paddingBottom} ${styleObj.paddingLeft}`
  63. if (padding != '0px 0px 0px 0px') {
  64. this.$dom.style.padding = '0px 0px 0px 0px'
  65. this.$container.style.padding = padding
  66. }
  67. // 设置初始值
  68. this.$container.scrollLeft = scrollLeft
  69. this.$container.scrollTop = scrollTop
  70. if (this.preventDefault) {
  71. this.$container.classList.add('x-scrollbar__container--preventDefault')
  72. }
  73. if (this.isMobile) return
  74. this.$dom.appendChild(this.$trackX)
  75. this.$dom.appendChild(this.$trackY)
  76. this.$container.classList.add('x-scrollbar__container--hideScrollbar')
  77. if (JSON.stringify(defaultStyle) != JSON.stringify(Object.keys(defaultStyle).reduce((obj, k) => ({ ...obj, [k]: this[k] }), {}))) {
  78. this.style()
  79. }
  80. // 自动隐藏
  81. if (!this.autoHide) this.$dom.classList.add('x-scrollbar-keep')
  82. // 绑定事件
  83. this.bindScroll()
  84. this.bindDrag()
  85. if (this.onlyHorizontal) {
  86. this.bindWheel()
  87. }
  88. // 响应容器和内容大小改变
  89. if (this.autoUpdate) {
  90. // 首次自动触发
  91. this.resizeObserver()
  92. } else {
  93. this.update()
  94. }
  95. }
  96. /**
  97. * 设置滑块大小
  98. */
  99. setThumbSize() {
  100. // (clientWidth / scrollWidth) = (滑块大小 / clientWidth)
  101. // 最大滑动距离 = clientWidth - 滑块大小
  102. // 最大滚动距离 = scrollWidth - clientWidth
  103. // (滑动距离 / 最大滑动距离) = (滚动距离 / 最大滚动距离)
  104. // 容器大小
  105. this.clientWidth = this.$container.clientWidth
  106. this.clientHeight = this.$container.clientHeight
  107. // 内容大小
  108. this.scrollWidth = this.$container.scrollWidth
  109. this.scrollHeight = this.$container.scrollHeight
  110. //是否存在滚动条
  111. this.hasXScrollbar = this.scrollWidth > this.clientWidth
  112. this.hasYScrollbar = this.scrollHeight > this.clientHeight
  113. //滑块大小
  114. this.thumbXWidth = Math.max((this.clientWidth / this.scrollWidth) * this.clientWidth, 30)
  115. this.thumbYHeight = Math.max((this.clientHeight / this.scrollHeight) * this.clientHeight, 30)
  116. //最大滑动距离
  117. this.thumbXMaxLeft = this.clientWidth - this.thumbXWidth
  118. this.thumbYMaxTop = this.clientHeight - this.thumbYHeight
  119. //最大滚动距离
  120. this.maxScrollLeft = this.scrollWidth - this.clientWidth
  121. this.maxScrollTop = this.scrollHeight - this.clientHeight
  122. this.$trackX.style.display = this.hasXScrollbar ? 'block' : 'none'
  123. this.$trackY.style.display = this.hasYScrollbar ? 'block' : 'none'
  124. this.$thumbX.style.width = this.thumbXWidth + 'px'
  125. this.$thumbY.style.height = this.thumbYHeight + 'px'
  126. }
  127. /**
  128. * 拖动事件
  129. */
  130. bindDrag() {
  131. // 上一次的拖动位置
  132. let screenX = null
  133. let screenY = null
  134. this.$thumbX.addEventListener('mousedown', e => {
  135. this.$trackX.classList.add('x-scrollbar__track--draging')
  136. this.thumbXActive = true
  137. screenX = e.screenX
  138. })
  139. this.$thumbY.addEventListener('mousedown', e => {
  140. this.$trackY.classList.add('x-scrollbar__track--draging')
  141. this.thumbYActive = true
  142. screenY = e.screenY
  143. })
  144. document.addEventListener('mouseup', _ => {
  145. this.$trackX.classList.remove('x-scrollbar__track--draging')
  146. this.$trackY.classList.remove('x-scrollbar__track--draging')
  147. this.thumbXActive = false
  148. this.thumbYActive = false
  149. })
  150. document.addEventListener('mousemove', e => {
  151. if (!(this.thumbXActive || this.thumbYActive)) return
  152. e.preventDefault()
  153. requestAnimationFrame(() => {
  154. if (this.thumbXActive) {
  155. let offset = e.screenX - screenX
  156. screenX = e.screenX
  157. let left = Math.max(Math.min(parseFloat(this.$thumbX.style.left || 0) + offset, this.thumbXMaxLeft), 0)
  158. this.$thumbX.style.left = left + 'px'
  159. this.$container.scrollLeft = (left / this.thumbXMaxLeft) * this.maxScrollLeft
  160. } else {
  161. let offset = e.screenY - screenY
  162. screenY = e.screenY
  163. let top = Math.max(Math.min(parseFloat(this.$thumbY.style.top || 0) + offset, this.thumbYMaxTop), 0)
  164. this.$thumbY.style.top = top + 'px'
  165. this.$container.scrollTop = (top / this.thumbYMaxTop) * this.maxScrollTop
  166. }
  167. })
  168. })
  169. }
  170. /**
  171. * 仅水平滚动(拨动鼠标滚轮时将作用于X轴)
  172. */
  173. bindWheel() {
  174. let easeout = (start, end) => {
  175. if (Math.abs(end - start) <= 1) return end
  176. return start + (end - start) / 4
  177. }
  178. this.$container.addEventListener('wheel', e => {
  179. // 仅响应 y 滚动 => 作用于 x
  180. if (!this.hasXScrollbar) return
  181. if (e.deltaY && !e.shiftKey) {
  182. // 结束值
  183. this.scrollLeft = Math.max(Math.min((this.scrollLeft || this.$container.scrollLeft) + (e.deltaY > 0 ? 100 : -100), this.maxScrollLeft), 0)
  184. this.left = (this.scrollLeft / this.maxScrollLeft) * this.thumbXMaxLeft
  185. // 阻止向上传递 || !(终点)
  186. if (this.preventDefault || !(this.scrollLeft == 0 || this.scrollLeft == this.maxScrollLeft)) {
  187. e.preventDefault()
  188. e.stopPropagation()
  189. }
  190. if (this.reqId) return
  191. // 起始值
  192. let scrollLeft = this.$container.scrollLeft
  193. let left = parseFloat(this.$thumbX.style.left || 0)
  194. let animate = () => {
  195. scrollLeft = easeout(scrollLeft, this.scrollLeft)
  196. left = easeout(left, this.left)
  197. this.$container.scrollLeft = scrollLeft
  198. this.$thumbX.style.left = left + 'px'
  199. this.innerScroll = true
  200. if (scrollLeft != this.scrollLeft) {
  201. this.reqId = requestAnimationFrame(animate)
  202. } else {
  203. this.reqId = null
  204. this.scrollLeft = null
  205. requestAnimationFrame(() => (this.innerScroll = false))
  206. }
  207. }
  208. animate()
  209. }
  210. })
  211. }
  212. /**
  213. * 滚动事件 => 修正滑块位置
  214. */
  215. bindScroll() {
  216. this.$container.addEventListener('scroll', () => {
  217. if (this.thumbXActive || this.thumbYActive || this.innerScroll) return
  218. if (this.hasXScrollbar) {
  219. this.$thumbX.style.left = (this.$container.scrollLeft / this.maxScrollLeft) * this.thumbXMaxLeft + 'px'
  220. }
  221. if (this.hasYScrollbar) {
  222. this.$thumbY.style.top = (this.$container.scrollTop / this.maxScrollTop) * this.thumbYMaxTop + 'px'
  223. }
  224. })
  225. }
  226. /**
  227. * 观察容器大小
  228. */
  229. resizeObserver() {
  230. this.$resizeObserver = new ResizeObserver(entries => {
  231. let contentRect = entries[0].contentRect
  232. if (!(contentRect.width || contentRect.height)) return
  233. this.update()
  234. })
  235. this.$resizeObserver.observe(this.$container)
  236. this.$resizeObserver.observe(this.$content)
  237. }
  238. /**
  239. * 使用滚动值修正滑块
  240. * 在 容器大小 或 内容大小 发生改变时调用
  241. */
  242. update() {
  243. this.setThumbSize()
  244. if (this.hasXScrollbar) {
  245. this.$thumbX.style.left = (this.$container.scrollLeft / this.maxScrollLeft) * this.thumbXMaxLeft + 'px'
  246. }
  247. if (this.hasYScrollbar) {
  248. this.$thumbY.style.top = (this.$container.scrollTop / this.maxScrollTop) * this.thumbYMaxTop + 'px'
  249. }
  250. }
  251. /**
  252. * html字符串 转 dom对象
  253. * @param {*} html
  254. * @returns
  255. */
  256. html2dom(html) {
  257. let element = document.createElement('div')
  258. element.innerHTML = html
  259. let children = element.children
  260. if (children.length <= 1) {
  261. return children[0]
  262. } else {
  263. return children
  264. }
  265. }
  266. /**
  267. * 生成自定义样式
  268. */
  269. style() {
  270. let content = `
  271. /* 轨道 */
  272. .x-scrollbar__track-x {
  273. height: ${parseInt(this.thumbSize) * 2 + 4}px;
  274. }
  275. .x-scrollbar__track-y {
  276. width: ${parseInt(this.thumbSize) * 2 + 4}px;
  277. }
  278. /* 滑块 */
  279. .x-scrollbar__track-x > .x-scrollbar__thumb-x,
  280. .x-scrollbar__track-y > .x-scrollbar__thumb-y {
  281. background: ${this.thumbBackground};
  282. border-radius: ${parseInt(this.thumbRadius || 0) != 5 ? parseInt(this.thumbRadius || 0) : parseInt(this.thumbSize)}px;
  283. }
  284. .x-scrollbar__track-x > .x-scrollbar__thumb-x {
  285. height: ${parseInt(this.thumbSize)}px;
  286. }
  287. .x-scrollbar__track-y > .x-scrollbar__thumb-y {
  288. width: ${parseInt(this.thumbSize)}px;
  289. }
  290. /* 激活后大小 */
  291. .x-scrollbar__track-x:hover > .x-scrollbar__thumb-x,
  292. .x-scrollbar__track--draging > .x-scrollbar__thumb-x {
  293. height: ${parseInt(this.thumbSize) * 2}px;
  294. }
  295. .x-scrollbar__track-y:hover > .x-scrollbar__thumb-y,
  296. .x-scrollbar__track--draging > .x-scrollbar__thumb-y {
  297. width: ${parseInt(this.thumbSize) * 2}px;
  298. }
  299. /* 鼠标移入轨道 || 拖动过程中 => 显示轨道 & 高亮滑块 */
  300. .x-scrollbar__track-x:hover,
  301. .x-scrollbar__track-y:hover,
  302. .x-scrollbar__track-x.x-scrollbar__track--draging,
  303. .x-scrollbar__track-y.x-scrollbar__track--draging {
  304. background: ${this.trackBackground || 'transparent'};
  305. }`
  306. this.key = 'x-scrollbar-' + Math.abs(((1 + Math.random()) * Date.now()) | 0).toString(16)
  307. this.$dom.setAttribute(this.key, '')
  308. let style = this.html2dom(`<style ${this.key}></style>`)
  309. content = content.replaceAll('\n.x-scrollbar', `\n[${this.key}] > .x-scrollbar`)
  310. content = content.replaceAll(';', ' !important;')
  311. style.innerHTML = content
  312. document.querySelector('head').appendChild(style)
  313. }
  314. }
  315. export default XScrollbar