如何用CSS为Xaringan演示文稿标题页添加GitHub Corner?
当然可以实现!每次重新编织Xaringan演示文稿就会覆盖手动修改的HTML确实很烦人,我来给你两种可靠的方法,把GitHub Corner/丝带整合到编织流程里,再也不用重复改HTML啦:
方法一:HTML片段 + YAML Includes(最适合新手)
这种方法把GitHub Corner的代码放在单独的HTML文件里,通过Xaringan的YAML配置自动导入,不会被编织过程覆盖,还能轻松控制只在标题页显示。
步骤1:创建GitHub Corner的HTML片段
新建一个名为github-corner.html的文件,把下面的代码粘贴进去,记得替换href里的仓库地址为你自己的:
<a href="https://github.com/你的用户名/你的仓库名" class="github-corner" aria-label="View source on GitHub"> <svg width="80" height="80" viewBox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"> <path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path> <path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path> <path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path> </svg> </a> <style> .github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out} @keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}} @media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}} </style>
步骤2:创建自定义CSS控制显示范围
新建custom.css文件,添加以下代码,让GitHub Corner只在标题页显示,避免遮挡其他页面内容:
/* 给标题页设置相对定位,让角落元素可以绝对定位 */ .title-slide { position: relative; } /* 隐藏非标题页的GitHub Corner */ .slide:not(.title-slide) .github-corner { display: none; }
步骤3:在Xaringan的YAML中引入文件
修改你演示文稿的YAML头部,添加css和includes配置:
--- title: "你的演示文稿标题" output: xaringan::moon_reader: css: custom.css includes: after_body: github-corner.html ---
这样每次编织时,GitHub Corner会自动被插入到页面中,而且只在标题页显示!
方法二:CSS + 内嵌JavaScript(无需额外HTML文件)
如果你不想单独维护HTML文件,可以把所有代码整合到Rmd和CSS里,用JavaScript在页面加载时自动插入GitHub Corner元素。
步骤1:创建自定义CSS
同样新建custom.css,内容如下(已经包含了GitHub Corner的样式和显示控制):
.title-slide { position: relative; } .github-corner { position: absolute; top: 0; right: 0; z-index: 100; } .github-corner svg { fill: #151513; color: #fff; width: 80px; height: 80px; border: 0; } .github-corner:hover .octo-arm { animation: octocat-wave 560ms ease-in-out; } @keyframes octocat-wave { 0%, 100% { transform: rotate(0); } 20%, 60% { transform: rotate(-25deg); } 40%, 80% { transform: rotate(10deg); } } @media (max-width: 500px) { .github-corner:hover .octo-arm { animation: none; } .github-corner .octo-arm { animation: octocat-wave 560ms ease-in-out; } } /* 只在标题页显示 */ .slide:not(.title-slide) .github-corner { display: none; }
步骤2:在Rmd中添加内嵌JavaScript
在你的Xaringan Rmd文件开头(YAML之后),添加这段隐藏的JavaScript代码,记得替换仓库地址:
```{js, echo=FALSE} document.addEventListener('DOMContentLoaded', function() { // 创建GitHub Corner链接元素 const githubLink = document.createElement('a'); githubLink.href = 'https://github.com/你的用户名/你的仓库名'; githubLink.className = 'github-corner'; githubLink.setAttribute('aria-label', 'View source on GitHub'); // 插入SVG内容 githubLink.innerHTML = `<svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"> <path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path> <path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path> <path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path> </svg>`; // 找到标题页并插入元素 const titleSlide = document.querySelector('.title-slide'); if (titleSlide) { titleSlide.appendChild(githubLink); } });
### 步骤3:在YAML中引入CSS 同样修改YAML头部引入`custom.css`: ```yaml --- title: "你的演示文稿标题" output: xaringan::moon_reader: css: custom.css ---
替换为GitHub丝带的方法
如果你想要的是顶部的GitHub丝带(不是角落的Octocat图标),只需要替换HTML/JS里的内容即可,比如用这段代码:
<a href="https://github.com/你的用户名/你的仓库名" target="_blank"> <img loading="lazy" width="149" height="149" style="position: absolute; top: 0; right: 0; border: 0;" src="https://github.blog/wp-content/uploads/2008/12/forkme_right_darkblue_121621.png?resize=149%2C149" alt="Fork me on GitHub"> </a>
然后调整CSS里的样式(比如去掉动画相关的代码),同样可以控制只在标题页显示。
内容的提问来源于stack exchange,提问作者Justin Millar
相关产品推荐
相关产品推荐

