自更新域名后,我就对博客进行了大改造,尤其是主页。
顺便也给文集增加了些新玩意儿,反正也是折腾。
1 文本卡片
在文章中增加了一个文本卡片的样式,如下:
重要提示
这里是粉色卡片内容
技术说明
这里是蓝色卡片内容
1.1 卡片样式文件
通过新建一个css文件,具体代码如下。
/* 文本卡片样式 */
.text-card {
position: relative;
margin: 1.5rem 0;
padding: 1.5rem;
background: #fff9f9;
border-left: 4px solid #ff7f7f;
border-radius: 0 4px 4px 0;
box-shadow: 0 2px 8px rgba(0,0,0,0.05);
transition: all 0.3s ease;
}
.text-card.blue {
background: #f5f9ff;
border-left-color: #5b9aff;
}
.text-card:hover {
box-shadow: 0 4px 12px rgba(0,0,0,0.1);
transform: translateY(-2px);
}
.text-card-title {
font-size: 1.1rem;
font-weight: bold;
color: #d23669;
margin-bottom: 0.8rem;
padding-bottom: 0.4rem;
border-bottom: 1px dashed #ffb6c1;
}
.text-card.blue .text-card-title {
color: #2a6fdb;
border-bottom-color: #a8c6ff;
}
.text-card-content {
font-size: 1rem;
line-height: 1.6;
color: #555;
}
.text-card-content pre {
background: #f8f8f8;
padding: 1rem;
border-radius: 4px;
overflow: auto;
}
/* 响应式调整 */
@media (max-width: 768px) {
.text-card {
margin: 1rem 0;
padding: 1rem;
}
}
1.2 样式引用
然后在 .html 中添加卡片容器。
<!-- 添加卡片样式引用 -->
<link rel="stylesheet" href="/css/card.css">
1.3 使用方法
在Markdown中的正确使用方法如下。
<!-- 粉色卡片 -->
<div class="text-card">
<div class="text-card-title">重要提示</div>
<div class="text-card-content">
这里是粉色卡片内容
</div>
</div>
<!-- 蓝色卡片 -->
<div class="text-card blue">
<div class="text-card-title">技术说明</div>
<div class="text-card-content">
这里是蓝色卡片内容
</div>
</div>
2 悬浮目录
之前有过左目录的想法,但一直搁置着,这次博客大修,正好就完善了,不过样式简单而且小透明式(在文化左侧),这也是我自己想要的结果。
2.1 目录样式文件
我同样是直接建立了一个css文件用来放toc样式,具体代码如下。
.toc-indicator {
position: fixed;
left: 20px;
top: 50%;
transform: translateY(-50%);
z-index: 1000;
}
.toc-indicator-item {
width: 4px;
height: 12px;
background: #ccc;
margin: 6px 0;
cursor: pointer;
transition: all 0.3s ease;
position: relative;
}
.toc-indicator-item.h2 { height: 16px; }
.toc-indicator-item.h3 { height: 12px; margin-left: 8px; width: 3px; }
.toc-indicator-item.h4 { height: 8px; margin-left: 16px; width: 2px; }
.toc-indicator-item.active {
background: #108adc;
}
.toc-indicator-item:hover {
background: #666;
}
.toc-tooltip {
position: absolute;
left: 15px;
top: 50%;
transform: translateY(-50%);
background: white;
padding: 2px 8px;
border-radius: 3px;
box-shadow: 0 1px 3px rgba(0,0,0,0.2);
font-size: 12px;
white-space: nowrap;
opacity: 0;
transition: opacity 0.2s;
pointer-events: none;
}
.toc-indicator-item:hover .toc-tooltip {
opacity: 1;
}
@media (max-width: 768px) {
.toc-indicator {
left: 10px;
}
.toc-tooltip {
display: none;
}
}
2.2 目录交互脚本
创建一个Js文件用于生成目录,具体代码如下。
function smoothScrollTo(element) {
window.scrollTo({
behavior: 'smooth',
top: element.offsetTop - 100
});
}
function setupTOC() {
const headings = document.querySelectorAll('article h2, article h3, article h4');
const tocContainer = document.createElement('div');
tocContainer.className = 'toc-indicator';
if (headings.length > 0) {
document.body.appendChild(tocContainer);
headings.forEach(heading => {
const id = heading.textContent.toLowerCase().replace(/\s+/g, '-');
heading.id = id;
const indicator = document.createElement('div');
indicator.className = `toc-indicator-item ${heading.tagName.toLowerCase()}`;
indicator.dataset.target = id;
const tooltip = document.createElement('div');
tooltip.className = 'toc-tooltip';
tooltip.textContent = heading.textContent;
indicator.appendChild(tooltip);
tocContainer.appendChild(indicator);
indicator.addEventListener('click', () => {
smoothScrollTo(heading);
});
});
// 滚动时高亮当前章节
window.addEventListener('scroll', () => {
const scrollPosition = window.scrollY + 150;
document.querySelectorAll('.toc-indicator-item').forEach(indicator => {
indicator.classList.remove('active');
});
for (let i = headings.length - 1; i >= 0; i--) {
if (scrollPosition >= headings[i].offsetTop) {
const activeId = headings[i].id;
document.querySelector(`.toc-indicator-item[data-target="${activeId}"]`)
.classList.add('active');
break;
}
}
});
}
}
document.addEventListener('DOMContentLoaded', setupTOC);
2.3 目录文件引用
最后,仍然是在 .html 中添加toc容器,引用样式css、js。
<head>
<link rel="stylesheet" href="/css/toc.css">
</head>
<body>
<script src="/js/toc.js"></script>
</body>