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

本文就记录下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. yaml 1.6 操作

    /** * Copyright (c) 2008, http://www.snakeyaml.org * * Licensed under the Apache License, Version 2. ...

  2. VS 代码段 自定义

    <?xml version="1.0" encoding="utf-8"?> <CodeSnippets xmlns="http:/ ...

  3. Head First Html 与 Css 截图

    1.认识HTML web语言 2.深入了解超文本 HTML中的"HT" 3. 构建模块 web页面建设 4. 连接起来 5. 认识媒体,为页面添加图像 6. 严肃的HTML标准及其 ...

  4. web前端代码规范 - HTML代码规范

    Bootstrap HTML编码规范 本文转载自w3cschool. 由于bootstrap得到了世界的认可,因此,此规范用于规范html代码有一定的说服力. 交流qq群:164858883.欢迎各位 ...

  5. Android软键盘弹出时布局问题

    最近项目需要做一个类似聊天室的模块,基于Socket实现的,这部分稍后一段时间再做总结,功能上的相关点都实现了小例子也做出来了,最后发现一个比较腻歪的问题就是软键盘弹出时总是会把标题“挤出”屏幕,(无 ...

  6. STM32外部中断具体解释

      一.基本概念 ARM Coetex-M3内核共支持256个中断,当中16个内部中断,240个外部中断和可编程的256级中断优先级的设置.STM32眼下支持的中断共84个(16个内部+68个外部), ...

  7. HTML 5 视频使用

    视频格式 当前,video 元素支持三种视频格式: 格式 IE Firefox Opera Chrome Safari Ogg No 3.5+ 10.5+ 5.0+ No MPEG 4 9.0+ No ...

  8. python 安装 memcache

    方式一: python3 -m pip install python-memcached 方式二: pip3 install python-memcached 方式三: tar zxf python- ...

  9. hdu 油菜花王国

    Problem Description 程序设计竞赛即将到来,作为学校ACM集训队主力,小明训练一直很努力.今天天气不错,教练也心情大好,破例给各位队员放假一天,小明就骑着自己的小电驴到郊外踏青去了. ...

  10. Combination Sum,Combination Sum II,Combination Sum III

    39. Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique co ...