当主题平滑滚动已存在时,如何获得.click来平滑锚的滚动滚动

问题描述 投票:0回答:1

我有3个列ID的jQuery,使它们可单击,可导航到页面下方的锚ID。

如果添加CSS平滑滚动

`Html{scroll-behavior:smooth}`

它破坏了主题的流畅滚动,这是网站其他部分所需的,并且我尝试了很多其他示例,例如: <script> $(document).ready(function() { $('a').click(function(){ $('html, body').animate({ scrollTop: $( $.attr(this, 'href') ).offset().top }, 5000); return false; }); }); </script>在JS中,但我无法使它们工作。这就是我用来使列可点击的方法:

    jQuery('#heures, #services, #con').css('cursor','pointer');
jQuery('#heures').click(function() {window.location="#nos-heures";});
jQuery('#services').click(function() {window.location="#nos-services";});
jQuery('#con').click(function() {window.location="#contact";});

可单击的部分按预期工作,但我无法包括平滑滚动的内容

jquery wordpress anchor smooth-scrolling
1个回答
0
投票

这可以通过Vanilla JS使用Elelment.scrollIntoView完成。请记住,IE中不提供平滑滚动功能,但是它将使它们跳到那里。

https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView

© www.soinside.com 2019 - 2024. All rights reserved.