js网页返回页面顶部的小方法
咳咳,在网页出现滚动条的时候,许多网站会在右下角出现一个图标,点击可以回到页面顶部
本文就记录下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网页返回页面顶部的小方法的更多相关文章
- HTML5商城开发五 实现返回页面顶部
本文内容主要是网上参考收集,介绍四种简单的返回页面顶部代码,可以使用简单的HTML锚标记,也可使用Javascript Scroll函数动态返回等等. 一.使用锚标记返回页面顶部 使用HTML锚标记最 ...
- 基于JS实现回到页面顶部的五种写法(从实现到增强)
这篇文章主要介绍了基于JS实现回到页面顶部的五种写法(从实现到增强)的相关资料,本文介绍的非常详细,实用性也非常高,非常具有参考借鉴价值,需要的朋友可以参考下 写法 [1]锚点 使用锚点链接是一种 ...
- react-router(v4) 路由跳转后返回页面顶部问题
遇到的问题 由A页面跳转到B页面,B页面停留在A页面的位置,没有返回到顶部. 问题分析 首先分析下出现此问题的原因: 在项目中使用的是 hashHistory,它是建立在 history 之上的,当路 ...
- 代码: 返回页面顶部 jquery
jquery代码: 返回页面顶部 <script type="text/javascript" src="http://cdn.bootcss.com/jquery ...
- atitit. js 跨界面 页面 web cs 传值方法总结
atitit. js 跨界面 页面 web cs 传值方法总结 #--需求 js #---两个方法: 直接传跟跟间接传递... 1.直接传跟new form(param) web使用url方 ...
- 解决点击a标签返回页面顶部的问题
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- jquery实现返回页面顶部功能。
<p id="back-to-top"> <span></span> </p> <script type="text ...
- (转)解决点击a标签返回页面顶部的问题
本文转载至http://www.cnblogs.com/chenluomenggongzi/p/5950670.html 1 <!DOCTYPE html> 2 <html lang ...
- 原生态js,返回至顶部
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
随机推荐
- Squid--hash代码分析
#ifndef SQUID_HASH_H #define SQUID_HASH_H //几个函数和变量的别名 typedef void HASHFREE(void *); typedef int HA ...
- hdu 3836 Equivalent Sets(tarjan+缩点)
Problem Description To prove two sets A and B are equivalent, we can first prove A is a subset of B, ...
- testng跑失败用例重试
testng 提高用例通过率,失败用例要重新运行一次 步骤: 1.新建一个Retry 类,implements IRetryAnalyzer接口,这个类里面确定重跑次数,以及分析每次失败是否需要重新运 ...
- c++ 之 字符和字符串
字符 1.字符的分类 字符主要包括字母.数字.标点符号.控制字符等 在ASCII编码表中,每一个字符都用一个十进制数来表示 注:ASCII的全称是American Standard Code for ...
- C#工厂模式代码实例
此处示例为一个简易计算器工厂模式的实现. 创建类库,名为CalcLib,我把计算功能全部放在这个类库中. 首先,创建一个抽象的计算器算法父类,如下: /// <summary> /// 计 ...
- C# 创建验证码图片
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; us ...
- .net 4.0 面向对象编程漫谈基础篇读书笔记
话说笔者接触.net 已有些年头,做过的项目也有不少,有几百万的,也有几十万的,有C/S的,也有B/S的.感觉几年下来,用过的框架不少,但是.net的精髓一直没有掌握.就像学武之人懂得各种招式,但内功 ...
- Understanding JavaScript Function Invocation and "this"
Understanding JavaScript Function Invocation and "this" 11 Aug 2011 Over the years, I've s ...
- hdu4296 贪心
E - 贪心 Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:32768KB 64bit I ...
- Linux Shell(初识)
什么是Shell:Shell是一个命令解释器. Linux下支持shell的种类: 1. Bourne Shell(简称sh) 2.C Shell(简称csh) 3.Korn Shell(简称ksh ...