需要在内容向下滚动时,从水平滚动菜单列表中显示活动菜单。

huangapple go评论77阅读模式
英文:

Need to show active menu from a horizontal scrolling menu list while a scrolling the content to bottom

问题

我有一个位于页面顶部的水平菜单列表,底部有内容部分。页眉菜单有链接到底部内容的特定部分。点击每个菜单时,内容需要平滑滚动到相应的部分。我从网上找到了一些我想要的样式,但没有水平滚动到菜单部分。

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

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

这是选择菜单2时的示例

这是当我们将内容区域滚动到"section 5"时会发生的

请查看两者的活动菜单。

英文:

I have a horizontal list of the menu on the top of the page and the contents at the bottom. The header menu has a link to the particular sections at the bottom content. While clicking on each menu, content needs to scroll to the section with a smooth effect. I have found some styles which I want from online, but it has no horizontal scrolling to the menu section.

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

this is an example of my requirement, but it needs to have more menu on the top part. Also while we scrolling the contents to the bottom, the respective menu needs to scroll the visible area.

Sorry I have edited my question because I wasn't clear on my description.

this is an example while selecting menu2

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

please see the active menu for both.

答案1

得分: 1

只需在以下 "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) {
        // 目标元素 id
        var id = jQuery(this).attr('href');

        // 目标元素
        var $id = jQuery(id);
        if ($id.length === 0) {
            return;
        }

        // 防止标准哈希导航(避免在 IE 中闪烁)
        e.preventDefault();

        // 相对于文档的顶部位置
        var pos = $id.offset().top;

        // 带动画的顶部滚动
        jQuery('body, html').animate({scrollTop: pos});
    });
</script>
英文:

Just add below css

html {
    scroll-behavior: smooth;
}

Or you can also do with jQuery

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

	    // 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(&#39;body, html&#39;).animate({scrollTop: pos});
	});
&lt;/script&gt;

huangapple
  • 本文由 发表于 2020年1月3日 15:17:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574601.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定