欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jquery如何实现锚点链接之间的平滑滚动

程序员文章站 2022-06-11 12:54:47
代码如下: $('a[href*=#]').click(function() { if (location.pathname.replace(/^\//,...

代码如下:


$('a[href*=#]').click(function() {
if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
var $target = $(this.hash);
$target = $target.length && $target || $('[name=' + this.hash.slice(1) + ']');
if ($target.length) {
var targetoffset = $target.offset().top;
$('html,body').animate({
scrolltop: targetoffset
},
500);
return false;
}
}
});