Skip to content

Commit

Permalink
feat: remove autoplay bgm
Browse files Browse the repository at this point in the history
  • Loading branch information
zerosoul committed Sep 6, 2020
1 parent 356cae3 commit 781270a
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/components/Bgm.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ const Rotation = keyframes`
transform: rotate(359deg);
}
`;
const Swing = keyframes`
from {
transform: translateX(10px);
}
to {
transform: translateX(0);
}
`;
const StyledWraper = styled.aside`
position: fixed;
left: 0.4rem;
Expand All @@ -32,10 +40,21 @@ const StyledWraper = styled.aside`
&.paused {
animation-play-state: paused;
}
.tip {
position: absolute;
font-size: 0.5rem;
padding: 0.2rem 0.4rem;
background: rgba(2, 2, 2, 0.4);
line-height: 1.4;
right: -180%;
border-radius: 0.5rem;
animation: ${Swing} 2s infinite ease-in-out forwards;
}
`;

const MUSIC_PLAYED = !!localStorage.getItem('MUSIC_PLAYED') || false;
export default function Bgm() {
const [playing, setPlaying] = useState(false);
const [played, setPlayed] = useState(MUSIC_PLAYED);
const bgMusic = useRef(null);
const handlePause = () => {
setPlaying(false);
Expand All @@ -44,6 +63,10 @@ export default function Bgm() {
setPlaying(true);
};
const togglePlay = () => {
if (!played) {
setPlayed(true);
localStorage.setItem('MUSIC_PLAYED', true);
}
const bgM = bgMusic.current;
if (playing) {
bgM.pause();
Expand Down Expand Up @@ -72,12 +95,13 @@ export default function Bgm() {
</svg>
<audio
ref={bgMusic}
autoPlay={true}
autoPlay={false}
onPlaying={handlePlaying}
onPause={handlePause}
loop={true}
src="./static/bgm.mp3"
></audio>
{!played && <div className="tip">👈 点击播放</div>}
</StyledWraper>
);
}

0 comments on commit 781270a

Please sign in to comment.