咳咳,在网页出现滚动条的时候,许多网站会在右下角出现一个图标,点击可以回到页面顶部

本文就记录下js实现代码:

1.在html页面body添加dom元素

<img src="toTop.png" alt="" class="gotop" >

<div class="gotopdiv" style="display: none;"><span>返回顶部</span></div>

2.页面向下滚动时,出现该图标

//滚动时出现返回顶部
$(window).scroll(function () {
var top_scroll = $(document).scrollTop();
if (top_scroll > 0) {
$('img.gotop').css({ 'display': 'block', 'cursor': 'pointer' });
$('.gotopdiv').css({ 'display': 'none' }); } else {
$('img.gotop,.gotopdiv').css({ 'display': 'none' })
} })

3.点击该图标,回到页面顶端

   //点击返回顶部
$(document).on('click','.gotopdiv,.gotop',function () {
$(document).scrollTop(0);
$('.gotop').hide()
$('.gotopdiv').hide();
})

4.加入该图标鼠标放置离开css效果

   $(document).on('mouseenter','.gotop',function () {
var top_scroll = $(document).scrollTop();
if (top_scroll > 0) {
$(this).next().show();
$(this).hide();
}
})
$(document).on('mouseleave','.gotopdiv',function () {
var top_scroll = $(document).scrollTop();
if (top_scroll > 0) {
$(this).prev().show()
$(this).hide();
}
})

5 加入css。页面最终全部代码如下:

<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<script src="jquery-1.10.2.min.js"></script>
<style>
.gotop,.gotopdiv{
position: fixed;
bottom: 20px;
width: 52px;
display: none;
height: 52px;
right: 100px;
z-index: 999;
} .gotopdiv{
background: #60BF8B;
font-weight: bold;
text-align: center;
cursor: pointer;
width: 52px;
height: 55px;
color: #fff;
font-size: 15px;
}
.gotopdiv span{display: inline-block;
padding: 10px;
position: relative;
}
</style> </head>
<body> <div style="position:absolute;width:600px;height:3000px;"></div>
<img src="toTop.png" alt="" class="gotop" > <div class="gotopdiv" style="display: none;"><span>返回顶部</span></div> </body> <script>
$(document).on('mouseenter','.gotop',function () {
var top_scroll = $(document).scrollTop();
if (top_scroll > 0) {
$(this).next().show();
$(this).hide();
}
})
$(document).on('mouseleave','.gotopdiv',function () {
var top_scroll = $(document).scrollTop();
if (top_scroll > 0) {
$(this).prev().show()
$(this).hide();
}
}) //点击返回顶部
$(document).on('click','.gotopdiv,.gotop',function () {
$(document).scrollTop(0);
$('.gotop').hide()
$('.gotopdiv').hide();
})
//滚动时出现返回顶部
$(window).scroll(function () {
var top_scroll = $(document).scrollTop();
if (top_scroll > 0) {
$('img.gotop').css({ 'display': 'block', 'cursor': 'pointer' });
$('.gotopdiv').css({ 'display': 'none' }); } else {
$('img.gotop,.gotopdiv').css({ 'display': 'none' })
} })
</script> </html>

Demo效果和源码下载可以点击demo

js网页返回页面顶部的小方法的更多相关文章

  1. HTML5商城开发五 实现返回页面顶部

    本文内容主要是网上参考收集,介绍四种简单的返回页面顶部代码,可以使用简单的HTML锚标记,也可使用Javascript Scroll函数动态返回等等. 一.使用锚标记返回页面顶部 使用HTML锚标记最 ...

  2. 基于JS实现回到页面顶部的五种写法(从实现到增强)

    这篇文章主要介绍了基于JS实现回到页面顶部的五种写法(从实现到增强)的相关资料,本文介绍的非常详细,实用性也非常高,非常具有参考借鉴价值,需要的朋友可以参考下   写法 [1]锚点 使用锚点链接是一种 ...

  3. react-router(v4) 路由跳转后返回页面顶部问题

    遇到的问题 由A页面跳转到B页面,B页面停留在A页面的位置,没有返回到顶部. 问题分析 首先分析下出现此问题的原因: 在项目中使用的是 hashHistory,它是建立在 history 之上的,当路 ...

  4. 代码: 返回页面顶部 jquery

    jquery代码: 返回页面顶部 <script type="text/javascript" src="http://cdn.bootcss.com/jquery ...

  5. atitit. js 跨界面 页面 web cs 传值方法总结

    atitit. js 跨界面 页面 web cs 传值方法总结 #--需求 js #---两个方法:   直接传跟跟间接传递... 1.直接传跟new form(param)    web使用url方 ...

  6. 解决点击a标签返回页面顶部的问题

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

  7. jquery实现返回页面顶部功能。

    <p id="back-to-top"> <span></span> </p> <script type="text ...

  8. (转)解决点击a标签返回页面顶部的问题

    本文转载至http://www.cnblogs.com/chenluomenggongzi/p/5950670.html 1 <!DOCTYPE html> 2 <html lang ...

  9. 原生态js,返回至顶部

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

随机推荐

  1. Ubuntu 无线连接能上网,但是有线连接不能上

    这两天装Ubuntu,遇到小问题.最头疼的还是上网,过去我装了Ubuntu时,都是插上网线就能直接上网,这次就不行了. 我刚点开一个网页,接下来点就不能上了,但是无线连接就可以正常上网. 我在一个论坛 ...

  2. (转)iOS Wow体验 - 第二章 - iOS用户体验解析(2)

    本文是<iOS Wow Factor:Apps and UX Design Techniques for iPhone and iPad>第二章译文精选的第二部分,其余章节将陆续放出.上一 ...

  3. [Cycle.js] Generalizing run() function for more types of sources

    Our application was able to produce write effects, through sinks, and was able to receive read effec ...

  4. jquery之提示信息

    //生成优惠券并分发 function saveCouponAssign(){ //发行券种 var couponTypeId = $("#couponTypeId").combo ...

  5. java基础之成员变量与局部变量

    成员变量的含义 局部变量的含义 成员变量与局部变量的区别

  6. asp.net获取ip地址的方法

    在ASP中使用 Request.ServerVariables("REMOTE_ADDR") 来取得客户端的IP地址,但如果客户端是使用代理服务器来访问,那取到的就是代理服务器的I ...

  7. oracle 的服务器进程(PMON, SMON,CKPT,DBWn,LGWR,ARCn)

    来着TOM的<oracle 编程艺术 9i,10g,11g> PMON PMON,进程监视.PMON主要有3个用途: 1,在进程非正常中断后,做清理工作.例如:dedicated serv ...

  8. c#类似单片机的8bit或运算

    1.正确 PWMSUBM0 &= (byte)(PWMSUBM0 | 0xfc); PWMSUBM0 &= (byte)(PWMSUBM0 | (byte)0xfc); 2.不能编译的 ...

  9. 函数:lambda表达式 - 零基础入门学习Python021

    函数:lambda表达式 让编程改变世界 Change the world by program lambda表达式 Python允许使用lambda关键字来创建匿名函数.我们提到一个新的关键字:匿名 ...

  10. Jasper_sheetName_defined by parameter or hard coding or filed name

    1.根据传递的参数定义sheet name (jasper sheet name defined by parameter) (1) 获取后台参数 <parameter name="P ...