简体   繁体   English

链接到另一个页面,执行功能,然后跳转到锚点

[英]Link to another page, execute function, then jump to anchor

I have two pages. 我有两页。 The first has a slideshow that allows me to add links for each slide. 第一个有一个幻灯片 ,可让我为每张幻灯片添加链接。 The second page has numerous expanding div's that are expandable/collapsible onclick using this show/hide function: 第二页包含许多扩展div,使用此show / hide函数可以在onclick上进行扩展/折叠:

 function showtxt(divID) {
   var item = document.getElementById(divID);
   if (item) {
     item.className=(item.className=='hidetxt')?'showtxt':'hidetxt';
   }
 }

Then each expanding/collapsing div on that page has its own function to call it when clicked: 然后,该页面上的每个展开/折叠div都有自己的函数,可在单击时调用它:

function ANCHORbutton() { 
  var img = document.getElementById('expANCHORbutton').src;
  if (img.indexOf('plus.png')!=-1) { 
    document.getElementById('expANCHORbutton').src  = 'minus.png'; 
  }
  else { 
    document.getElementById('expANCHORbutton').src = 'plus.png'; 
  } 
}

What I'd like to do, if possible, is link each slide from that slideshow to the second page, expand the corresponding div, and then jump down the page to the specified anchor. 如果可能的话,我想做的就是将幻灯片中的每张幻灯片链接到第二页,展开相应的div,然后将页面跳到指定的锚点。

If I didn't have everything collapsed, it'd be a simple href="http://domain.com/page2.html#ANCHOR" , but I'm struggling with how to expand the appropriate section before jumping to my anchor. 如果我没有崩溃,那将是一个简单的href="http://domain.com/page2.html#ANCHOR" ,但是我在跳转至锚点之前正在努力扩展适当的部分。

Problem 1: Collapsed divs all show so no need for browser to scroll 问题1:折叠的div全部显示,因此不需要浏览器滚动
Problem 2: Hidden divs are not being scrolled to since they are hidden. 问题2:由于隐藏的div是隐藏的,因此它们不会滚动到。

Solution could be this at the bottom of the page (onload does not trigger on reload on some browsers) 解决方案可能是页面底部(在某些浏览器上重载不会触发重载)

<script>
var hash = location.hash;
if (hash) {
  var divID = hash.substring(1); // remove the #
  showtxt(divID);
  document.getElementById(divID).scrollIntoView();
}
</script>
</body>

Update to add 100px: 更新以添加100px:

var hash = location.hash;
if (hash) {
  var divID = hash.substring(1); // remove the #
  showtxt(divID);
  var div = document.getElementById(divID); 
  div.scrollIntoView(); 
  div.style.top=(parseInt(div.style.top,10)+100)+"px"; 
}

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

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