优化代码逻辑

master
chenjiajian 2023-03-23 18:56:07 +08:00
parent a01a8e5d4e
commit 20616529db
1 changed files with 53 additions and 23 deletions

View File

@ -13,8 +13,8 @@ class Slider extends Component {
default: 0,
observer() {}
},
max: null,
min: null,
max: 100,
min: 0,
step: 1,
disabled: false,
readonly: false,
@ -75,6 +75,7 @@ class Slider extends Component {
}
}
.tips {
user-select: none;
position: absolute;
top: -100%;
border-radius: 4px;
@ -101,7 +102,7 @@ class Slider extends Component {
cursor: grabbing !important;
}
`
progress = 0
mounted() {
console.log('slider mounted')
console.log(this.$refs, 'refs')
@ -114,46 +115,75 @@ class Slider extends Component {
onMousedown(e) {
document.documentElement.classList.toggle('cursor-default')
const start = e.clientX
const { value: preValue, step, max, min } = this
let { value: preValue, step, max, min, progress } = this
preValue = preValue || min
const onMousemove = bind(document, 'mousemove', e => {
const distance = e.clientX - start
const diff = Math.round((distance / this.$runway.clientWidth) * 100)
let newValue = preValue + diff
newValue = Math.floor(newValue)
if (newValue < 0) {
newValue = 0
const scale = distance / this.$runway.clientWidth
const diff = Math.round(scale * (max - min))
let newValue = Math.floor(preValue + diff)
// console.log({ diff, preValue })
let newProgress = progress + Math.floor(scale * 100)
if (newValue < min) {
newValue = min
}
if (newValue > 100) {
newValue = 100
if (newValue > max) {
newValue = max
}
if (newValue % step) {
return
}
this.$bar.style.width = newValue + '%'
this.$dotWrap.style.left = newValue + '%'
this.$tips.style.left = newValue + '%'
// console.warn(newValue)
this.value = newValue
this.setProgress(newProgress)
})
const onMouseup = bind(document, 'mouseup', e => {
const onMouseup = bind(document, 'mouseup', () => {
unbind(document, 'mousemove', onMousemove)
unbind(document, 'mouseup', onMouseup)
document.documentElement.classList.toggle('cursor-default')
})
}
onClick(e) {
if (e.target !== this.$refs.runway) {
return
}
const { max, min, step } = this
const { clientWidth } = e.target
const { offsetX } = e
this.value = Math.floor((offsetX / clientWidth) * 100)
const barWidth = `${this.value}%`
this.$bar.style.width = barWidth
this.$dotWrap.style.left = barWidth
this.$tips.style.left = barWidth
const range = max - min
const scale = offsetX / clientWidth
// console.log(
// {
// scale,
// range
// },
// scale * range + min
// )
let newValue = Math.floor(scale * range + min)
const mod = newValue % step
if (mod) {
const half = step / 2
if (mod > half) {
newValue += step - mod
} else {
newValue -= mod
}
}
this.value = newValue
console.log(scale * 100)
//const progress = Math.floor(scale * 100)
const progress = Math.floor(((newValue - min) / range) * 100)
// console.log({ progress })
this.setProgress(progress)
}
setProgress(val) {
val = Math.floor(val)
val = val > 100 ? 100 : val
val = val < 0 ? 0 : val
this.$bar.style.width = `${val}%`
this.$dotWrap.style.left = `${val}%`
this.$tips.style.left = `${val}%`
this.progress = val
}
render() {
return html`<div class="slider" ref="slider">