根据导航栏中的活动链接显示内容。

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

Show content based on active link in nav bar

问题

I created three active links using CSS and JavaScript. 我使用CSS和JavaScript创建了三个活动链接。

I have three data sets in forms of image that I want to show based on which link is active. 我有三个图像数据集,我想根据哪个链接处于活动状态来显示它们。

The images will be below the link in the form of swiper slides. 这些图像将以swiper幻灯片的形式显示在链接下方。

How I do this? 我应该如何做到这一点?

My reference for this is the active slide on New Balance's website. 我的参考是New Balance网站上的活动幻灯片。

英文:

I created three active links using CSS and javascript. I have three data sets in forms of image that I want to show based on which link is active. The images will be below the link in form of swiper slides. How I do this? My reference for this is the active slide on new balances website

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.active-link {
  position: relative;
  display: flex;
  top: 12rem;
  /*position for active link */
  justify-content: center;
}

.button {
  padding: 0 40px 10px 40px;
  cursor: pointer;
  display: inline-block;
  font-weight: 500;
  border-bottom: 2px solid black;
}

.button:active,
.active {
  color: red;
  border-bottom: 5px solid red;
  padding-bottom: 7px;
}

.slide-container {
  position: relative;
  // I&#39;m not sure what you&#39;re looking for visually so I just used a top position greater than what you&#39;re already using on the buttons.
  top: 14rem;
  display: none;
}

.slide-container.active {
  display: block;
}

<!-- language: lang-html -->

&lt;div class=&quot;active-link&quot; id=&quot;active-link&quot;&gt;
  &lt;li&gt;
    &lt;a class=&quot;button&quot;&gt;Him&lt;/a&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;a class=&quot;button active&quot;&gt;2&lt;/a&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;a class=&quot;button&quot;&gt;Her&lt;/a&gt;
  &lt;/li&gt;
&lt;/div&gt;
&lt;div class=&quot;slide-containers&quot;&gt;
  &lt;div id=&quot;slide-container-1&quot; class=&quot;slide-container&quot;&gt;
    Slide Container 1
  &lt;/div&gt;
  &lt;div id=&quot;slide-container-2&quot; class=&quot;slide-container&quot;&gt;
    Slide Container 2
  &lt;/div&gt;
  &lt;div id=&quot;slide-container-3&quot; class=&quot;slide-container&quot;&gt;
    Slide Container 3
  &lt;/div&gt;
&lt;/div&gt;



&lt;script&gt;
  var btnContainer = document.getElementById(&quot;active-link&quot;);
  var btns = btnContainer.getElementsByClassName(&quot;button&quot;);

  function removeClass(elems, className) {
    [].forEach.call(document.querySelectorAll(elems), function(el) {
      el.classList.remove(className);
    });
  }

  const resetSlideContainers = () =&gt; {
    [...document.querySelectorAll(&#39;.slide-container&#39;)].forEach((slide) =&gt;
      slide.classList.remove(&#39;active&#39;));
  }

  for (var i = 0; i &lt; btns.length; i++) {
    btns[i].addEventListener(&#39;click&#39;, function(e) {
      removeClass(&#39;.button&#39;, &#39;active&#39;)
      this.classList.toggle(&#39;active&#39;);
      resetSlideContainers();
      document.getElementById(this.getAttribute(&#39;data-slide&#39;)).classList.add(&#39;active&#39;);
    })
  }
&lt;/script&gt;

<!-- end snippet -->

I created three active links using CSS and javascript. I have three data sets in forms of image that I want to show based on which link is active. The images will be below the link in form of swiper slides. How I do this? My reference for this is the active slide on new balances website

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var btnContainer = document.getElementById(&quot;active-link&quot;);
var btns = btnContainer.getElementsByClassName(&quot;button&quot;);

function removeClass(elems, className) {
  [].forEach.call(document.querySelectorAll(elems), function(el) {
    el.classList.remove(className);
  });
}

for (var i = 0; i &lt; btns.length; i++) {
  btns[i].addEventListener(&#39;click&#39;, function(e) {
    removeClass(&#39;.button&#39;, &#39;active&#39;)
    this.classList.toggle(&#39;active&#39;);
  })
}

<!-- language: lang-css -->

.active-link {
  position: relative;
  display: flex;
  top: 12rem;
  justify-content: center;
}

.button {
  padding: 0 40px 10px 40px;
  cursor: pointer;
  display: inline-block;
  font-weight: 500;
  border-bottom: 2px solid black;
}

.button:active,
.active {
  color: red;
  border-bottom: 5px solid blue;
  padding-bottom: 7px;
}

<!-- language: lang-html -->

&lt;div class=&quot;active-link&quot; id=&quot;active-link&quot;&gt;
  &lt;li&gt;
    &lt;a class=&quot;button&quot;&gt;1&lt;/a&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;a class=&quot;button active&quot;&gt;2&lt;/a&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;a class=&quot;button&quot;&gt;3&lt;/a&gt;
  &lt;/li&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 0

我会创建一个容器来存放每个幻灯片的内容,并使用数据属性将按钮链接到幻灯片。当点击按钮时,你可以移除前一个幻灯片的活动类,并为当前幻灯片添加一个新的类。以下是我添加的相关代码,并附有底部的Codepen链接。

```html
&lt;div class=&quot;active-link&quot; id=&quot;active-link&quot;&gt;
  &lt;li&gt;
    &lt;a class=&quot;button&quot; data-slide=&quot;slide-container-1&quot;&gt;1&lt;/a&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;a class=&quot;button active&quot; data-slide=&quot;slide-container-2&quot;&gt;2&lt;/a&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;a class=&quot;button&quot; data-slide=&quot;slide-container-3&quot;&gt;3&lt;/a&gt;
  &lt;/li&gt;
&lt;/div&gt;

&lt;div class=&quot;slide-containers&quot;&gt;
  &lt;div id=&quot;slide-container-1&quot; class=&quot;slide-container&quot;&gt;
    Slide Container 1
  &lt;/div&gt;
  &lt;div id=&quot;slide-container-2&quot; class=&quot;slide-container&quot;&gt;
    Slide Container 2
  &lt;/div&gt;
  &lt;div id=&quot;slide-container-3&quot; class=&quot;slide-container&quot;&gt;
    Slide Container 3
  &lt;/div&gt;
&lt;/div&gt;
.slide-container {
    position: relative;
    // 我不确定你在视觉上想要什么,所以我只是使用了一个比按钮上已经使用的top位置更大的值。
    top: 14rem;
    display: none;
}

.slide-container.active {
    display: block;
}
const resetSlideContainers = () =&gt; {
  [ ...document.querySelectorAll(&#39;.slide-container&#39;) ].forEach((slide) =&gt; slide.classList.remove(&#39;active&#39;));
}

for (var i = 0; i &lt; btns.length; i++) {
  btns[i].addEventListener(&#39;click&#39;, function(e) {
    removeClass(&#39;.button&#39;, &#39;active&#39;)
    this.classList.toggle(&#39;active&#39;);
    resetSlideContainers();
    document.getElementById(this.getAttribute(&#39;data-slide&#39;)).classList.add(&#39;active&#39;);
  })
}

这是一个可运行的示例


<details>
<summary>英文:</summary>

I would create a container to hold the contents of each slide and link buttons to a slide using a data attribute. When a button is clicked, you can remove the active class of the previous slide and add a new one for the current slide. Here is the relevant code that I added with a Codepen link at the bottom.

<div class="active-link" id="active-link">
<li>
<a class="button" data-slide="slide-container-1">1</a>
</li>
<li>
<a class="button active" data-slide="slide-container-2">2</a>
</li>
<li>
<a class="button" data-slide="slide-container-3">3</a>
</li>
</div>

<div class="slide-containers">
<div id="slide-container-1" class="slide-container">
Slide Container 1
</div>
<div id="slide-container-2" class="slide-container">
Slide Container 2
</div>
<div id="slide-container-3" class="slide-container">
Slide Container 3
</div>
</div>


.slide-container {
position: relative;
// I'm not sure what you're looking for visually so I just used a top position greater than what you're already using on the buttons.
top: 14rem;
display: none;
}

.slide-container.active {
display: block;
}


const resetSlideContainers = () => {
[ ...document.querySelectorAll('.slide-container') ].forEach((slide) => slide.classList.remove('active'));
}

for (var i = 0; i < btns.length; i++) {
btns[i].addEventListener('click', function(e) {
removeClass('.button', 'active')
this.classList.toggle('active');
resetSlideContainers();
document.getElementById(this.getAttribute('data-slide')).classList.add('active');
})
}


[Here is a working example](https://codepen.io/chadaort/pen/RwBKyxx).

</details>



huangapple
  • 本文由 发表于 2023年1月9日 07:07:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051928.html
匿名

发表评论

匿名网友

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

确定