Xlenco

Xlenco

github
zhihu
email

Butterfly美化魔改:自訂右鍵菜單

最近發現DoraKika寫的右鍵菜單,風格簡潔,十分好看。並且支持fontawesome

可以在 rightmenu.pug 中自行添加

下面帶來教程

創建 rightmenu.pug 文件#

/themes/butterfly/layout/includes

在 /includes/dorakika 裡創建 rightmenu.pug 文件

dorakika 不是自帶的文件夾,而是博主創建的方便管理的,推薦也創建個。

rightmenu.pug#

#rightMenu
	.rightMenu-group.rightMenu-small
		a.rightMenu-item(href="javascript:window.history.back();")
			i.fa-solid.fa-arrow-left
		a.rightMenu-item(href="javascript:window.location.reload();")
			i.fa-solid.fa-arrow-rotate-right
		a.rightMenu-item(href="javascript:window.history.forward();")
			i.fa-solid.fa-arrow-right
		a.rightMenu-item#menu-radompage(href='https://nicexl.vercel.app/')
			i.fa-solid.fa-house
	.rightMenu-group.rightMenu-line.hide#menu-text
		a.rightMenu-item(href="javascript:kk.copySelect();")
			i.fa-solid.fa-copy
			span='複製'
	.rightMenu-group.rightMenu-line
		a.rightMenu-item(href="javascript:kk.switchDarkMode();")
			i.fa-solid.fa-circle-half-stroke
			span='昼夜切換'
		a.rightMenu-item(href="javascript:kk.switchReadMode();")
			i.fa-solid.fa-book
			span='閱讀模式'

修改 layout.pug 文件#

添加如下內容

!=partial('includes/dorakika/rightmenu',{}, {cache:true})

創建 rightmenu.css 文件#

在如下目錄創建

/themes/butterfly/source/css
/* rightMenu右鍵菜單 */
#rightMenu{
	display: none;
	position: fixed;
  padding: 0 0.25rem;
	width: 9rem;
	height: fit-content;
	top: 10%;
	left: 10%;
	background-color: var(--heo-card-bg);
  color: var(--heo-fontcolor);
	border-radius: 12px;
	z-index: 102;
  border: var(--style-border);
  transition: 0.3s;
}
#rightMenu:hover{
  border: var(--style-border-hover);
  box-shadow: var(--heo-shadow-theme);
}
#rightMenu .rightMenu-group{
	padding: 0.35rem 0.3rem;
}
#rightMenu .rightMenu-group:not(:nth-last-child(1)){
	border-bottom: 1px dashed var(--heo-theme-op);
}
#rightMenu .rightMenu-group.rightMenu-small{
	display: flex;
	justify-content: space-between;
}
#rightMenu .rightMenu-group .rightMenu-item{
	border-radius: 8px;
	transition: 0.3s;
  cursor: pointer;
}

#rightMenu .rightMenu-line .rightMenu-item{
  margin: 0.25rem 0;
  padding: 0.25rem 0;
}
#rightMenu .rightMenu-group.rightMenu-line .rightMenu-item{
	display: flex;
}
#rightMenu .rightMenu-group .rightMenu-item:hover{
	background-color: var(--heo-theme);
  color: var(--heo-white);
  box-shadow: var(--heo-shadow-theme);
}
#rightMenu .rightMenu-group .rightMenu-item i{
	display: inline-block;
	text-align: center;
	line-height: 1.5rem;
	width: 1.5rem;
	padding: 0 0.25rem;
}
#rightMenu .rightMenu-line .rightMenu-item i{
  margin: 0 0.25rem;
}
#rightMenu .rightMenu-group .rightMenu-item span{
	line-height: 1.5rem;
}
#rightmenu-mask{
  position: fixed;
  width: 100vw;
  height: 100vh;
  background: var(--heo-maskbg);
  top: 0;
  left: 0;
  display: none;
  z-index: 101;
  margin: 0!important;
}

創建 rightmenu.js 文件#

在如下目錄裡創建

/themes/butterfly/source/js
let kk = {};
kk.showRightMenu = function(isTrue, x=0, y=0){
    let $rightMenu = $('#rightMenu');
    $rightMenu.css('top',x+'px').css('left',y+'px');

    if(isTrue){
        $rightMenu.show();
    }else{
        $rightMenu.hide();
    }
}
kk.switchDarkMode = function(){
    const nowMode = document.documentElement.getAttribute('data-theme') === 'dark' ? 'dark' : 'light'
    if (nowMode === 'light') {
        activateDarkMode()
        saveToLocal.set('theme', 'dark', 2)
        GLOBAL_CONFIG.Snackbar !== undefined && btf.snackbarShow(GLOBAL_CONFIG.Snackbar.day_to_night)
    } else {
        activateLightMode()
        saveToLocal.set('theme', 'light', 2)
        GLOBAL_CONFIG.Snackbar !== undefined && btf.snackbarShow(GLOBAL_CONFIG.Snackbar.night_to_day)
    }
    // 處理一些情況
    typeof utterancesTheme === 'function' && utterancesTheme()
    typeof FB === 'object' && window.loadFBComment()
    window.DISQUS && document.getElementById('disqus_thread').children.length && setTimeout(() => window.disqusReset(), 200)
};
kk.switchReadMode = function(){
    const $body = document.body
    $body.classList.add('read-mode')
    const newEle = document.createElement('button')
    newEle.type = 'button'
    newEle.className = 'fas fa-sign-out-alt exit-readmode'
    $body.appendChild(newEle)

    function clickFn () {
        $body.classList.remove('read-mode')
        newEle.remove()
        newEle.removeEventListener('click', clickFn)
    }

    newEle.addEventListener('click', clickFn)
}
kk.switchTheme=function(load=false){
    //空字符串表示butterfly原版主題(即不加載css)
    //FallGuys.css是我自己的魔改主題,需替換
    let themes = ['FallGuys.css',''];
    let vTheme = parseInt(localStorage.getItem('visitor-theme'));
    if(!vTheme){
        vTheme = load?0:1;
    }else{
        vTheme += load?0:1;
        vTheme%=themes.length;
    }
    localStorage.setItem('visitor-theme',vTheme)
    let themesrc = ''
    if(themes[vTheme]){
        themesrc += window.location.origin+'/css/dorakika/'+themes[vTheme];
    }
    //css引入時link標籤添加屬性tag="theme"
    let themeLink = $(document).find('[tag="theme"]')[0];
    if(themeLink)themeLink.href = themesrc;
};

//複製選中文字
kk.copySelect = function(){
    document.execCommand('Copy',false,null);
    //這裡可以寫點東西提示一下 已複製
}

//回到頂部
kk.scrollToTop = function(){
    btf.scrollToDest(0, 500);
}

// 右鍵菜單事件
if(! (navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i))){
    window.oncontextmenu = function(event){
        $('.rightMenu-group.hide').hide();
        if(document.getSelection().toString()){
            $('#menu-text').show();
        }

        console.log(event.target);
        let pageX = event.clientX + 10;
        let pageY = event.clientY;
        let rmWidth = $('#rightMenu').width();
        let rmHeight = $('#rightMenu').height();
        if(pageX + rmWidth > window.innerWidth){
            pageX -= rmWidth+10;
        }
        if(pageY + rmHeight > window.innerHeight){
            pageY -= pageY + rmHeight - window.innerHeight;
        }

        kk.showRightMenu(true, pageY, pageX);
        return false;
    };

    window.addEventListener('click',function(){kk.showRightMenu(false);});
    //     window.addEventListener('load',function(){kk.switchTheme(true);});
}

在 head 和 bottom 分別引入 js 和 css#

在 head 引入如下內容

- <link rel="stylesheet" href="/css/rightmenu.css">

在 bottom 引入如下內容

- <script src="/js/rightmenu.js"></script>
- <script src="https://npm.elemecdn.com/[email protected]/dist/jquery.min.js"></script>

然後就大功告成了!(◍˃̶ᗜ˂̶◍)✩

載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。