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的更多相关文章

  1. 第二十三课:jQuery.event.add的原理以及源码解读

    本课主要来讲解一下jQuery是如何实现它的事件系统的. 我们先来看一个问题: 如果有一个表格有100个tr元素,每个都要绑定mouseover/mouseout事件,改成事件代理的方式,可以节省99 ...

  2. 错误源:WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for 'jquery'. Please add a ScriptResourceMapping named jquery(case-sensitive).

    Server Error in '/' Application. WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping ...

  3. WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for 'jquery'. Please add a ScriptResourceMapping named jquery(case-sensitive).

    新开一个Web site.没有使用jQuery,当Insus.NET使用一些验证控件时,如RequiredfieldValidator,程序出现下面错误: WebForms UnobtrusiveVa ...

  4. jquery的add()用法总结

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="utf-8&quo ...

  5. jquery的add()方法扩大选择返回

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. [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 ...

  7. jquery remove/add css

    <input type="submit" class="btn btn-primary" id="submit" value=&quo ...

  8. jQuery EasyUI - Add link to datagrid cell

    Extracted from: http://stackoverflow.com/questions/16061894/jquery-easyui-add-link-to-cell HTML: < ...

  9. 原生JS和JQuery代码编写窗口捕捉函数和页面视觉差效果(scroll()、offsetTop、滚动监听的妙用)

    想实现窗口滚动到一定位置时,部分网页的页面发生一些变化,但是手头没有合适的插件,所以就想到自己编写一个简易的方法, 想到这个方法要有很高的自由度和适应性,在这,就尽量的削减其功能,若有错误的地方或者更 ...

随机推荐

  1. 1002 GTY's birthday gift

    GTY's birthday gift                                                                       Time Limit ...

  2. Rating

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission ...

  3. [改善Java代码]动态加载不适合数组

    上一个建议解释了为什么要使用forName,本建议就说说哪些地方不适合使用动态加载. 如果forName要加载一个类,那它必须是一个类------8中基本类型就排除在外.它们不是一个具体的类. 其次它 ...

  4. Linux 内核配置和编译

    Linux 内核配置和编译 一.配置内核 (1). 为什么要配置内核 1. 硬件需求 2. 软件需求 选出需要的,去掉不要的 (2). 如何配置内核 1. make  config 基于文本模式的交互 ...

  5. NVelocity 实现简单的 CIUD

    1, NVelocity 是 一般处理程序.ashx 和 前台页面模板的桥梁.  2,我们现在建立一个简单的查询:   A,新建项目,把NVelocity.dll拉入项目中,并添加对其引用 B,新建C ...

  6. 【Knockout】一、认识Knockout,你会爱上它

    介绍 Knockout简称ko,是一个轻量级的javascript类库,采用MVVM设计模式(即Model.view.viewModel),简单优雅的实现了双向绑定,实时更新,帮助您使用干净的数据模型 ...

  7. Cocos移植到Android-通过命令行交叉编译打包

    当我们在Windows下使用Visual Studio开发好Win32平台的游戏程序后,我们需要把它们移植到不同的平台上.Cocos2d-x支持很多个平台,然而,我们不可能介绍全部平台的移植.我们总结 ...

  8. Cocos2d-x开发实例:单点触摸事件

    下面我们通过一个实例详细了解一下,层中单点触摸事件的实现过程.感受一下它的缺点和优点.该实例场景如下图所示,场景中有两个方块精灵,我们可以点击和移动它们.   下面我们看看HelloWorldScen ...

  9. wpf DataGrid 双击获取当前行的控件

    <DataGrid Margin="10" HorizontalAlignment="Left" VerticalAlignment="Top& ...

  10. 项目中的那些事---JavaScript

    一.String.charAt(index) 作用:获取字符串指定索引位置的字符 注意:index的值是0~(字符串长度-1)之间的值 <script type="text/javas ...