如何在滚动网站内容或达到特定div时更新菜单。

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

How to update menu when scrolling website content or reach a particular div

问题

以下是翻译好的部分:

这里有一个示例:
https://www.msdmanuals.com/professional/pulmonary-disorders/diagnostic-and-therapeutic-pulmonary-procedures/bronchoscopy?query=bronchoscopy

基本上,它显示了一个菜单,其中包含指向页面各部分的链接,当用户滚动页面时,这些链接会突出显示。如何突出显示菜单?如何在HTML、CSS或JavaScript中实现这一效果?

HTML的示例代码:

<section id="main">
    <div class="target" id="1">目标 1</div>
    <div class="target" id="2">目标 2</div>
    <div class="target" id="3">目标 3</div>
    <div class="target" id="4">目标 4</div>
</section>
<aside id="nav">
    <nav>
        <a href="#1" class="active">点 1</a>
        <a href="#2">点 2</a>
        <a href="#3">点 3</a>
        <a href="#4">点 4</a>
    </nav>
</aside>

请注意,上述代码示例包括HTML部分的翻译。

英文:

Here is an example:
https://www.msdmanuals.com/professional/pulmonary-disorders/diagnostic-and-therapeutic-pulmonary-procedures/bronchoscopy?query=bronchoscopy

Basically it displays a menu with links to parts of the page that get highlighted as the user scrolls past them. How to highlight the menu? How to achieve this in HTML, CSS or JavaScript?

Sample code of HTML:

&lt;section id=&quot;main&quot;&gt;
    &lt;div class=&quot;target&quot; id=&quot;1&quot;&gt;TARGET 1&lt;/div&gt;
    &lt;div class=&quot;target&quot; id=&quot;2&quot;&gt;TARGET 2&lt;/div&gt;
    &lt;div class=&quot;target&quot; id=&quot;3&quot;&gt;TARGET 3&lt;/div&gt;
    &lt;div class=&quot;target&quot; id=&quot;4&quot;&gt;TARGET 4&lt;/div&gt;
&lt;/section&gt;
&lt;aside id=&quot;nav&quot;&gt;
    &lt;nav&gt;
        &lt;a href=&quot;#1&quot; class=&quot;active&quot;&gt;Punkt 1&lt;/a&gt;
        &lt;a href=&quot;#2&quot;&gt;Punkt 2&lt;/a&gt;
        &lt;a href=&quot;#3&quot;&gt;Punkt 3&lt;/a&gt;
        &lt;a href=&quot;#4&quot;&gt;Punkt 4&lt;/a&gt;
    &lt;/nav&gt;
&lt;/aside&gt;

答案1

得分: 1

这是你要找的被称为scrollspy的内容。尝试像这样做:

<section id="main">
    <div class="target" id="1">目标 1</div>
    <div class="target" id="2">目标 2</div>
    <div class="target" id="3">目标 3</div>
    <div class="target" id="4">目标 4</div>
</section>
<aside id="nav">
    <nav>
        <a class="link" id="link_1" href="#1">点 1</a>
        <a class="link" id="link_2" href="#2">点 2</a>
        <a class="link" id="link_3" href="#3">点 3</a>
        <a class="link" id="link_4" href="#4">点 4</a>
    </nav>
</aside>
a.active {
  background-color: #CCCCFF;
}

还有JS代码:

// 不需要 JQuery

function isInViewport(el) {
  const rect = el.getBoundingClientRect();
  const windowHeight = (window.innerHeight || document.documentElement.clientHeight);
  const windowWidth = (window.innerWidth || document.documentElement.clientWidth);
  const vertInView = (rect.top <= windowHeight) && ((rect.top + rect.height) >= 0);
  const horInView = (rect.left <= windowWidth) && ((rect.left + rect.width) >= 0);

  return (vertInView && horInView);
}

window.onscroll = () => {
  for(el of document.getElementsByClassName('list')) {
    el.classList.remove('active');   // 重置列表    
  }
  // 找到视口中的元素
  for(el of document.getElementsByClassName('target')) {
    if(isInViewport(el)) {
      // 设置活动元素
      document.getElementById('link_' + el.id).classList.add('active');
    }
  }
}

这应该适用于你。

英文:

That's what you're looking for is named scrollspy. Try something like that:

&lt;section id=&quot;main&quot;&gt;
    &lt;div class=&quot;target&quot; id=&quot;1&quot;&gt;TARGET 1&lt;/div&gt;
    &lt;div class=&quot;target&quot; id=&quot;2&quot;&gt;TARGET 2&lt;/div&gt;
    &lt;div class=&quot;target&quot; id=&quot;3&quot;&gt;TARGET 3&lt;/div&gt;
    &lt;div class=&quot;target&quot; id=&quot;4&quot;&gt;TARGET 4&lt;/div&gt;
&lt;/section&gt;
&lt;aside id=&quot;nav&quot;&gt;
    &lt;nav&gt;
        &lt;a class=&quot;link&quot; id=&quot;link_1&quot; href=&quot;#1&quot;&gt;Punkt 1&lt;/a&gt;
        &lt;a class=&quot;link&quot; id=&quot;link_2&quot; href=&quot;#2&quot;&gt;Punkt 2&lt;/a&gt;
        &lt;a class=&quot;link&quot; id=&quot;link_3&quot; href=&quot;#3&quot;&gt;Punkt 3&lt;/a&gt;
        &lt;a class=&quot;link&quot; id=&quot;link_4&quot; href=&quot;#4&quot;&gt;Punkt 4&lt;/a&gt;
    &lt;/nav&gt;
&lt;/aside&gt;
a.active {
  background-color: #CCCCFF;
}

And JS:

// doesn&#39;t require JQuery

function isInViewport(el) {
  const rect = el.getBoundingClientRect()
  const windowHeight = (window.innerHeight || document.documentElement.clientHeight)
  const windowWidth = (window.innerWidth || document.documentElement.clientWidth)
  const vertInView = (rect.top &lt;= windowHeight) &amp;&amp; ((rect.top + rect.height) &gt;= 0)
  const horInView = (rect.left &lt;= windowWidth) &amp;&amp; ((rect.left + rect.width) &gt;= 0)

  return (vertInView &amp;&amp; horInView)
}

window.onscroll = () =&gt; {
  for(el of document.getElementsByClassName(&#39;list&#39;)) {
    el.classList.remove(&#39;active&#39;)   // reset list    
  }
  // finding element in the viewport
  for(el of document.getElementsByClassName(&#39;target&#39;)) {
    if(isInViewport(el)) {
      // setting active element
      document.getElementById(&#39;link_&#39; + el.id).classList.add(&#39;active&#39;)
    }
  }
}

That should work for you.

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

发表评论

匿名网友

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

确定