简体   繁体   English

使用Javascript / CSS / HTML链接到轮​​播中的特定幻灯片

[英]Link to a specific slide in carousel with Javascript/CSS/HTML

There have been other questions similar to this, but the answers always pertain to specific JQuery code that I'm not using. 还有其他与此类似的问题,但是答案始终与我未使用的特定JQuery代码有关。 Instead, I'm using the W3Schools Slideshow example here: 相反,我在这里使用W3Schools幻灯片示例:

http://www.w3schools.com/howto/howto_js_slideshow.asp http://www.w3schools.com/howto/howto_js_slideshow.asp

I have links on a different page, and I'd like each of those links to open the slideshow page on a specific slide. 我在其他页面上有链接,我希望每个链接都可以在特定幻灯片上打开幻灯片页面。

The most recent thing I tried is this: 我最近尝试过的是:

LINK on separate page: 链接在单独的页面上:

<a href="slideshow.html#currentSlide(3)">

Dot links under the slideshow on slideshow.html (couldn't think of anything else to link to): 在slideshow.html上的幻灯片下的点链接(无法想到要链接的其他任何内容):

<div style="text-align:center">
  <span class="dot" onclick="currentSlide(1)"></span>
  <span class="dot" onclick="currentSlide(2)"></span>
  <span class="dot" onclick="currentSlide(3)"></span>
   <span class="dot" onclick="currentSlide(4)"></span>
  <span class="dot" onclick="currentSlide(5)"></span>
  <span class="dot" onclick="currentSlide(6)"></span>
</div>

And javascript that supposedly tells the slideshow what to load based on the hash: javascript可以告诉幻灯片根据哈希值加载什么:

if (location.hash)
{
    $('#' + location.hash.substring(1)).click();
}

Probably a dumb idea, but I'm relatively new to this stuff and am out of ideas. 可能是个愚蠢的主意,但我对这些东西还比较陌生,并且没有主意。

You've almost got it, but you've got one issue. 差不多了,但是有一个问题。 Your script that handles which item to show references an element that doesn't exist. 处理要显示的项目的脚本引用了不存在的元素。 You wrote: 你写了:

$('#' + location.hash.substring(1)).click();

Which is going to evaluate to something like this: 这将评估为这样的事情:

$('#currentSlide(1)').click();

That tells jQuery to target an element with id=currentSlide(1) , which doesn't exist on your page. 这告诉jQuery使用id=currentSlide(1)定位元素,该元素在您的页面上不存在。

What I would recommend instead is to name your URL fragments with just the index of the image you want to show, like this: 相反,我建议使用仅要显示的图像的索引来命名URL片段,如下所示:

<a href="slideshow.html#0">Image 1</a>
<a href="slideshow.html#1">Image 2</a>
<a href="slideshow.html#2">Image 3</a>

Then in the slideshow.html page use this command to trigger the click event: 然后在slideshow.html页面中使用此命令来触发click事件:

$(document).ready(function() {
  if (location.hash) {
    var hash = location.hash.slice(1);
    $(".dot").eq(hash).click();
  }
});

That will look for the dot class at the index provided , and click that element. 这将在提供的索引处查找dot类,然后单击该元素。

You could do something like this: 您可以执行以下操作:

if(location.hash){
     //assuming you have a hash like this -> slide1
     var slide =  location.hash;
     var slideNumber = slide.replace("#slide","");
     currentSlide(slide);
}

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

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