JavaScript中通用的jquery动画滚屏实例
作者:北京王老师??????? 发布时间:2024-04-22 22:22:34
标签:JavaScript,jquery,动画,滚屏
实现效果
在网站页面上,点击某个超链接,页面跳转到某个位置,跳转过程有一个动画滚动效果,这是一种比较酷的体验。这种效果是如何实现的呢,本文通过实际代码来详解实现方法。
实现代码
网页代码
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>滚屏效果</title>
<script src="./assets/js/jquery.min.js"></script>
</head>
<body style=" margin-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; ">
<div id="header" style="height: 100px;">
<a id="tech" class="scroll-a" href="#hi-tech" rel="external nofollow" rel="external nofollow" >技术</a>
<a id="code" class="scroll-a" href="#hi-code" rel="external nofollow" rel="external nofollow" >代码</a>
</div>
<div style="background-color: gray;height: 600px;">
<h1>间隔</h1>
</div>
<div style="background-color: white;height: 600px;" id="hi-tech">
<h1>技术</h1>
<a id="tohead1" class="scroll-a" href="#header" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >返回顶部</a>
</div>
<div style="background-color: gray;height: 600px;" id="hi-code">
<h1>代码</h1>
<a id="tohead" class="scroll-a" href="#header" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >返回顶部</a>
</div>
</body>
<script>
$('#tech').on("click", function () {
$('html,body').animate({scrollTop:$('#hi-tech').offset().top}, 800);
return false;
});
$('#code').on("click", function () {
$('html,body').animate({scrollTop:$('#hi-code').offset().top}, 800);
return false;
});
$('#tohead').on("click", function () {
$('html,body').animate({scrollTop:$('#header').offset().top}, 800);
return false;
});
$('#tohead1').on("click", function () {
$('html,body').animate({scrollTop:$('#header').offset().top}, 800);
return false;
});
</script>
</html>
这里主要用到了jquery的animate方法,实现思路是,当点击某个超链接时,通过jquery的animate将屏幕滚动到某个位置。注意animate函数的两个参数,一个是滚动位置,一个是动画滚动的时间毫秒。
$('#tech').on("click", function () {
$('html,body').animate({scrollTop:$('#hi-tech').offset().top}, 800);
return false;
});
虽然实现了效果,这里有个问题,如果滚动的超链接较多,那么就要写不少重复代码,还要注意滚动位置不要写错。下面通过改变一下jquery选择器来优化代码。
优化代码
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>滚屏效果</title>
<script src="./assets/js/jquery.min.js"></script>
</head>
<body style=" margin-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; ">
<div id="header" style="height: 100px;">
<a id="tech" class="scroll-a" href="#hi-tech" rel="external nofollow" rel="external nofollow" >技术</a>
<a id="code" class="scroll-a" href="#hi-code" rel="external nofollow" rel="external nofollow" >代码</a>
</div>
<div style="background-color: gray;height: 600px;">
<h1>间隔</h1>
</div>
<div style="background-color: white;height: 600px;" id="hi-tech">
<h1>技术</h1>
<a id="tohead1" class="scroll-a" href="#header" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >返回顶部</a>
</div>
<div style="background-color: gray;height: 600px;" id="hi-code">
<h1>代码</h1>
<a id="tohead" class="scroll-a" href="#header" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >返回顶部</a>
</div>
</body>
<script>
$('.scroll-a').on("click", function () {
let scrollto = $(this).attr('href');
$('html,body').animate({scrollTop:$(scrollto).offset().top}, 800);
return false;
});
</script>
</html>
可以看出,通过使用jquery class选择器,并使用jquery的this对象获取滚动目标,明显减少了代码,代码看起来更加清楚。
来源:https://blog.51cto.com/livestreaming/5512072


猜你喜欢
- 本文实例为大家分享了python判断设备是否联网的具体代码,供大家参考,具体内容如下直接上代码,就是用判断socket能不连上的方法来判断。
- 废话不多说了,上代码吧:import threadingimport requestsimport timeimport osclass M
- 在我的前一篇教程《九宫格基本布局》中,我介绍了用相对定位加绝对定位的方法来制作九宫格的基本布局。这是一种比较符合人们惯性思维的方法,好像制作
- 合理使用装饰器可以简化开发,并且使得代码更加清晰。下面我们分别介绍两种装饰器,不带参数的装饰器和带参数的装饰器。一、不带参数的装饰器我们用一
- 本文实例讲述了Python实用库 PrettyTable。分享给大家供大家参考,具体如下:PrettyTable安装使用pip即可十分方便的
- 说明一直想做一个基于VUE的项目,但是因为项目往往要涉及到后端的知识(不会后端真的苦),所以就没有一直真正的动手去做一个项目。直到发现Git
- 本文实例为大家分享了Python求多幅图像栅格值的平均值,供大家参考,具体内容如下本程序所采用的方法并不是最优方法,ARCGIS已经提供了相
- 数据可视化动画还在用 Excel 做?今天分享一个简单的 Python 包就能分分钟搞定!而且生成的动画也足够丝滑,效果是酱紫的:这是一位专
- 前言Python是面向对象的程序设计(Object Oriented Programming)。面向对象的程序设计的一条基本原则是:计算机程
- 作者:Scott Gerber原标题:Mobile App Development: 10 Tips for Small Business
- 一、介绍vue.js 是 目前 最火的前端框架,vue.js 兼具 angular.js 和 react.js 的优点,并剔除它们的缺点,并
- 首先要挂代理,但是还不够,pycharm默认不使用代理,需要进行设置找到代理软件的HTTP代理地址,将地址和端口填入设置中即可来源:http
- 用python实现文件夹下的成批文件格式转换我们对于文件转换的需求很大,甚至于对于图片的格式,JPG和PNG格式在肉眼看来都没什么差别,但是
- vue-cli npm解决vue项目中缺失core-js报错This dependency was not found:core-js/mo
- 核心代码function convert2utf8($string) { return iconv(&
- jwt详解Django之auth模块(用户认证)jwt的作用json web token,一般用于用户认证就是做用户登录的(前后端分离/微信
- 1、词表映射无论是深度学习还是传统的统计机器学习方法处理自然语言,都需要先将输入的语言符号(通常为标记Token),映射为大于等于0、小于词
- 很多DBA目前还停留在Oracle 9i或者10g,究其原因有可能是Oracle 11g的价格问题。本文将为大家讲解在Windows 7下安
- 这篇文章主要介绍了通过Kettle自定义jar包供javascript使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参
- 本文实例为大家分享了Python代码实现双链表的具体代码,供大家参考,具体内容如下双链表的每个节点有两个指针: 一个指向后一个节点,另一个指