简体   繁体   English

跳到同一页面上的隐藏部分

[英]Jump to a hidden section on the same page

I have a 3 groups of content in a page that can be navigated through tabs. 我在页面中有3组内容,可以通过选项卡进行导航。 There's a default visible tab when I enter the page and then I can click the tabs to hide the current content and show the other. 当我进入页面时,有一个默认的可见选项卡,然后我可以单击这些选项卡以隐藏当前内容并显示其他内容。

example.html example.html

<a href="example" name="example" class="tab active">Example</a> // default
<a href="about" name="about" class="tab">About</a>
<a href="contact" name="contact" class="tab">...</a>

...

<div class="example visible" id="example">...</div> // default visible content
<div class="about" id="about">...</div>
<div class="contact" id="contact">...</div>

Now, I added a footer where there are links connecting to the 3 tabs. 现在,我添加了一个页脚,其中有链接连接到3个选项卡。 So, if I am in a different part of the site, I can click a link, say, About Me , that would navigate to example.html and straight to the section About Me . 因此,如果我位于网站的其他部分,则可以单击“ 关于我”链接,该链接将导航到example.html并直接转到关于我一节。

footer.html footer.html

<ul>
   <li><a href="/example">Example</a></li>
   <li><a href="/example#about">Example About</a></li>
   <li><a href="/example#contact">Example Contact</a></li>
</ul>

The above works perfectly fine. 以上工作完全正常。 But here's the problem , when I click on the footer links while I am on example.html, it jumps to the position of the content but it cannot be seen because the current tab is still active. 但这是问题所在 ,当我在example.html上时单击页脚链接 ,它会跳到内容的位置,但由于当前选项卡仍处于活动状态而无法看到。 Is there a way to check the url whenever it changes so I can parse it and change the active tab based on it? 有没有一种方法可以随时检查url,以便我可以解析它并基于它更改活动选项卡?

JS JS

$(document).ready(function() {
// find which footer link was clicked; doesn't work when navigating on the same page
  var hash = window.location.href.slice(window.location.href.indexOf('#') + 1);

if(hash === "foundation") {
     $("a[name='foundation']").addClass("active").siblings().removeClass("
active");
     $(".foundation").addClass("visible").siblings().removeClass("visible");
}
else if(hash === "credits") {...}
...



// click event for tabs
  $("a").on("click", function(event) {
    event.preventDefault();
    $(this).addClass("active").siblings().removeClass("active");
    var currentAttrVal = $(this).attr("href");
    $("." + currentAttrVal).addClass("visible").siblings().removeClass("visible");
  });
});

I know the script is too repetitive, but I'll refactor everything once I sort this out! 我知道脚本太重复了,但是一旦解决了它,我就会重构所有内容!

the location object has its own property for the current hash 位置对象对当前哈希具有自己的属性

window.location.hash

this includes the hash sign too so you need 这也包括井号,所以您需要

window.location.hash.slice(1)

For listening to the change of the hash you will have to use the onhashchange event 为了监听哈希的变化,您将不得不使用onhashchange事件

You can use one of the following techniques 您可以使用以下技术之一

window.onhashchange = funcRef;  

or 要么

window.addEventListener("hashchange", funcRef, false);

This way is used in frameworks like angular when they use the #! 当他们使用#!时,这种方法可用于诸如angular的框架中。 way of defining routes for browsers that do not support the History API 为不支持History API的浏览器定义路由的方法

You can read more about it to MDN 您可以向MDN进一步了解

Here the concept which i think may help you: ( jsfiddle version ) 我认为这里的概念可能会对您有所帮助:( jsfiddle版本

footer.html footer.html

<ul>
  <li>
    <input type="button" class="navigateButton" value="example" onclick="navigate(value)">
  </li>
  <li>
    <input type="button" class="navigateButton" value="About" onclick="navigate(value)">
  </li>
  <li>
    <input type="button" class="navigateButton" value="Contact" onclick="navigate(value)">
  </li>
</ul>

CSS 的CSS

.navigateButton {
  background: none!important;
  border: none;
  padding: 0!important;
  font: inherit;
  /*border is optional*/
  border-bottom: 1px solid #444;
  cursor: pointer;
}

JS JS

var navigate = function (value) {

  switch (value) {
    case "example":
    //select appropriate tab here
      window.location.replace('/example');
      break;
    case "About":
    //select appropriate tab here
      window.location.replace('/example#About');
      break;      
    case "Contact":
    //select appropriate tab here
    window.location.replace('/example#Contact');
    break;
    default:
      alert('Unknown type');
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM