博客右键美化代码
<!-- 右键美化 -->
<style type="text/css">
:root {
    /* 亮色模式 - 基于 #8f4b28 的浅色衍生 */
    --menu-bg: #f8f1ec;                /* 浅米白,与主色同色系但更浅 */
    --menu-text: #5a3922;              /* 比主色稍深的棕色,确保可读性 */
    --menu-hover-bg: rgba(143, 75, 40, 0.15);  /* 主色半透明悬浮 */
    --menu-hover-text: #8f4b28;        /* 悬浮时显示主色 */
    --menu-border: #e0d0c5;            /* 浅棕色边框 */
}

[data-theme="dark"] {
    /* 暗色模式 - 基于 #8f4b28 的深色衍生 */
    --menu-bg: #2a1a10;                /* 深棕黑背景 */
    --menu-text: #d4b9a8;              /* 浅米黄文字 */
    --menu-hover-bg: rgba(143, 75, 40, 0.6);   /* 主色高透明度悬浮 */
    --menu-hover-text: #fff;           /* 悬浮文字白色 */
    --menu-border: #4a3324;            /* 深棕色边框 */
}

a { text-decoration: none; }

div.usercm {
    background: var(--menu-bg) !important;
    color: var(--menu-text);
    font-size: 13px !important;
    width: 150px;
    box-shadow: 0px 0px 15px rgba(0,0,0,0.2);
    position: absolute;
    display: none;
    z-index: 10000;
    opacity: 0.95;
    border-radius: 8px;
    padding: 5px 0;
}

div.usercm ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
}

div.usercm ul li {
    margin: 0;
    padding: 0;
    line-height: 35px;
    border-bottom: 1px solid var(--menu-border); /* 使用变量 */
}

div.usercm ul li a {
    color: var(--menu-text) !important; /* 统一使用变量 */
    padding: 0 15px;
    display: block;
    transition: all 0.2s;
}

div.usercm ul li a:hover {
    background: var(--menu-hover-bg) !important;
    color: var(--menu-hover-text) !important; /* 使用悬停文字变量 */
}

div.usercm ul li a i {
    margin-right: 10px;
}

a.disabled {
    color: #c8c8c8 !important;
    cursor: not-allowed;
}

a.disabled:hover {
    background-color: transparent !important;
}
</style>

<div class="usercm" style="display: none;">
    <ul>
        <li><a href="/"><i class="fa fa-home fa-fw"></i><span>首页</span></a></li>
        <li><a href="javascript:void(0);" onclick="getSelect();"><i class="fa fa-copy fa-fw"></i><span>复制</span></a></li>
        <li><a href="javascript:history.go(1);"><i class="fa fa-arrow-right fa-fw"></i><span>前进</span></a></li>
        <li><a href="javascript:history.go(-1);"><i class="fa fa-arrow-left fa-fw"></i><span>后退</span></a></li>
        <li style="border-bottom:1px solid #eee"><a href="javascript:window.location.reload();"><i class="fa fa-refresh fa-fw"></i><span>刷新</span></a></li>
        <li><a href="javascript:void(0);" onclick="googleSearch();"><i class="fa fa-globe"></i><span>必应</span></a></li>
        <li><a href="javascript:void(0);" onclick="baiduSearch();"><i class="fa fa-paw fa-fw"></i><span>百度</span></a></li>
    </ul>
</div>

<script>
function updateLayerTheme() {
    // 通过自定义 CSS 设置字体颜色和其他样式
    const style = document.createElement('style');
    style.innerHTML = `
        /* 修改消息框背景颜色和字体颜色 */
        .layui-layer-msg .layui-layer-content {
            color: #8f4b28;  /* 字体颜色使用主题色 */
        }
        .layui-layer-msg .layui-layer-title {
            background-color: #8f4b28;  /* 标题背景使用主题色 */
            color: #fff;                /* 标题字体颜色为白色,确保对比度 */
        }
        .layui-layer-msg .layui-layer-content.success {
            color: #8f4b28;  /* 成功信息字体颜色 */
        }
        .layui-layer-msg .layui-layer-content.error {
            color: #ff6b6b;  /* 错误信息字体颜色 */
        }

        /* 让 Layer 弹窗本身也使用主题颜色 */
        .layui-layer-msg {
            border-color: #8f4b28;  /* 使用主题色作为边框颜色 */
        }

        /* 定义消息框背景颜色 */
        .layui-layer-msg .layui-layer-content {
            background-color: #f4f4f4;  /* 设置背景色为浅灰色 */
        }
    `;
    document.head.appendChild(style);

    // 关闭所有通知,确保更新后的主题配置生效
    layer.closeAll();
}

// 主题切换逻辑
document.getElementById('fabtn_toggle_darkmode').addEventListener('click', function() {
    const body = document.body;
    const isDark = body.getAttribute('data-theme') === 'dark';
    body.setAttribute('data-theme', isDark ? 'light' : 'dark');
    localStorage.setItem('theme', isDark ? 'light' : 'dark');
    updateLayerTheme();  // Layer 主题
});

// 初始化主题
const savedTheme = localStorage.getItem('theme');
if (savedTheme) {
    document.body.setAttribute('data-theme', savedTheme);
    updateLayerTheme();  // 初始化时固定主题色
}
</script>

<script type="text/javascript">
// 使用安全的jQuery封装模式
(function($) {
    // 1. 定义jQuery插件方法
    $.extend({
        mouseMoveShow: function(selector) {
            $(window).mousemove(function(event) {
                var winWidth = $(window).width();
                var winHeight = $(window).height();
                var pageX = event.pageX;
                var pageY = event.pageY;
                
                // 调整位置防止超出窗口
                if (event.clientX + $(selector).width() >= winWidth) {
                    pageX = pageX - $(selector).width() - 5;
                }
                if (event.clientY + $(selector).height() >= winHeight) {
                    pageY = pageY - $(selector).height() - 5;
                }
                
                // 右键菜单显示控制
                $("html").off("contextmenu").on("contextmenu", function(e) {
                    if (e.which === 3) { // 右键点击
                        $(selector).css({
                            left: pageX,
                            top: pageY
                        }).show();
                        return false; // 阻止默认右键菜单
                    }
                });
            });
            
            // 点击其他地方隐藏菜单
            $(document).on("click", function() {
                $(".usercm").hide();
            });
        },
        disabledContextMenu: function() {
            window.oncontextmenu = function() {
                return false;
            }
        }
    });

    // 2. 移动设备检测(优化版)
    function isMobileDevice() {
        return /Android|iPhone|iPad|iPod|Windows Phone/i.test(navigator.userAgent);
    }

    // 3. 复制选中文本功能
    window.getSelect = function() {
        var selectedText = window.getSelection ? window.getSelection().toString() : 
                         document.selection.createRange().text;
        if (!selectedText) {
            layer.msg('请先选择要复制的内容!', {time: 2000, icon: 0}); 
        } else {
            document.execCommand("Copy");
            layer.msg('已复制选中内容!', {time: 2000, icon: 1}); 
        }
    };

    // 4. 百度搜索功能
    window.baiduSearch = function() {
        var selectedText = window.getSelection ? window.getSelection().toString() : 
                         document.selection.createRange().text;
        if (!selectedText) {
            layer.msg('请先选择要搜索的内容', {time: 2000, icon: 2}); 
        } else {
            window.open("https://www.baidu.com/s?wd=" + encodeURIComponent(selectedText));
        }
    };

    // 5. 必应搜索功能
    window.googleSearch = function() {
        var selectedText = window.getSelection ? window.getSelection().toString() : 
                         document.selection.createRange().text;
        if (!selectedText) {
            layer.msg('请先选择要搜索的内容', {time: 2000, icon: 2}); 
        } else {
            window.open("https://cn.bing.com/search?q=" + encodeURIComponent(selectedText));
        }
    };

    // 6. 初始化执行(确保DOM加载完成)
    $(document).ready(function() {
        if (!isMobileDevice()) {
            $.mouseMoveShow(".usercm");
            $.disabledContextMenu();
        }
    });

})(jQuery);
</script>
<!-- 右键美化结束 -->

评论

  1. CNVHK
    博主
    Windows Edge
    已编辑
    1 年前
    2025-8-26 16:13:36

    测试

发送评论 编辑评论


				
上一篇
下一篇