

为博客添加返回顶部按钮
source link: https://xiang578.com/post/add-return-button-to-blog.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

为博客添加返回顶部按钮
适用于WordPress
由于主题没有自带的返回顶部按钮,所以我一直在寻找一种解决方案。之前使用的是wordpress插件提供的返回顶部按钮,后来在网上乱逛,发现有Xnces – 衔铁部落的返回顶部按钮非常的酷炫,正是我要寻找的。于是,我在那个博客下留言询问制作方法。 前几天,看到那个博客上出现了一篇文章-本博客的返回顶部效果。 按照上面的方法,折腾一会儿,我也制作出来了,特地记录一下。
首先,先加载一下环境。在后台
functions.php
中找到ms_scripts()
函数,添加wp_enqueue_script( 'jquery' );
调用wordpress默认的JQuery文件。我将这个效果有关的js代码放在了主题目录下新建的
themes.js
文件中。所以在上面的函数中添加下面两句话导入这个文件wp_register_script( 'themes_js', THEMEPATH . '/themes.js',array());
和wp_enqueue_script( 'themes_js' );
CSS代码我直接加在了主题
style.css
中。重新打开网站就能看到效果了。
themes.js
文件中代码如下
var bigfa_scroll = {
drawCircle: function(id, percentage, color) {
var width = jQuery(id).width();
var height = jQuery(id).height();
var radius = parseInt(width / 2.20);
var position = width;
var positionBy2 = position / 2;
var bg = jQuery(id)[0];
id = id.split("#");
var ctx = bg.getContext("2d");
var imd = null;
var circ = Math.PI * 2;
var quart = Math.PI / 2;
ctx.clearRect(0, 0, width, height);
ctx.beginPath();
ctx.strokeStyle = color;
ctx.lineCap = "square";
ctx.closePath();
ctx.fill();
ctx.lineWidth = 3;
imd = ctx.getImageData(0, 0, position, position);
var draw = function(current, ctxPass) {
ctxPass.putImageData(imd, 0, 0);
ctxPass.beginPath();
ctxPass.arc(positionBy2, positionBy2, radius, -(quart), ((circ) * current) - quart, false);
ctxPass.stroke();
}
draw(percentage / 100, ctx);
},
backToTop: function($this) {
$this.click(function() {
jQuery("body,html").animate({
scrollTop: 0
},
800);
return false;
});
},
scrollHook: function($this, color) {
color = color ? color: "#000000";
$this.scroll(function() {
var docHeight = (jQuery(document).height() - jQuery(window).height()),
$windowObj = $this,
$per = jQuery(".per"),
percentage = 0;
defaultScroll = $windowObj.scrollTop();
percentage = parseInt((defaultScroll / docHeight) * 100);
var backToTop = jQuery("#backtoTop");
if (backToTop.length > 0) {
if ($windowObj.scrollTop() > 200) {
backToTop.addClass("button--show");
} else {
backToTop.removeClass("button--show");
}
$per.attr("data-percent", percentage);
bigfa_scroll.drawCircle("#backtoTopCanvas", percentage, color);
}
});
}
}
jQuery(document).ready(function() {
jQuery("body").append('<div id="backtoTop" data-action="gototop"><canvas id="backtoTopCanvas" width="48" height="48"></canvas><div class="per"></div></div>');
var T = bigfa_scroll;
T.backToTop(jQuery("#backtoTop"));
T.scrollHook(jQuery(window), "#555555");
});
CSS文件要添加的代码
#backtoTop{
background-color:#eee;
border-radius:100%;
bottom:10%;height:48px;
position:fixed;
right:-100px;
width:48px;
transition:0.5s;
-webkit-transition:0.5s
}
#backtoTop.button--show{
right:10px
}
.per{
font-size:16px;
height:48px;
line-height:48px;
position:absolute;
text-align:center;
top:0;
width:48px;
color:#555;
cursor:pointer
}
.per:before{
content:attr(data-percent)
}
.per:hover:before{
content:"↑";font-size:20px
}
Recommend
-
16
博客顶部个人中心点击没反应,提问发图片不显示,佩服了 - OSCHINA - 中文开源技术交流社区 开源问答...
-
8
谈导航栏返回按钮的替代方案 呼,又要夜间上新了。其实自己偶尔还会进去关于Be For Web里面看看将近两年前写的博客开篇语。当时的动力现在仍在,当...
-
8
在实际项目开发中, 我们在进行页面跳转时, 偶尔会跳到不可返回的页面(比如退出登录后). 本篇文章就来记录下 Flutter 如何隐藏/移除导航栏的默认返回按钮. 有图有真相, 先来看下实际效果: 移除导航栏的默认返回按钮适用于:
-
8
去掉 iOS 导航栏返回按钮文本三种方案 去掉 iOS 导航栏返回按钮文本三种方案 ...
-
3
经常在某些文档中或者文章中可以看到这样一个"返回顶部"的功能,具体有两个交互只有滚动一定距离才会出现,返回到顶部重新隐藏点击会返回到顶部比如
-
11
Hexo 添加返回顶部按钮Hexo 已经为用户实现了清晰的模块化设计,我们只需要根据规范编写合适的 EJS 和 JS 文件放入指定目录并在所需位置进行引用即可,这里给大家分享如何为 Hexo 增加返回顶部功能。 返回顶部按钮已经成为一种习惯
-
7
建议给网页版发帖/回帖按钮添加去重(保证幂等) [复制链接] 分享: 楼主
-
6
之前介绍了 wordpress文本编辑器快速插入短代码 和 wordpress文本编辑器添加按钮 不过一般来说我们常用的是可视化面板写作 而且像我分享php代码(<pre></pre>) 经常要在可视化和文本模式下进行切换 会经常出错 下文讲的是:在默认编辑...
-
4
返回按钮和关闭按钮,你用的对吗? 小乞丐 2022-04-29 3 评论...
-
3
敢一行,则予知。脚踏实地,知行合一。 添加如下即可:
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK