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实现的动态文字变化输出效果示例【附演示与demo源码下载】
Mar 24 jQuery
jQuery使用正则验证15/18身份证的方法示例
Apr 27 jQuery
jQuery导航条固定定位效果实例代码
May 26 jQuery
jQuery绑定事件方法及区别(bind,click,on,live,one)
Aug 14 jQuery
利用jquery如何从json中读取数据追加到html中
Dec 01 jQuery
Vue+jquery实现表格指定列的文字收缩的示例代码
Jan 09 jQuery
jQuery实现模糊搜索功能的方法分析
Jun 29 jQuery
一文快速了解JQuery中的AJAX
May 31 jQuery
jQuery表单校验插件validator使用方法详解
Feb 18 jQuery
JQuery获得内容和属性方法解析
May 30 jQuery
基于JQuery和DWR实现异步数据传递
Oct 16 jQuery
jQuery实现容器间的元素拖拽功能
Dec 01 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 函数执行效率的小比较
2010/10/17 PHP
php多文件上传功能实现原理及代码
2013/04/18 PHP
destoon设置自定义搜索的方法
2014/06/21 PHP
浅析PHP中的闭包和匿名函数
2017/12/25 PHP
很酷的javascript loading效果代码
2008/06/18 Javascript
JS 常用校验函数
2009/03/26 Javascript
javascript实现面向对象类的功能书写技巧
2010/03/07 Javascript
js href的用法
2010/05/13 Javascript
JSON为什么那样红为什么要用json(另有洞天)
2012/12/26 Javascript
jQuery不间断滚动效果(模拟百度新闻支持文字/图片/垂直滚动)
2013/02/05 Javascript
javascript中的startWith和endWith的几种实现方法
2013/05/07 Javascript
javascript:void(0)的作用示例介绍
2013/10/28 Javascript
JS获取下拉列表所选中的TEXT和Value的实现代码
2014/01/11 Javascript
Bootstrap每天必学之弹出框(Popover)插件
2016/04/25 Javascript
JavaScript奇技淫巧44招【实用】
2016/12/11 Javascript
React-Native中禁用Navigator手势返回的示例代码
2017/09/09 Javascript
深入了解JavaScript 私有化
2019/05/30 Javascript
微信小程序如何刷新当前界面的实现方法
2019/06/07 Javascript
Moment.js实现多个同时倒计时
2019/08/26 Javascript
addEventListener()和removeEventListener()追加事件和删除追加事件
2020/12/04 Javascript
python的描述符(descriptor)、装饰器(property)造成的一个无限递归问题分享
2014/07/09 Python
python提取页面内url列表的方法
2015/05/25 Python
Python cookbook(数据结构与算法)同时对数据做转换和换算处理操作示例
2018/03/23 Python
解决pandas中读取中文名称的csv文件报错的问题
2018/07/04 Python
基于pygame实现童年掌机打砖块游戏
2020/02/25 Python
python selenium自动化测试框架搭建的方法步骤
2020/06/14 Python
将pymysql获取到的数据类型是tuple转化为pandas方式
2020/05/15 Python
Python使用shutil模块实现文件拷贝
2020/07/31 Python
python连接mongodb数据库操作数据示例
2020/11/30 Python
工艺员岗位职责
2014/02/11 职场文书
大学活动总结格式
2014/04/29 职场文书
政府采购方案
2014/06/12 职场文书
职工年度考核评语
2014/12/31 职场文书
新闻通讯稿范文
2015/07/22 职场文书
学习师德师风的心得体会(2篇)
2019/10/08 职场文书
k-means & DBSCAN 总结
2021/04/27 Python