用jquery中的document.ready()替换click()

时间:2010-05-03 11:01:26

标签: jquery effects document-ready

我下载了jquery效果示例,所有效果只出现在onclick上,但我希望它在document.ready()上执行并继续......

<script type="text/javascript">
 var ImgIdx = 2;//To mark which image will be select next

 function PreloadImg(){
  $.ImagePreload("images/im2.jpg");
  $.ImagePreload("images/im3.jpg");
  $.ImagePreload("images/im4.jpg");
  $.ImagePreload("images/im5.jpg");
 }
 $(document).ready(function(){
  PreloadImg();
  $(".SlashEff ul li").click(function(){
  $(".Slash").ImageSwitch({Type:$(this).attr("rel"),      NewImage:"images/im"+ImgIdx+".jpg", speed: 4000
          });
   ImgIdx++;
   if(ImgIdx>5) ImgIdx = 1;
  });  
 });
</script>

和我的

<div class="SlashEff">
 <ul>
 <li class="TryFadeIn" rel="FadeIn">Fade in</li>
  <li class="TryFlyIn"  rel="FlyIn">Fly in</li>
 <li class="TryFlyOut"  rel="FlyOut">Fly out</li>
 <li class="TryFlipIn"  rel="FlipIn">Flip in</li>
 <li class="TryFlipOut"  rel="FlipOut">Flip out</li>    
 <li class="TryScroll"  rel="ScrollIn">Scroll in</li>
 <li class="TryScroll"  rel="ScrollOut">Scroll out</li>
 <li class="TrySingleDoor" rel="SingleDoor">Single Door</li>
 <li class="TryDoubleDoor" rel="DoubleDoor">Double Door</li>
</ul>  
</div>

以下是链接http://www.hieu.co.uk/blog/index.php/imageswitch/

我试过了,

$(document).ready(function(){
  PreloadImg();
    $(".Slash").ImageSwitch({Type:$(this).attr("rel"), 
           NewImage:"images/im"+ImgIdx+".jpg", speed: 4000
          });
   ImgIdx++;
   if(ImgIdx>5) ImgIdx = 1;
 });

我尝试了这个,但它只执行了一次....我想每5000ms执行一次......这可能......

2 个答案:

答案 0 :(得分:4)

您可以使用setInterval来实现重复:

function swap() {
  $(".Slash").ImageSwitch({Type:$(this).attr("rel"), 
    NewImage:"images/im"+ImgIdx+".jpg", speed: 4000
  });
  ImgIdx++;
  if(ImgIdx>5) ImgIdx = 1;
}
$(document).ready(function(){
  PreloadImg();
  setInterval(swap, 5000);
});

答案 1 :(得分:0)

$(document).ready(function() {
  PreloadImg();
  setTimeout(doEffect, 5000);
});

function doEffect() {
  $(".Slash").ImageSwitch({Type:$(this).attr("rel"), 
    NewImage:"images/im"+ImgIdx+".jpg", speed: 4000
  });
  ImgIdx++;
  if(ImgIdx>5) ImgIdx = 1;
}