Joe 主题 6.xx 增加顶部下拉隐藏,并显示文章标题

chen'mo
2021-03-29 / 0 评论 / 2,990 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2021年03月29日,已超过1118天没有更新,若内容或图片失效,请留言反馈。

效果和本博客在查看文章时的效果一致,当下滑的时候,顶部会自动收起并显示文章的标题。

原理没啥好讲的,就是监听下滚动时间,判断下滚动距离。

教程

添加到自定义css:

@keyframes hideIndex {
  0% {
    opacity: 0;
    transform: translate(0, 30px);
  }
  100% {
    opacity: 1;
    transform: translate(0, 0);
  }
}
.post_no {
  display: none !important;
}

body #Joe {
  padding-top: 105px !important;
}
body #Joe > header {
  position: fixed;
  width: 100%;
}
body #Joe #post_top_title {
  font-size: 16px;
  line-height: 50px;
  font-weight: 600;
  width: 100%;
  animation: hideIndex 0.6s;
  -moz-animation: hideIndex 0.6s;
  -webkit-animation: hideIndex 0.6s;
  -o-animation: hideIndex 0.6s;
}
body #Joe #post_top_title span {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--main);
}

@media (max-width: 768px) {
  body #Joe {
    padding-top: 55px !important;
  }
}

自定义js:

$(document).ready(function(){
if (!document.querySelector("#post_top_title span").innerHTML){
            return
        }
        let header = document.querySelector("header.joe_header")
        let row_above = $(".joe_header__above")
        let above_nav = $(".joe_header__above .joe_container:first-child")
        let below = $(".joe_header__below")
        let post_title = $("#post_top_title")
        let canSlideDown = true
        let canSlideUp = true
        let caluSideBar = function () {
            // 计算侧边栏最后一个元素的高度
            let ele = $('.joe_aside__item:last-child')
            ele.css('transition','top 100ms');
            ele.css('top', $('.joe_header').height() + 15)
        }
        let showNav = function(){
            post_title.addClass("post_no")
            above_nav.removeClass("post_no")
            below.slideDown("fast",function (){
                canSlideDown = true
                caluSideBar()
            })
        }
        let hideNav = function(){
            post_title.removeClass("post_no");
            above_nav.addClass("post_no")
            below.slideUp("normal",function () {
                canSlideUp = true
                caluSideBar()
            })
        }

        let lastScrollPos = 0
        if(screen.width < 768) {
            $(window).scroll(function() {
                let scrollPos = $(window).scrollTop(); //得到滚动的距离
                if (scrollPos > 395 && scrollPos < 505) return // 防止nav出现触发再次scroll
                if (scrollPos >= 450) { //比较判断是否fixed
                    if (lastScrollPos > scrollPos && canSlideUp){
                        canSlideDown = false
                        row_above.slideDown("fast",function (){
                            canSlideDown = true
                        })
                    }
                    else{
                        if (canSlideDown){
                            canSlideUp = false
                            row_above.slideUp("normal",function () {
                                canSlideUp = true
                            })
                        }
                    }
                } else {
                    row_above.slideDown("fast",function (){
                        canSlideDown = true
                    })
                }
                lastScrollPos = scrollPos
            })
        }else {
            let navOffw = header.offsetWidth
            if (post_title.length > 0 && navOffw > 750) {
                $(window).scroll(function() {
                    let scrollPos = $(window).scrollTop(); //得到滚动的距离
                    if (scrollPos > 400 && scrollPos < 500) return // 防止nav出现触发再次scroll
                    if (scrollPos >= 450) { //比较判断是否fixed
                        if (lastScrollPos > scrollPos && canSlideUp){ //向上滚动举例超过100
                            canSlideDown = false
                            showNav()
                        }
                        else{
                            if (canSlideDown){
                                canSlideUp = false
                                hideNav()
                            }
                        }
                    } else {
                        showNav()
                    }
                    lastScrollPos = scrollPos
                })

            }
        }

})

添加 html:

在 Joe/public/header.php 文件顶部找到

<header class="joe_header">
----<div class="joe_header__above">
--------<div class="joe_container">
------------xxxxx很多代码
--------</div>
----</div> // 找到这一个闭合的标签

说白了就是在 <div class="joe_header__above"> 这个标签对应的闭合标签</div>的前面插入:

<header class="joe_header">
----<div class="joe_header__above">
--------<div class="joe_container">
------------xxxxx很多代码
--------</div>
--------<div class="joe_container post_no" id="post_top_title"><span><?php if ($this->is('post')): ?><span><?php $this->title(); ?></span><?php endif; ?></div>
----</div> // 找到这一个闭合的标签,在前面插入

刷新一下大功告成。

那一年 · 3月29日
宝塔联合腾讯云撸28元2年国内1H1G3M轻量服务器
« 上一篇 03-24
Joe 主题 6.xx 底部增强,显示标签及二维码分享
下一篇 » 03-30

评论 (0)

插入图片