Hexo博客集成LivePhoto

昨日看到一网友分享,在个人博客里集成Live Photo,甚是炫酷!必须借鉴一手。

奈何技术水平不高,只能使用AI辅助编写,使用海外版的Trae。直接输入指令,让AI帮我集成Apple's LivePhotosKit.js

背景

Hexo + cactus主题

步骤

新增livephoto.js 和 livephoto.css

javascript 复制代码
/**
 * LivePhotos integration for Hexo
 * This script handles the integration of Apple's LivePhotosKit.js
 */

(function() {
  // 当文档加载完成后执行
  document.addEventListener('DOMContentLoaded', function() {
    // 查找所有带有live-photo类的元素
    var livePhotoElements = document.querySelectorAll('.live-photo');

    if (livePhotoElements.length > 0) {
      // 如果页面上有实况照片元素,才加载LivePhotosKit.js
      var script = document.createElement('script');
      script.src = 'https://cdn.apple-livephotoskit.com/lpk/1/livephotoskit.js';
      script.onload = function() {
        // LivePhotosKit.js加载完成后,初始化所有实况照片
        initializeLivePhotos(livePhotoElements);
      };
      document.head.appendChild(script);
    }
  });

  // 初始化所有实况照片元素
  function initializeLivePhotos(elements) {
    elements.forEach(function(element) {
      // 获取照片和视频的URL
      var photoSrc = element.getAttribute('data-photo-src');
      var videoSrc = element.getAttribute('data-video-src');

      if (photoSrc && videoSrc) {
        // 创建LivePhotosKit播放器
        var player = LivePhotosKit.Player(element);

        // 设置照片和视频源
        player.photoSrc = photoSrc;
        player.videoSrc = videoSrc;

        // 添加鼠标悬停事件来播放实况照片
        element.addEventListener('mouseenter', function() {
          player.play();
        });

        element.addEventListener('mouseleave', function() {
          player.pause();
        });

        // 添加触摸事件支持(移动设备)
        element.addEventListener('touchstart', function() {
          player.play();
        });

        element.addEventListener('touchend', function() {
          player.pause();
        });

        // 添加样式
        element.style.position = 'relative';
        element.style.overflow = 'hidden';
        element.style.display = 'inline-block';
      }
    });
  }
})();
javascript 复制代码
.live-photo {
    width: 100%;
    max-width: 800px;
    height: auto;
    aspect-ratio: 4/3;
    margin: 20px 0;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    cursor: pointer;
}

.live-photo img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 8px;
}

.live-photo video {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 8px;
}

修改文件引用

javascript 复制代码
// <meta name="keywords" content= "<%= (theme.keywords) %>">
// meta标签之下
<!-- livephotos styles -->
    <%- css('css/livephotos') %>
javascript 复制代码
// <%- js('js/main') %> 43行之下
<%- js('js/livephotos') %>

编写示例

在Markdown文件中,你可以使用以下格式添加实况照片:

html 复制代码
<div class="live-photo" data-photo-src="https://你的OSS地址/照片.JPEG" data-video-src="https://你的OSS地址/视频.MOV">
</div>
相关推荐
薛定猫AI3 小时前
【深度解析】Gemma Chat 本地 AI 编程 Agent:Electron + MLX + 开源模型的离线 Vibe Coding 实战
javascript·人工智能·electron
全栈前端老曹3 小时前
【前端地图】多地图平台适配方案——高德、百度、腾讯、Google Maps SDK 差异对比、封装统一地图接口
前端·javascript·百度·dubbo·wgs84·gcj-02·bd09
笑虾3 小时前
Win10 修改注册表 让鼠标悬停PNG上时 tip 始终显示分辨率
开发语言·javascript·ecmascript
雾岛听风6913 小时前
JavaScript基础语法速查手册
开发语言·前端·javascript
用户2367829801684 小时前
从零实现 GIF 制作工具:LZW 压缩与 Median Cut 色彩量化
前端·javascript
棉猴4 小时前
Python海龟绘图之绘制文本
javascript·python·html·write·turtle·海龟绘图·输出文本
Highcharts.js5 小时前
线形比赛积分增长或竞赛图|Highcharts企业图表代码示列
开发语言·前端·javascript·折线图·highcharts·竞赛图
让学习成为一种生活方式5 小时前
大肠杆菌合成扑热息痛--对乙酰氨基酚--文献精读227
开发语言·前端·javascript
多秋浮沉度华年5 小时前
electron 初始使用记录
javascript·arcgis·electron
竹林8185 小时前
用 wagmi v2 + WebSocket 硬磕 NFT 上架失败:一个前端开发者踩过的实时状态同步坑
javascript·next.js