Using jQuery to add a dynamic “Back To Top” floating button with smooth scroll
Ever read a really long blog post or article and then had to scroll all the way up to the top of the screen to get to the menu? It can be a little frustrating. It’s easy to fix, you can have a fixed menu or as you’ll see here you can provide a quick and stylish way to get back to the top.
Getting Started
HTML wise all we need to do is add a “back to top” link at the bottom of the blog post
<a href="#" class="back-to-top">Back to Top</a>

We then need to style and position it. Here I’m setting its position to fixed and moving it to the bottom right side of the screen. I’ve also given it a semi-transparent background and finally hidden it. To make it stand out a little more, I’ve also given it a hover effect to darken the background a little.
.back-to-top {
position: fixed;
bottom: 2em;
right: 0px;
text-decoration: none;
color: #000000;
background-color: rgba(235, 235, 235, 0.80);
font-size: 12px;
padding: 1em;
display: none;
}
.back-to-top:hover {
background-color: rgba(135, 135, 135, 0.50);
}

The jQuery
First off we need to add jQuery to our page, you can do this by adding a script tag like this
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
Or, if you’re using WordPress like this using add_action and wp_enqueue_script
function theme_enqueue_script(){
wp_enqueue_script('jquery');
}
add_action('wp_enqueue_scripts', 'theme_enqueue_script');
The actual jQuery function is pretty simple, first we set our offset (after how much scrolling from the top we want the button to appear) and how long we want the scroll to top effect to last. We don’t want it to be too quick, but if it’s really slow it will be annoying. I’ve set it here to half a second.
We then need to make our button visible when the user scrolls. To do this we use the jQuery scroll function. In this function we grab the current scroll position using scrollTop , check to see if it is greater than our offset and if it is we show the button using the fadeIn function. If it’s not we make sure the button is not visible using the fadeOut function.
To actually scroll to the top, we need to intercept the click event on our button. First we prevent the default click from being triggered, and then we scroll back to the top using the animate function, passing in our duration. Finally we return false to ensure that no other events are raised after this.
jQuery(document).ready(function() {
var offset = 220;
var duration = 500;
jQuery(window).scroll(function() {
if (jQuery(this).scrollTop() > offset) {
jQuery('.back-to-top').fadeIn(duration);
} else {
jQuery('.back-to-top').fadeOut(duration);
}
});
jQuery('.back-to-top').click(function(event) {
event.preventDefault();
jQuery('html, body').animate({scrollTop: 0}, duration);
return false;
})
});
And here it is, a quick, simple but effective way of getting back to the top of a page.
http://www.tuicool.com/articles/iEZFBv
Using jQuery to add a dynamic “Back To Top” floating button with smooth scroll的更多相关文章
- 第二十三课:jQuery.event.add的原理以及源码解读
本课主要来讲解一下jQuery是如何实现它的事件系统的. 我们先来看一个问题: 如果有一个表格有100个tr元素,每个都要绑定mouseover/mouseout事件,改成事件代理的方式,可以节省99 ...
- 错误源:WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for 'jquery'. Please add a ScriptResourceMapping named jquery(case-sensitive).
Server Error in '/' Application. WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping ...
- WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for 'jquery'. Please add a ScriptResourceMapping named jquery(case-sensitive).
新开一个Web site.没有使用jQuery,当Insus.NET使用一些验证控件时,如RequiredfieldValidator,程序出现下面错误: WebForms UnobtrusiveVa ...
- jquery的add()用法总结
<!DOCTYPE html> <html> <head lang="en"> <meta charset="utf-8&quo ...
- jquery的add()方法扩大选择返回
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- [Tools] Add a Dynamic Tweet Button to a Webpage
To let people easily share the patio11bot, we'll add a "Tweet" button to the page. You can ...
- jquery remove/add css
<input type="submit" class="btn btn-primary" id="submit" value=&quo ...
- jQuery EasyUI - Add link to datagrid cell
Extracted from: http://stackoverflow.com/questions/16061894/jquery-easyui-add-link-to-cell HTML: < ...
- 原生JS和JQuery代码编写窗口捕捉函数和页面视觉差效果(scroll()、offsetTop、滚动监听的妙用)
想实现窗口滚动到一定位置时,部分网页的页面发生一些变化,但是手头没有合适的插件,所以就想到自己编写一个简易的方法, 想到这个方法要有很高的自由度和适应性,在这,就尽量的削减其功能,若有错误的地方或者更 ...
随机推荐
- Spring 中JCA CCI分析--转载
转载地址:http://blog.csdn.net/a154832918/article/details/6790612 J2EE提供JCA(Java Connector Architecture)规 ...
- IP101A芯片默认物理地址(PHY Adress)确定
转载:http://blog.csdn.net/ropai/article/details/6961157 根据IP101A的DataSheet,芯片的第9,10,12,13,15脚为PHYAD0~P ...
- 嵌入式Linux开发系列之一: 走进嵌入式Linux的世界
转载:http://www.ibm.com/developerworks/cn/linux/l-embed/part1/index.html 随着信息化技术的发展和数字化产品的普及,以计算机技术. ...
- TCP/IP协议原理与应用笔记17:IP编址(重点)
1. IP地址(通用标识符) 对于同一个网络设备(主机或路由器)的不同网络连接,需要不同的IP地址进行标识 2. 主机标识符 主要有下面三种方式的主机标识方式: (1)Name:是什么,可读性强(了解 ...
- Linux系统root用户忘记密码解决方法
一:在linux系统启动时(如下图),按e键 二:进入到设置页面,定位到如下行: 三:按e键,进入输入界面 四:在编辑行最后面,空格,输入single,回车后回到第二步界面,只是后面多了single ...
- mysql输出的错误提示是法语
参考MySQL用户手册 http://dev.mysql.com/doc/refman/5.5/en/error-message-language.html mysql.ini 文件里头有一个参数叫做 ...
- 通用函数get和set
这两个函数在画图时很常用,所有对象都有属性来定义它们的特征,正是通过设定这些属性来修正图形显示的方式.尽管许多属性所有的对象都有,但与每一种对象类型(比如坐标轴,线,曲面)相关的属性列表都是独一无二的 ...
- java按行读取txt并按行写入
IO流想必大家都很熟悉了,本次实现的需求是按行读取文件内容并且按行写入,代码如下: try { String encoding="utf-8"; //设定自己需要的字符编码集 Fi ...
- CSS/块级元素与内联元素的深入理解
今天终于对html中的块级元素和行内元素有了一个较为理性的认识.首先w3c对于block和inline的解释为:
- IT技术开发人员35岁之前应该做的十件事
第一,学会本行业所需要的一切知识并有所发展.已故零件大王布鲁丹在他35岁时,已经成为零件行业的领袖,并且组建了年收入达千万美元的海湾与西部工业公司.每个人在年轻时都可能有过彻夜不眠.刻苦攻读,这在20 ...