【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中

网站添加灵动岛美化样式&WordPress插件

一、简介

这是一款模仿苹果灵动岛的一个样式代码,每刷新一次页面,都会通过灵动岛显示页面标题。

二、效果

图片[1]-网站添加灵动岛美化样式&WordPress插件-聆风小站

三、代码部署

1、css代码

<!-- 灵动岛CSS 开始 -->
<style type="text/css">
.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;
    }
}
</style>
<!-- 灵动岛CSS 结束 -->

2、html代码

<!-- 灵动岛HTML 开始 -->
<div class="dynamic-island inactive" id="dynamicIsland" style="opacity: 0;">
    <img src="https://img.alicdn.com/imgextra/i1/2210123621994/O1CN01lajerM1QbIl9aoHcJ_!!2210123621994.png" 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>
<!-- 灵动岛HTML 结束 -->

3、js代码

// 灵动岛JS 放子比functions.php 开始
function add_dynamic_island_script() {
    ?>
    <script type="text/javascript">
    window.onload = function() {
        // 触发灵动岛的显示
        triggerIsland();
        // 获取当前页面的标题
        let title;
        // 获取当前 URL 路径
        const currentUrl = window.location.pathname;
        
        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 = "欢迎来到:" + <?php echo json_encode(get_bloginfo('name')); ?>;
        } else if (document.body.classList.contains('single')) {
            // 文章页面,获取文章标题
            title = <?php echo json_encode(html_entity_decode(get_the_title())); ?>;
            document.querySelector('.bars p').innerText = "正在访问:" + title;
        } else if (document.body.classList.contains('category')) {
            // 分类页面,获取当前分类的名称
            const category = <?php echo json_encode(html_entity_decode(get_queried_object()->name ?? '')); ?>;
            document.querySelector('.bars p').innerText = "正在访问:" + category + " 分类";
        } else if (document.body.classList.contains('page')) {
            // 单个页面,获取页面标题并解码
            title = <?php echo json_encode(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');
            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);
        }
    }
    </script>
    <?php
}
add_action('wp_head', 'add_dynamic_island_script');
// 灵动岛JS 放子比functions.php 结束

四、WP插件

由于js代码要修改主题文件,为防止因主题更新导致样式失效,将此代码整合为WordPress插件形式,直接安装启动即可使用。


内容仅供学习,禁止非法商用。严禁使用本站资源从事任何非法活动!
© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容