昨日看到一网友分享,在个人博客里集成Live Photo,甚是炫酷!必须借鉴一手。
奈何技术水平不高,只能使用AI辅助编写。
背景
Hexo + cactus主题
步骤
新增livephoto.js 和 livephoto.css
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
(function() { document.addEventListener('DOMContentLoaded', function() { var livePhotoElements = document.querySelectorAll('.live-photo');
if (livePhotoElements.length > 0) { var script = document.createElement('script'); script.src = 'https://cdn.apple-livephotoskit.com/lpk/1/livephotoskit.js'; script.onload = function() { initializeLivePhotos(livePhotoElements); }; document.head.appendChild(script); } });
function initializeLivePhotos(elements) { elements.forEach(function(element) { var photoSrc = element.getAttribute('data-photo-src'); var videoSrc = element.getAttribute('data-video-src');
if (photoSrc && videoSrc) { 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'; } }); } })();
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| .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; }
|
修改文件引用
1 2 3 4
|
<!-- livephotos styles --> <%- css('css/livephotos') %>
|
1 2
| <%- js('js/livephotos') %>
|
编写示例
在Markdown文件中,你可以使用以下格式添加实况照片:
1 2
| <div class="live-photo" data-photo-src="https://你的OSS地址/照片.JPEG" data-video-src="https://你的OSS地址/视频.MOV"> </div>
|
实现效果
点击查看