Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

歌词滚动功能(hot) #175

Open
Sunny-117 opened this issue Nov 3, 2022 · 1 comment
Open

歌词滚动功能(hot) #175

Sunny-117 opened this issue Nov 3, 2022 · 1 comment

Comments

@Sunny-117
Copy link
Owner

No description provided.

@z1the3
Copy link

z1the3 commented Apr 29, 2023

    // vue
    // use-lyric.js

    const lyric = [
        {text:'第一句',time:1000},
        {text:'第二句',time:2000}
    ]
    
    export default function useLyric({lyric,currentTime}){
        const currentLine = ref(0)
        const lyricScrollRef = ref(null)
        const lyricListRef = ref(null)
    
        const setLine = (line) =>{
                currentLine.value = line
                scrollToLine(index)
        }
        function playLyric(){
            if(lyric){
                for(let i=0;i<lyric.length;i++){
                    if(lyric[i].time - currentTime.value>-1000
                        &&lyric[i].time - currentTime.value<0){
                            setLine(i)
                            return
                    }
                }
            }
        }
    
        function scrollToLine(index){
            const scrollComp = lyricScrollRef.value
            const listEl = lyricListRef.value
            if (!listEl) return
            if (lineNum > 5) {
                // 滚动到当前行的前五行位置
                const lineEl = listEl.children[lineNum - 5]
                scrollComp.scroll.scrollToElement(lineEl, 5000)
            } else {
                scrollComp.scroll.scrollTo(0, 0, 1000)
            }
            return lineNum
    
        }
        return {
            currentLine,
            setLine,
            playLyric,
            lyricListRef,
            lyricScrollRef
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants