jQuery实现锚点向下平滑滚动特效示例


Posted in jQuery onAugust 29, 2017

实现效果:

jQuery实现锚点向下平滑滚动特效示例

实现原理:

使用jQuery animate()方法实现页面平滑滚动特效

$('html, body').animate({
scrollTop: $(hash).offset().top
}, 800, function(){
window.location.hash = hash;
});

简单实例代码:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
 // Add smooth scrolling to all links
 $("a").on('click', function(event) {
 
  // Make sure this.hash has a value before overriding default behavior
  if (this.hash !== "") {
   // Prevent default anchor click behavior
   event.preventDefault();
 
   // Store hash
   var hash = this.hash;
 
   // Using jQuery's animate() method to add smooth page scroll
   // The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
   $('html, body').animate({
    scrollTop: $(hash).offset().top
   }, 800, function(){
  
    // Add hash (#) to URL when done scrolling (default click behavior)
    window.location.hash = hash;
   });
  } // End if
 });
});
</script>
 <style>
body, html, .main {
  height: 100%;
}
 
section {
  min-height: 100%;
}
</style>
</head>
<body>
<a href="#section2" rel="external nofollow" style="
   font-size: 30px;
   font-weight: bold;
   text-align: center;
">点击此处平滑滚动到第二部分</a>
<div class="main">
 <section></section>
</div>
<div class="main" id="section2">
 <section style="
   background-color: #03c03c;
   color: #fff;
   font-size: 30px;
   text-align: center">
   SECTION 2
 </section>
</div>
</body>
</html>

以上这篇jQuery实现锚点向下平滑滚动特效示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

jQuery 相关文章推荐
jQuery实现jQuery-form.js实现异步上传文件
Apr 28 jQuery
jQuery+C#实现参数RSA加密传输功能【附jsencrypt.js下载】
Jun 26 jQuery
jquery动态添加以及遍历option并获取特定样式名称的option方法
Jan 29 jQuery
Angular5中调用第三方库及jQuery的添加的方法
Jun 07 jQuery
jQuery实现模糊搜索功能的方法分析
Jun 29 jQuery
jQuery实现表单动态添加数据并提交的方法
Jul 19 jQuery
jQuery 同时获取多个标签的指定内容并储存为数组
Nov 20 jQuery
在Vue项目中引入JQuery-ui插件的讲解
Jan 27 jQuery
jquery.tagsinput.js实现记录checkbox勾选的顺序
Sep 21 jQuery
jQuery使用ajax传递json对象到服务端及contentType的用法示例
Mar 12 jQuery
jQuery 淡入/淡出效果函数用法分析
May 19 jQuery
html中两种获取标签内的值的方法
Jun 16 jQuery
jQuery 1.9版本以上的浏览器判断方法代码分享
Aug 28 #jQuery
jQuery Collapse1.1.0折叠插件简单使用
Aug 28 #jQuery
解决IE7中使用jQuery动态操作name问题
Aug 28 #jQuery
jQuery菜单实例(全选,反选,取消)
Aug 28 #jQuery
基于jQuery实现图片推拉门动画效果的两种方法
Aug 26 #jQuery
详解webpack3如何正确引用并使用jQuery库
Aug 26 #jQuery
jQuery图片缩放插件smartZoom使用实例详解
Aug 25 #jQuery
You might like
深入解析php之apc
2013/05/15 PHP
php5.3提示Function ereg() is deprecated Error问题解决方法
2014/11/12 PHP
php版微信自动获取收货地址api用法示例
2016/09/22 PHP
thinkphp5.1框架模板赋值与变量输出示例
2020/05/25 PHP
jquery ajax请求方式与提示用户正在处理请稍等
2014/09/01 Javascript
理解javascript回调函数
2014/12/28 Javascript
28个常用JavaScript方法集锦
2015/01/14 Javascript
使用C++为node.js写扩展模块
2015/04/22 Javascript
基于jQuery Bar Indicator 插件实现进度条展示效果
2015/09/30 Javascript
深入剖析JavaScript中的函数currying柯里化
2016/04/29 Javascript
JS两种类型的表单提交方法实例分析
2016/11/28 Javascript
探究JavaScript中的五种事件处理程序方式
2016/12/07 Javascript
Vue.js:使用Vue-Router 2实现路由功能介绍
2017/02/22 Javascript
jQuery设置图片等比例缩小的方法
2017/04/29 jQuery
javaScript 逻辑运算符使用技巧整理
2017/05/03 Javascript
js微信应用场景之微信音乐相册案例分享
2017/08/11 Javascript
初识 Vue.js 中的 *.Vue文件
2017/11/22 Javascript
jquery 获取索引值在一定范围的列表方法
2018/01/25 jQuery
Vue中对比scoped css和css module的区别
2018/05/17 Javascript
Angular4.0动画操作实例详解
2019/05/10 Javascript
微信小程序实现搜索功能并跳转搜索结果页面
2019/05/18 Javascript
vue router动态路由设置参数可选问题
2019/08/21 Javascript
jQuery操作选中select下拉框的值代码实例
2020/02/07 jQuery
JetBrains PyCharm(Community版本)的下载、安装和初步使用图文教程详解
2020/03/19 Python
python def 定义函数,调用函数方式
2020/06/02 Python
Python应用实现处理excel数据过程解析
2020/06/19 Python
Python的3种运行方式:命令行窗口、Python解释器、IDLE的实现
2020/10/10 Python
请写出 BOOL flag 与"零值"比较的 if 语句
2016/02/29 面试题
兼职业务员岗位职责
2014/01/01 职场文书
前厅部经理岗位职责范文
2014/02/04 职场文书
推广普通话演讲稿
2014/05/23 职场文书
学校领导干部民主生活会整改方案
2014/09/29 职场文书
商务邀请函
2015/01/30 职场文书
电气工程师岗位职责
2015/02/12 职场文书
2015年小班保育员工作总结
2015/05/27 职场文书
教你怎么用python实现字符串转日期
2021/05/24 Python