使用JS给html锚点跳转增加动画效果

            $(".label").bind("click touch",function(){
                //根据a标签的href转换为id选择器,获取id元素所处的位置,并高度减50px(这里根据需要自由设置)
                $('html,body').animate({scrollTop: ($($(this).attr('href')).offset().top -50 )},500);
            });

注释:

- html中仍然还是照常使用锚点<a href='#xxx'>  → <div id='xxx'>

- 给所有锚点(a标签)加上"class='label'") → 这个class名字可以随便定

- JS代码需要放在html后面才生效

猜你喜欢

转载自blog.csdn.net/weixin_41395648/article/details/82424137