在CSS和HTML中平滑滚动

发布于 2025-02-07 18:17:15 字数 773 浏览 1 评论 0原文

您好,我正在使用多个页面上的网站上工作,我想在某个页面上进行平滑滚动,但我不想使用HTML标签,因为它仅适用于此特定页面,而不是整个网站,这是我的代码。

{% if section.settings.display_product_detail_description == false and section.settings.display_product_reviews == false and section.settings.display_shipping_returns == false %}
{% assign tab5_active = true %}
{% endif %}

<div class="scroll-to-table">      
  <li class = "tab-title">
    <a href="#product_attributes_table" class="tab-links {% if tab5_active %} active {% endif %}">
    Specs
    </a>
  </li>
  </div>

这是HTML的代码

div.scroll-to-table{
scroll-behavior: smooth;}

CSS代码

,以下是页面所做的所有 是跳跃而不是光滑的滚动。我已经尝试使用ID代替类,.scroll到croll-to.scroll to.scroll到table,并更改我从中称为CSS的元素,但没有运气

Hello I am working on my website with multiple pages and I want to do smooth scrolling on a certain page but I don't want to use the html tag because it will only be for this specific page and not the whole website here is my code.

{% if section.settings.display_product_detail_description == false and section.settings.display_product_reviews == false and section.settings.display_shipping_returns == false %}
{% assign tab5_active = true %}
{% endif %}

<div class="scroll-to-table">      
  <li class = "tab-title">
    <a href="#product_attributes_table" class="tab-links {% if tab5_active %} active {% endif %}">
    Specs
    </a>
  </li>
  </div>

This is the code for the HTML

div.scroll-to-table{
scroll-behavior: smooth;}

And here is the code for the CSS

At the moment all that the page is doing is a jump and not a smooth scroll. I've tried using ID instead of Class, .scroll-to-table instead of div.scroll-to-table, and changing the element in which I call the CSS from but no luck

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看轻我的陪伴 2025-02-14 18:17:15

我们可以使用JavaScript IIFE(立即调用函数表达式)将scroll-behaviour:平滑添加到特定页面。这将把平滑的行为添加到运行此脚本的唯一页面。

<script>
  (function () {
    document.documentElement.style.scrollBehavior = smooth;
  })();
</script>

We can add scroll-behaviour: smooth to particular page by using javascript IIFE (Immediately Invoked Function Expression). This will add the smooth behaviour to the only page that runs this script.

<script>
  (function () {
    document.documentElement.style.scrollBehavior = smooth;
  })();
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文