diff --git a/demo/package.json b/demo/package.json index 676fdc0..62430a8 100644 --- a/demo/package.json +++ b/demo/package.json @@ -6,7 +6,7 @@ "@testing-library/jest-dom": "^5.17.0", "@testing-library/react": "^13.4.0", "@testing-library/user-event": "^13.5.0", - "rc-bullets-ts": "^1.3.0", + "rc-bullets-ts": "^1.3.2", "react": "^18.2.0", "react-dom": "^18.2.0", "react-scripts": "5.0.1", diff --git a/demo/src/App.js b/demo/src/App.js index fbe3a90..a9d974b 100644 --- a/demo/src/App.js +++ b/demo/src/App.js @@ -1,6 +1,6 @@ import React, {useEffect, useRef} from 'react'; -import './App.css'; import BulletScreen, {StyledBullet} from 'rc-bullets-ts'; +import './App.css'; function App() { const isPause = useRef(false); @@ -8,6 +8,14 @@ function App() { useEffect(() => { // 给页面中某个元素初始化弹幕屏幕,一般为一个大区块。此处的配置项全局生效 barrageScreen.current = new BulletScreen('.screen-container', {duration: 10}); + document.addEventListener('visibilitychange', function () { + if (!document.hidden) { + console.log('page is visible'); + barrageScreen.current.resize(); + } else { + console.log('page is invisible'); + } + }); }, []); /** @@ -16,16 +24,16 @@ function App() { */ const handleSend = (barrageInfo) => { if (barrageInfo) { - barrageScreen.current.push( - , - { - bottom: '9px', // 指定所有弹幕距离容器底部距离为9px - } - ); + setInterval(() => { + barrageScreen.current.push( + , + {} + ); + }, 500); } }; diff --git a/package-lock.json b/package-lock.json index ff2d405..1bed6c8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "rc-bullets-ts", - "version": "1.0.7", + "version": "1.4.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 5b7570b..5fa1133 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rc-bullets-ts", - "version": "1.3.2", + "version": "1.4.0", "description": "一个简单的react弹幕库", "main": "dist/main.js", "module": "dist/main.js", diff --git a/src/bulletScreen.tsx b/src/bulletScreen.tsx index 2e001d1..68f1f0e 100644 --- a/src/bulletScreen.tsx +++ b/src/bulletScreen.tsx @@ -61,6 +61,11 @@ class BulletScreen { style.sheet?.insertRule(`@keyframes RightToLeft { ${from} ${to} }`, 0); } + /** + * 发送弹幕 + * @param item + * @param opts + */ push(item: pushItem, opts: Partial) { const options = {...this.options, ...opts}; const {onStart, onEnd, top, bottom} = options; @@ -101,6 +106,10 @@ class BulletScreen { return bulletContainer.id; } + /** + * 获取需要渲染的dom样式 + * @param Item + */ getRenderDom(Item: pushItem) { if (React.isValidElement(Item)) { return Item; @@ -169,6 +178,15 @@ class BulletScreen { this.bullets = []; } + /** + * 样式重置 + */ + resize() { + const {trackHeight} = this.options; + this.initBulletTrack(trackHeight); + this.initBulletAnimate(this.target); + } + /** * 获取播放轨道 * @returns @@ -226,8 +244,6 @@ class BulletScreen { const observer = new IntersectionObserver(enteries => { for (const entry of enteries) { const {intersectionRatio, target, isIntersecting} = entry; - console.log('bullet id', target.id, intersectionRatio, isIntersecting); - console.log('resTarget', this.target, entry); if (intersectionRatio >= 1) { const curTaget = target as HTMLElement; const trackIdx = typeof (curTaget.dataset.track) === 'undefined' ? undefined : +curTaget.dataset.track; diff --git a/typings/bulletScreen.d.ts b/typings/bulletScreen.d.ts index 0b4cbe6..d65732b 100644 --- a/typings/bulletScreen.d.ts +++ b/typings/bulletScreen.d.ts @@ -19,7 +19,16 @@ declare class BulletScreen { * @param screen */ initBulletAnimate(screen: HTMLElement): void; + /** + * 发送弹幕 + * @param item + * @param opts + */ push(item: pushItem, opts: Partial): string; + /** + * 获取需要渲染的dom样式 + * @param Item + */ getRenderDom(Item: pushItem): JSX.Element; _toggleAnimateStatus: (id: string | null, status?: string) => void; pause(id?: string | null): void; @@ -27,6 +36,10 @@ declare class BulletScreen { hide(): void; show(): void; clear(id?: null): void; + /** + * 样式重置 + */ + resize(): void; /** * 获取播放轨道 * @returns diff --git a/typings/src/bulletScreen.d.ts b/typings/src/bulletScreen.d.ts index 0b4cbe6..d65732b 100644 --- a/typings/src/bulletScreen.d.ts +++ b/typings/src/bulletScreen.d.ts @@ -19,7 +19,16 @@ declare class BulletScreen { * @param screen */ initBulletAnimate(screen: HTMLElement): void; + /** + * 发送弹幕 + * @param item + * @param opts + */ push(item: pushItem, opts: Partial): string; + /** + * 获取需要渲染的dom样式 + * @param Item + */ getRenderDom(Item: pushItem): JSX.Element; _toggleAnimateStatus: (id: string | null, status?: string) => void; pause(id?: string | null): void; @@ -27,6 +36,10 @@ declare class BulletScreen { hide(): void; show(): void; clear(id?: null): void; + /** + * 样式重置 + */ + resize(): void; /** * 获取播放轨道 * @returns