平滑滚动仅适用于按钮

时间:2017-03-31 09:16:30

标签: javascript jquery html

我有这个代码,允许页面在点击锚链接时顺畅滚动:

$(function() {
   $('input.anchor-button').click(function(e) {
      e.preventDefault();
      var hash = $(this).parent('form').attr('action');
      var target = $(hash);
      target = target.length ? target : $('[name=' + hash.slice(1) +']');
      if (target.length) {
         $('html, body').animate({
            scrollTop: target.offset().top
         }, 1000);
      }
   });
});
<form id = "form_createaccount_button_orange" action="#createfaculty">
   <input type="submit" class="anchor-button" value="  create faculty account  " />
</form>

然而,它仅适用于输入按钮。我试过<a href=#createfaculty...它让我跳到锚点,但光滑的卷轴不起作用。无论如何转换也许是为了修改它也适用于a href链接的javascript?

0 个答案:

没有答案