自动移动锚点

问题描述:

我需要一种方法来自动/编程移动锚点在WordPress site的主页上。我想穿过的锚是中间的标签。自动移动锚点

例如:页面加载,等待指定时间,标签2打开,等待指定时间,标签3打开,等待指定时间,标签4打开,然后继续重复。一旦它结束,我希望它回到开始。理想情况下,如果鼠标悬停,它将停止,但我还没有尝试实现。

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

<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> 

标签的插件是Easy Responsive Tabs

感谢brasofilo,这里是最后的工作代码:

<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来防止点击:

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 
    } 
} 
+0

[旁注]你'涡轮Background_web编辑-2_edited-1.png'有百日咳5.6MB,可以很容易地深入到KB的... – brasofilo 2014-09-27 20:11:20

+0

谢谢你的笔记。我还没有优化图像。我很感激你花时间提及它! – tmbouman 2014-09-28 01:46:11

+0

有关如何让tabSlider停止鼠标悬停的任何想法?谢谢! – tmbouman 2014-09-28 02:07:17

location.hash只会将哈希值添加到URL中,而不会实际导航到哈希值。

正如你已经在网站上加载jQuery的,它只是一个强制点击锚的事情:

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()必须在最后一次超时之内,这样它才会产生所需的循环。

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

工作完美! – tmbouman 2014-09-28 01:46:27

+0

是的,我知道,我使用Chrome Dev Tools直接在您的页面上测试了代码:) – brasofilo 2014-09-28 01:48:30