优化scroll组件

master
chenjiajian 2023-03-21 18:18:16 +08:00
parent 21cb347354
commit 20e07082e1
1 changed files with 16 additions and 19 deletions

View File

@ -8,13 +8,7 @@ import { css, bind, html, unbind, Component } from '@bd/core'
class Scroll extends Component { class Scroll extends Component {
static props = { static props = {
axis: { axis: 'xy',
type: String,
default: 'xy',
observer(val) {
this.onAxisChange(val)
}
},
distance: 0 distance: 0
} }
dragging = false dragging = false
@ -23,7 +17,7 @@ class Scroll extends Component {
css` css`
:host { :host {
position: relative; position: relative;
overflow: hidden; overflow: auto;
display: block; display: block;
&::-webkit-scrollbar { &::-webkit-scrollbar {
display: none; display: none;
@ -34,7 +28,6 @@ class Scroll extends Component {
position: relative; position: relative;
} }
.scroll-bar { .scroll-bar {
display: none;
position: absolute; position: absolute;
background: #909399; background: #909399;
width: 0; width: 0;
@ -64,16 +57,22 @@ class Scroll extends Component {
visibility: hidden; visibility: hidden;
} }
} }
:host([axis='x']) {
overflow-x: auto;
overflow-y: hidden;
.vertical {
display: none;
}
}
:host([axis='y']) {
overflow-y: auto;
overflow-x: hidden;
.horizon {
display: none;
}
}
` `
] ]
onAxisChange(val) {
this.style.overflow = 'hidden'
if (val === 'xy') {
this.style.overflow = 'auto'
} else {
this.style[`overflow-${val}`] = 'auto'
}
}
render() { render() {
return html` return html`
@ -193,12 +192,10 @@ class Scroll extends Component {
this.horizonWidth = 0 this.horizonWidth = 0
} }
if (axis.includes('x')) { if (axis.includes('x')) {
horizon.style.display = 'block'
horizon.style.width = this.horizonWidth + 'px' horizon.style.width = this.horizonWidth + 'px'
bind(horizon, 'mousedown', this.onmousedown.bind(this)) bind(horizon, 'mousedown', this.onmousedown.bind(this))
} }
if (axis.includes('y')) { if (axis.includes('y')) {
vertical.style.display = 'block'
vertical.style.height = this.verticalHeight + 'px' vertical.style.height = this.verticalHeight + 'px'
bind(vertical, 'mousedown', this.onmousedown.bind(this)) bind(vertical, 'mousedown', this.onmousedown.bind(this))
} }