1

让我以我不了解 JS 的事实作为开头,因此非常感谢任何帮助。

我有一个滑动开关,我想在页面加载 3 秒后打开它。这是我目前的幻灯片切换:

$(function(){
  // hide/show header
  $('#close-open-top a').bind('click', function() {
    if($('header:visible').length) {
      $('img', this).attr('src', 'img/open.png');
    } else {
      $('img', this).attr('src', 'img/close.png');
    }
    $('header').slideToggle('slow');

    return false;
  });

目前它加载关闭,当我单击箭头时它会打开。我希望它在页面加载后 3 秒自动打开。

4

2 回答 2

2

在准备好的文档中尝试以下操作:

$('header').delay(3000).slideToggle('slow');

.delay()方法延迟动画队列中的后续项目(注意,您不能使用它来延迟非动画方法)。

或者,如果您希望执行单击处理程序中的所有代码(包括设置图像 src),那么最简单的方法是在 3 秒延迟后以编程方式触发单击,因此将以下内容添加到文档就绪处理程序的末尾(在点击处理程序的绑定之后):

setTimeout(function() { $('#close-open-top a').trigger('click'); }, 3000);
于 2012-05-22T00:20:33.413 回答
1

Well, I think i didn't get you totally..but maybe I can help you

to get a delay use setTimeout like:

setTimeout(function(){ $('header').slideToggle('slow');
},3000);

so inside of the function you can put the code to be executed. The second parameter has to be chosem as the milliseconds you want to be waited before the function is called. If you want to do it automatically 3 seconds after Pageload use this:

$(document).ready(function(){
    setTimeout(function(){ $('header').slideToggle('slow');
    },3000);  
});
于 2012-05-22T00:27:23.817 回答