美文网首页
html 使用a标签做定位锚点,如何添加滚动动画

html 使用a标签做定位锚点,如何添加滚动动画

作者: 两年半练习程序员 | 来源:发表于2019-08-21 16:35 被阅读0次

1.给a标签添加一个统一的类名anchor(可自定义)

//html
<a class="anchor" href="#gsxx" rel="nofollow">工商信息</a>
<div id="gsxx">工商信息</div>

//jquery
$(".anchor").bind("click touch",function(){
    //根据a标签的href转换为id选择器,获取id元素所处的位置
    $('html,body').animate({scrollTop: ($($(this).attr('href')).offset().top)},300);
});

相关文章