点击链接平滑到指定位置 滚动插件jquery.smoothScroll

JQuery 1215 0 2019-04-11

点击链接平滑到指定位置 滚动插件jquery.smoothScroll

需要引入文件:jquery.js和smoothScroll.js这两个文件,实例讲解如下:

HTML代码:

<a href="#top" class="gd">顶部</a>
<a href="#service" class="gd">服务</a>
<a href="#footer" class="gd">底部</a>

<section id="top"><div>内容......</div></section>
<section id="service"><div>内容......</div></section>
<section id="footer"><div>内容......</div></section>

JS代码:

$(function(){
	$('.gd').click(function(){
		var toid=$(this).attr('href');
		if(toid=='#top'){
			//直接滚到顶部
			window.scroll({ top: 0, left: 0, behavior: 'smooth' });
		}else{
			//指定到相应位置
			document.querySelector(toid).scrollIntoView({ behavior: 'smooth' });
		}
	});	
})


更多参考:

http://iamdustan.com/smoothscroll/

https://www.cnblogs.com/cherishli/p/6941251.html?utm_source=itdadao&utm_medium=referral

http://amazeui.org/javascript/smooth-scroll

上一篇:jquery ajax 接收返回数据类型(dateType)并处理

下一篇:jquery实现复选框全选或取消

讨论数量:0

请先登录再发表讨论。 2024-04-16

天涯网魂
3 杠 5 星
TA 的文章
TA 的随言
TA 的资源链