When browsing a personal blog, background music adds to the immersion. But traditional multi-page blogs refresh the whole page on every navigation, cutting the music off. This article explains in detail how to build a vinyl-style music player in a Hexo blog and use PJAX to enable site-wide navigation without full refreshes, so the music keeps playing.

I. Interface Design: Retro Vinyl Style
Our goal is a mini player with a spinning animation and a tonearm effect, placed below the sidebar navigation.
1. HTML Structure
Add the following structure to the sidebar template (such as layout/partials/sidebar.ejs):
|
2. CSS Styles and Animation
Use CSS3 to achieve a skeuomorphic effect with the record spinning and the tonearm moving. The core code is below (Stylus):
|
II. Implementing the Playback Logic
Use JavaScript to keep the audio element and the UI state in sync.
|
III. The Core Difficulty: Uninterrupted Playback with PJAX
To keep the music playing across page navigations, we need PJAX (PushState + AJAX). Its principle: intercept link clicks, request the new page’s content via AJAX, and replace only the main body of the page (Main Content), keeping the sidebar (where the player lives) and the footer and other rarely-changing areas intact.
1. Adding the Dependencies
Include jQuery and jquery-pjax in layout.ejs:
|
2. Initializing PJAX
Configure PJAX to take over all in-site links and specify .main-content as the update container:
|
3. Pitfalls and Solutions
Problem 1: The comment section doesn’t load
Comment plugins like Gitalk usually initialize on page load. After PJAX replaces the content, the old scripts no longer run.
Fix: put the comment script inside the .main-content container, or manually re-instantiate the comment plugin in the pjax:complete callback.
Problem 2: Third-party JS stops working
Things like the Busuanzi counter and math-formula rendering all need to be explicitly reloaded or refreshed in the pjax:complete callback.
IV. Summary
With these steps we not only add a good-looking vinyl player to the blog but also greatly improve the user experience through PJAX. Now users can browse articles and check the archives while enjoying uninterrupted background music.
This “single-page-app-like” experience is one of the important directions for optimizing modern static blogs.
评论
0 条评论