You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ant-design-vue/components/badge/ScrollNumber.vue

128 lines
3.2 KiB

<script>
7 years ago
import BaseMixin from '../_util/BaseMixin'
7 years ago
function getNumberArray (num) {
return num
? num.toString()
.split('')
.reverse()
.map(i => Number(i)) : []
}
export default {
name: 'ScrollNumber',
props: {
className: Object,
prefixCls: String,
count: [Number, String],
titleNumber: [Number, String],
styleNumber: {
type: Object,
default: () => ({}),
},
},
7 years ago
mixins: [BaseMixin],
data () {
7 years ago
const { count } = this
return {
7 years ago
animateStarted: true,
lastCount: 0,
stateCount: count,
}
},
7 years ago
watch: {
count (newValue, oldValue) {
// 复原数字初始位置
this.setState({
animateStarted: true,
lastCount: oldValue,
}, () => {
// 等待数字位置复原完毕
// 开始设置完整的数字
setTimeout(() => {
this.setState({
animateStarted: false,
stateCount: newValue,
})
}, 30)
})
},
},
methods: {
7 years ago
getPositionByNum (num, i) {
const { animateStarted, lastCount, stateCount } = this
if (animateStarted) {
return 10 + num
}
const currentDigit = getNumberArray(stateCount)[i]
const lastDigit = getNumberArray(lastCount)[i]
// 同方向则在同一侧切换数字
if (stateCount > lastCount) {
if (currentDigit >= lastDigit) {
return 10 + num
}
return 20 + num
}
if (currentDigit <= lastDigit) {
return 10 + num
}
return num
},
renderNumberList (position) {
const childrenArr = new Array(30).fill(1)
const childrenHtml = childrenArr.map((item, i) => {
const currentClassName = (position === i) ? 'current' : ''
return <p key={i.toString()} class={currentClassName}>{i % 10}</p>
})
return childrenHtml
},
renderCurrentNumber (num, i) {
const { animateStarted, prefixCls } = this
const position = this.getPositionByNum(num, i)
let removeTransition = animateStarted ||
(getNumberArray(this.lastCount)[i] === undefined)
if (!removeTransition) {
removeTransition = ''
}
const styleSpan = {
transition: `${removeTransition}` && 'none',
msTransform: `translateY(${-position * 100}%)`,
WebkitTransform: `translateY(${-position * 100}%)`,
transform: `translateY(${-position * 100}%)`,
}
return (
<span
key={i}
class={`${prefixCls}-only`}
style = {styleSpan}
>
{
this.renderNumberList(position)
}
</span>
)
},
renderNumberElement () {
const { stateCount } = this
if (!stateCount || isNaN(Number(stateCount))) {
return stateCount
}
return getNumberArray(stateCount)
.map((num, i) => this.renderCurrentNumber(num, i)).reverse()
},
},
7 years ago
render () {
const { prefixCls, className, titleNumber, styleNumber } = this
return (
<sup
class={[prefixCls, className]}
title={titleNumber}
style={styleNumber}>
{
this.renderNumberElement()
}
</sup>
)
},
}
</script>