简体   繁体   English

锚链接会在另一页中打开灯箱

[英]Anchor link opens Lightbox in another page

I'm having a bit of an issue figuring out a very simple interaction. 在解决一个非常简单的交互方面,我遇到了一个问题。

Some Context: I'm working on a website that showcases some products in a grid and when clicked, a Lightbox pops up with the information of the product..pretty simple! 一些上下文:我正在一个网站上展示一些产品,这些产品以网格形式显示,当单击时,会弹出一个Lightbox,其中包含产品信息。非常简单! Roughly my markup/script: 大概我的标记/脚本:

<img id="1234" src=".../blah.jpg"></img>

$( img ).click(function() {
  // open (this) lightbox
  // etc. etc.
});

Now I'm trying to implement the search functionality, which exists obviously in another page. 现在,我正在尝试实现搜索功能,该功能显然存在于另一页中。 The search reutrn a list of products each one with a path such as: 搜索返回产品列表,每个产品都有如下路径:

<a href="/parentCat/childCat/#1234">Product 1234</a>

So if I click the item, it will take me to the correct page where the item exist and since I'm including the anchor link, it will kind of place it visible to the user. 因此,如果我单击该项目,它将带我到该项目存在的正确页面,并且由于我包括锚链接,因此它将在某种程度上对用户可见。 This works fine. 这很好。

My question is, how can I make the Lightbox open automatically after being directed from the search to the actual category page where the product exists? 我的问题是,从搜索定向到产品所在的实际类别页面后,如何使灯箱自动打开?

This seems to be super simple, but for some reason I can't manage to figure it out! 这似乎非常简单,但是由于某些原因,我无法弄清楚! Any help would be really appreciated! 任何帮助将非常感激!

Thanks! 谢谢!

So when the dom is ready on the category page, you wan to check the url to see if an anchor exists. 因此,当dom在类别页面上准备就绪时,您需要检查URL以查看锚点是否存在。 This will mean that they have arrived via the search results page. 这意味着他们已经通过搜索结果页面到达了。

reference: How can you check for a #hash in a URL using JavaScript? 参考: 如何使用JavaScript检查URL中的#哈希? .

Something like this: 像这样:

 if(window.location.hash) {
      var hash = window.location.hash.substring(1); //Puts hash in variable, and removes the # character
      alert (hash);
      // hash found
      // open (this) lightbox
  } 

If it exists, get the product id from the hashtag and trigger the lightbox functionality 如果存在,请从主题标签获取产品ID,并触发灯箱功能

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

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