首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自动通过锚

自动通过锚
EN

Stack Overflow用户
提问于 2014-09-27 16:53:46
回答 1查看 118关注 0票数 2

我需要一种自动/以编程方式通过WordPress站点主页上的锚移动的方法。锚,我想通过的是中间的标签。

例如:页面加载,等待指定时间,选项卡2打开,等待指定时间,选项卡3打开,等待指定时间,选项卡4打开,然后继续重复。一旦它走到尽头,我希望它回到起点。理想情况下,如果鼠标悬停,它就会停止,但我还没有尝试实现它。

我试图在主页上显示的帖子的“文本”部分创建一个JavaScript程序,但它似乎不起作用。我看到了"Test"警报,但从未看到"Hello"警报。

代码语言:javascript
复制
<script type="text/javascript">
function scrollTo(hash) {
    location.hash = "#" + hash;
    alert('Hello');
}
function tabSlider() {
    delay = 2000; //2 second delay
    setTimeout(function() {scrollTo(ert_pane1-1);},delay);
    setTimeout(function() {scrollTo(ert_pane1-2);},delay*2);
    setTimeout(function() {scrollTo(ert_pane1-3);},delay*3);
    setTimeout(function() {scrollTo(ert_pane1-4);},delay*4);
    setTimeout(function() {scrollTo(ert_pane1-0);},delay*5);
    tabSlider();
    alert('Test');
}
window.onload = tabSlider();
//-->
</script>

标签的插件是容易响应的标签

多亏了brasofilo,下面是最后的工作代码:

代码语言:javascript
复制
<script type="text/javascript">
function scrollTo(hash) {
    location.hash = "#" + hash;
    jQuery( "a[href='#" + hash + "']" ).click(); // finds <a> with href==hash and clicks
}
function tabSlider() {
    delay = 3000; //2 second delay
    setTimeout(function() {scrollTo('ert_pane1-1');},delay);
    setTimeout(function() {scrollTo('ert_pane1-2');},delay*2);
    setTimeout(function() {scrollTo('ert_pane1-3');},delay*3);
    setTimeout(function() {scrollTo('ert_pane1-4');},delay*4);
    setTimeout(function() { scrollTo('ert_pane1-0'); tabSlider(); }, delay*5 );
}
window.onload = tabSlider();
//-->
</script>

对于那些想知道我是如何进行悬停的人,编辑,我只是使用jQuery来防止单击:

代码语言:javascript
复制
var hovering = 0;
jQuery ("#primary").hover(function() { hovering = 1; },
                  function() { hovering = 0; });
function scrollTo(hash) {
    if (hovering==1) {
    //Do nothing
    } else {
    location.hash = "#" + hash;
    jQuery( "a[href='#" + hash + "']" ).click(); // finds <a> with href==hash and clicks
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-27 20:35:13

location.hash只会将哈希添加到URL,实际上它不会导航到该哈希。

由于您已经在站点上加载jQuery,这只是强制单击锚点的问题:

代码语言:javascript
复制
function scrollTo(hash) {
    location.hash = "#" + hash;
    jQuery( "a[href='#" + hash + "']" ).click(); // finds <a> with href==hash and clicks
}

tabSlider函数有两个问题:

  • scrollTo(value)需要发送一个字符串,您正在发送一个不存在的“变量”。 如果您有var ert_pane1 = 'hash-value';,那么scrollTo(ert_pane1)就会工作。 因为这不是您需要的情况:scrollTo('ert_pane1')
  • tabSlider()的下一次调用必须在最后一次超时内,这样它就会产生所需的循环。
代码语言:javascript
复制
function tabSlider() {
    delay = 2000;
    setTimeout(function() { scrollTo('ert_pane1-1'); }, delay );
    setTimeout(function() { scrollTo('ert_pane1-2'); }, delay*2 );
    setTimeout(function() { scrollTo('ert_pane1-3'); }, delay*3 );
    setTimeout(function() { scrollTo('ert_pane1-4'); }, delay*4 );
    setTimeout(function() { scrollTo('ert_pane1-0'); tabSlider(); }, delay*5 );
}
tabSlider();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26076996

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档