无需复杂开发!3 步实现响应式灵动岛效果,适配 WordPress / 静态网页,主题更新不失效,提升网站交互体验与视觉质感。
核心优势(SEO 重点)
- 「零技术门槛」:复制粘贴即可使用,无需编程基础,新手也能快速上手
- 「主题更新兼容」:提供免修改核心文件方案,避免更新后效果丢失
- 「全场景适配」:自动识别首页、文章页、分类页、用户中心等场景,显示定制化文案
- 「动态交互设计」:悬停放大、条形波动动画、平滑显示 / 隐藏过渡,增强用户粘性
- 「响应式布局」:适配 PC / 移动端,最大宽度限制 + 自动适配,不影响页面排版
- 「SEO 友好」:可自定义文案包含关键词,提升页面相关性,助力搜索引擎收录
![图片[1]-给网站添加灵动岛动态效果代码-轻松美化标题样式-微生之最](https://cos.swszz.cn/2026/01/20260117201944142-1024x182.webp)
![图片[2]-给网站添加灵动岛动态效果代码-轻松美化标题样式-微生之最](https://cos.swszz.cn/2026/01/20260117202037514-1024x229.webp)
第一步:添加动态灵动岛 CSS 样式(自定义 CSS 区域)
将以下代码粘贴到网站「自定义 CSS」区域(WordPress 路径:外观→自定义→额外 CSS;静态网页直接放入<style>标签内),定义灵动岛基础样式、动画效果与响应式规则。
/* 动态灵动岛美化样式代码*/
.dynamic-island {
position: fixed;
top: 80px;
left: 50%;
transform: translateX(-50%) scale(0);
transform-origin: center;
width: auto; /* 自动宽度 */
max-width: 80%; /* 最大宽度限制 */
height: 40px;
background-color: #000;
border-radius: 25px;
color: white;
display: flex;
align-items: center;
justify-content: space-between;
transition: transform 0.4s ease-in-out, height 0.6s ease-in-out, border-radius 0.6s ease-in-out, box-shadow 0.5s ease-in-out, opacity 0.5s ease-in-out;
overflow: visible;
z-index: 1000;
padding-left: 35px;
padding-right: 20px;
opacity: 0;
box-shadow: 0 0px 10px rgba(0, 0, 0, 0.45);
flex: 1; /* 使宽度自动扩展 */
white-space: nowrap; /* 防止文字换行 */
}
.dynamic-island.active {
transform: translateX(-50%) scale(1);
opacity: 1;
}
.dynamic-island.inactive {
transform: translateX(-50%) scale(0);
opacity: 0;
}
.island-content {
opacity: 0;
transition: opacity 0.9s ease-in-out, filter 0.8s ease-in-out;
font-weight: bold;
flex-grow: 1;
text-align: left; /* 文字左对齐 */
margin-left: 10px; /* 与图片留出空间 */
overflow: hidden; /* 防止溢出 */
text-overflow: ellipsis; /* 溢出显示省略号 */
}
.dynamic-island.active .island-content {
opacity: 1;
}
.dynamic-island img {
position: absolute;
left: 5px; /* 调整图片位置 */
width: 20px;
height: 20px;
object-fit: cover;
transition: height 0.8s ease-in-out, width 0.8s ease-in-out, filter 0.8s ease-in-out;
}
.dynamic-island:hover {
height: 60px;
border-radius: 50px;
}
.dynamic-island:hover img {
width: 30px;
height: 30px;
}
.bars {
display: flex;
align-items: center;
justify-content: flex-end;
gap: 3px;
min-width: 40px; /* 最小宽度限制 */
}
.bar {
width: 2px;
height: 13px;
background-color: green;
animation: bounce 1s infinite ease-in-out;
animation-direction: alternate;
}
.bar:nth-child(1) {
animation-duration: 1s;
}
.bar:nth-child(2) {
animation-duration: 0.9s;
}
.bar:nth-child(3) {
animation-duration: 0.8s;
}
.bar:nth-child(4) {
animation-duration: 0.7s;
}
.bar:nth-child(5) {
animation-duration: 0.6s;
}
.bar:nth-child(6) {
animation-duration: 0.9s;
}
.bar:nth-child(7) {
animation-duration: 0.7s;
}
@keyframes bounce {
0% {
transform: scaleY(0.3);
background-color: green;
}
50% {
transform: scaleY(1);
background-color: orange;
}
100% {
transform: scaleY(0.3);
background-color: green;
}
}
/* 动态灵动岛美化样式代码 结束*/
第二步:添加灵动岛 HTML 结构(自定义头部 HTML)
将以下代码粘贴到网站「自定义头部 HTML」区域(WordPress 路径:外观→自定义→自定义 HTML→头部 HTML;静态网页放入<head>标签内),构建灵动岛核心结构(含图标、文案、动画元素)。
<!-- 动态灵动岛美化样式代码 开始 !-->
<div class="dynamic-island inactive" id="dynamicIsland" style="opacity: 0;">
<img src="https://cos.swszz.cn/2026/01/20260117184144649.webp" alt="通知图标" width="30" height="30">
<div class="island-content">
<div class="bars" style="line-height: 50px; margin: 0;">
<p style="line-height: 50px; margin: 0; font-size: 12px; padding-right: 10px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis;"> 欢迎访问微生之最</p>
<div class="bar"></div>
<div class="bar"></div>
<div class="bar"></div>
<div class="bar"></div>
<div class="bar"></div>
<div class="bar"></div>
<div class="bar"></div>
</div>
</div>
</div>
<!-- 动态灵动岛美化样式代码 结束 !-->
第三步:添加动态逻辑代码(免修改主题,更新不失效)
方案 A:WordPress 用户(自定义 JS 区域)
粘贴到「外观→自定义→自定义 JS」或主题自带的「自定义 JavaScript」区域,无需修改function.php,主题更新不影响效果。
// 灵动岛通知 开始
function add_dynamic_island_script() {
?>
<script type="text/javascript">
window.onload = function() {
// 触发灵动岛的显示
triggerIsland();
// 获取当前页面的标题
let title;
const currentUrl = window.location.pathname;
// 获取当前 URL 路径
if (currentUrl.includes('/message/')) {
// 如果访问了消息页面
document.querySelector('.bars p').innerText = "正在访问消息页面";
} else if (currentUrl.includes('/user/')) {
// 如果访问了用户中心页面
document.querySelector('.bars p').innerText = "欢迎来到用户中心";
} else if (document.body.classList.contains('home') || document.body.classList.contains('front-page')) {
// 如果是首页
document.querySelector('.bars p').innerText = "欢迎来到<a target="_blank" href="https://yinhenote.cn/tag/%e9%93%b6%e6%b2%b3%e7%ac%94%e8%ae%b0/" title="View all posts in 银河笔记">银河笔记</a>";
} else if (document.body.classList.contains('single')) {
// 如果是单篇文章
title = "<?php echo addslashes(html_entity_decode(get_the_title())); ?>";
// 获取文章标题并解码
document.querySelector('.bars p').innerText = "正在访问:" + title;
} else if (document.body.classList.contains('category')) {
// 如果是分类页面
const category = "<?php echo addslashes(html_entity_decode(get_queried_object()->name)); ?>";
// 获取当前分类的名称
document.querySelector('.bars p').innerText = "正在访问:" + category + " 分类";
} else if (document.body.classList.contains('page')) {
// 如果是单个页面
title = "<?php echo addslashes(html_entity_decode(get_the_title())); ?>";
// 获取页面标题并解码
document.querySelector('.bars p').innerText = "正在访问:" + title;
} else {
// 如果以上都不匹配,可以使用默认值
document.querySelector('.bars p').innerText = "欢迎来到银河笔记";
}
}
;
// 触发灵动岛的显示
function triggerIsland() {
const island = document.getElementById('dynamicIsland');
if (island) {
island.style.opacity = 1;
island.classList.add('active')
island.classList.remove('inactive');
// 在4秒后触发关闭动画
setTimeout(() => {
closeIsland();
}
, 4000);
}
}
function closeIsland() {
const island = document.getElementById('dynamicIsland')
if (island) {
island.classList.remove('active');
island.classList.add('inactive');
setTimeout(() => {
island.style.opacity = 0;
// 使灵动岛透明
}
, 600);
// 与 transform 动画持续时间一致
}
}
</script>
<?php
}
add_action('wp_head', 'add_dynamic_island_script');
// 灵动岛 结束
方案 B:静态网页用户(直接放入前)
将以下代码粘贴到 HTML 文件的</body>标签前,无需依赖任何框架,直接生效。
© 版权声明
本站资源均来自互联网,请勿商业运营,仅供学习和研究,请在下载后24小时内删除,如有侵权请发送邮件至ws@swszz.cn,我们将在七个工作日内处理!!
THE END

















暂无评论内容