我需要一种自动/以编程方式通过WordPress站点主页上的锚移动的方法。锚,我想通过的是中间的标签。
例如:页面加载,等待指定时间,选项卡2打开,等待指定时间,选项卡3打开,等待指定时间,选项卡4打开,然后继续重复。一旦它走到尽头,我希望它回到起点。理想情况下,如果鼠标悬停,它就会停止,但我还没有尝试实现它。
我试图在主页上显示的帖子的“文本”部分创建一个JavaScript程序,但它似乎不起作用。我看到了"Test"警报,但从未看到"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>标签的插件是容易响应的标签。
多亏了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
}
}发布于 2014-09-27 20:35:13
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();https://stackoverflow.com/questions/26076996
复制相似问题