[英]Scroll to top button
I was thinking to myself how great the tumblr “scroll to top” button was. 我在想自己“滚动到顶部”按钮的功能多么出色。 And then I was also thinking to myself how much it sucked that Deviantart doesn't have a button like that.
然后我也在想自己,Deviantart没有这样的按钮有多糟。 Now I know, I can easily scroll to the top of the page by pressing the “home” button on my keyboard.
现在我知道,通过按键盘上的“主页”按钮,可以轻松滚动到页面顶部。 But what if I am too busy scrolling?
但是,如果我太忙于滚动,该怎么办? Or I simply do not want to fuss with my keyboard?
还是我根本不想在键盘上大惊小怪?
Well, I found a chrome extension that offers a similar function to what I want to do. 好吧,我发现了chrome扩展程序 ,它提供了与我想要执行的功能类似的功能。 It was a scroll to top button for 4Chan, but I adapted the manifest,json to use Deviantart instead.
这是滚动到4Chan的顶部按钮,但我修改了manifest,json改为使用Deviantart。 BUT, there is a problem with the code.
但是,代码有问题。
What I want the button to do, is only show up if the user has scrolled down more than 100px on the screen. 我希望按钮执行的操作仅在用户在屏幕上向下滚动超过100px时显示。 I also would like there to be some sort of animated scroll, similar to the way tumblr scrolls, but that is not necessary.
我也希望有某种动画滚动,类似于tumblr滚动的方式,但这不是必需的。
But what it currently does, is show a static button at the top right of the page, that I can only click when I am at the bottom most part of the page. 但是它当前所做的是在页面右上方显示一个静态按钮,只有当我位于页面最底部时才能单击。
Here is the main.js code: 这是main.js代码:
/*
* scroll to top button for deviantart
*
*/
function loadButton()
{
button = document.createElement("IMG");
button.setAttribute("onclick", "scrollTo(0)");
button.setAttribute("onmouseover", "this.style.opacity = '1'");
button.setAttribute("onmouseout", "this.style.opacity = '0.4'");
button.src = chrome.extension.getURL("images/scroll-to-top.png");
button.style.position = "fixed";
button.style.right = "10px";
button.style.top = "10px";
button.style.width = "75px";
button.style.height = "75px";
button.style.opacity = "0.4";
document.body.appendChild(button);
}
loadButton();
I have very minimal javascript knowledge, so any thoughts are welcome! 我对JavaScript的了解非常少,因此欢迎任何想法!
ALSO, A QUICK DISCLAIMER: The base code is not mine, and therefore, I do not intend to distribute it. 另请注意:基本代码不是我的,因此,我不打算分发它。 I am looking to code this for my personal use, and that alone.
我希望将其编码为个人使用,仅此而已。
Put this code at the end of your loadButton
function. 将此代码放在
loadButton
函数的末尾。 It hides/shows the button depending on whether the user has scrolled past 100px. 它根据用户是否滚动超过100像素来隐藏/显示按钮。
window.onscroll = function () {
if (document.documentElement.scrollTop > 100) {
button.style.display = "inline";
} else {
button.style.display = "none";
}
}
It does not do any animation, but for that you can try some jQuery smooth scrolling plugin. 它不做任何动画,但是为此您可以尝试一些jQuery平滑滚动插件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.