javascript - 需要在将内容滚动到底部时从水平滚动菜单列表显示事件菜单

标签 javascript jquery html css

我在页面顶部有一个水平菜单列表,在底部有一个内容列表。标题菜单有一个指向底部内容特定部分的链接。单击每个菜单时,内容需要滚动到具有平滑效果的部分。我从网上找到了一些我想要的样式,但它没有水平滚动到菜单部分。

https://www.cssscript.com/demo/simple-scrollspy-plugin-javascript/

这是我的要求的一个例子,但它需要在顶部有更多的菜单。此外,当我们将内容滚动到底部时,相应的菜单需要滚动可见区域。

抱歉,我编辑了我的问题,因为我的描述不清楚。

this is an example while selecting menu2

this is to happen while we scroll the content area to 'section 5'

请查看两者的事件菜单。

最佳答案

只需添加下面的css

html {
    scroll-behavior: smooth;
}

或者你也可以使用jQuery

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.js"></script>
<script type="text/javascript">
    jQuery(document).on('click', 'a[href^="#"]', function(e) {
        // target element id
        var id = jQuery(this).attr('href');

        // target element
        var $id = jQuery(id);
        if ($id.length === 0) {
            return;
        }

        // prevent standard hash navigation (avoid blinking in IE)
        e.preventDefault();

        // top position relative to the document
        var pos = $id.offset().top;

        // animated top scrolling
        jQuery('body, html').animate({scrollTop: pos});
    });
</script>

关于javascript - 需要在将内容滚动到底部时从水平滚动菜单列表显示事件菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59574601/

相关文章:

javascript - 我在文本字段中需要的复选框名称

html - 未封闭的 HTML 标签的正则表达式

javascript - Angular-UI-Bootstrap 和 UI-bootstrap

javascript - 当我们在textarea中粘贴代码时,是否有像ckeditor这样的脚本可以删除样式和脚本?

使用空格字符进行 JavaScript 密码验证

javascript - 在 jQuery 中取消延迟 promise

javascript 输入字段或数组表现异常

javascript - 通过 jquery.ajax 重新加载后从 div 中恢复输入/选择元素

javascript - 关于嵌套 setInterval

javascript - 用正则表达式拆分字符串,在行首保留定界符