繁体   English   中英

如何在javascript中链接到同一页面锚

[英]how to link to a same page anchor in javascript

我想将Coverflow强大滑块中的专辑封面链接到同一页面中的锚点。 它在顶部,页面下方有与每个封面相对应的各种音频播放器,每个都有自己的锚点。 如何使用链接url属性转到锚点。 这张专辑“ danceawaythenight”的主播是dan。

已经尝试了列表项两侧的链接,但这只会使专辑封面消失。

我在滑块上使用的代码如下所示。

每个专辑封面都有一个类似于以下链接的链接(目前指向的是插件滑块随附的默认链接)

<li class=”slide” data-mightyslider=” cover:’images/albumcovers/danceawaythenight.jpg’, link: { url: ‘https://itunes.apple.com/us/album/enrique-iglesias-greatest/id295364999?uo=4’, target: ‘_blank’ } ”>

您可以获取目标元素的坐标并为其设置滚动位置。

这是一种更简单的方法:

function jump(h){
    var url = location.href;               //Save down the URL without hash.
    location.href = "#"+h;                 //Go to the target element.
    history.replaceState(null,null,url);   //Don't like hashes. Changing it back.
}​

这使用replaceState来操纵URL。 如果您还需要对IE的支持,那么您将不得不采用复杂的方法。

function jump(h){
    var top = document.getElementById(h).offsetTop; //Getting Y of target element
    window.scrollTo(0, top);                        //Go there.
}​

您可能需要根据锚点设置链接网址。 并将链接目标设置为“ _self”或“ _top”。

<li class=”slide” data-mightyslider=” cover:’images/albumcovers/danceawaythenight.jpg’, link: { url: ‘#your-anchor-name’, target: ‘_self’ } ”>

暂无
暂无

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

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