jquery实现图片自动播放

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
<style>
ul{ margin:0px;padding:0px;width:360px;}
li{ float:left; margin-left:10px; list-style-type:none;width:40px;background-color:#96EDE4;text-align:center;}
img{ width:166px;height:112px;}
#stop{ clear:both;}
</style>
<script src="jquery.js"></script>
</head>

<body>
<div><img src="img/1.gif" id='img1' /></div>
<div><ul><li id='1'>1</li><li id='2'>2</li><li id='3'>3</li></ul></div>
<div id='stop'><input type="button" value="停止自动播放"</div>


<script>
function showImg1(){
 $('#img1').attr('src','img/1.gif');
}
function showImg2(){
 $('#img1').attr('src','img/2.gif');
}
function showImg3(){
 $('#img1').attr('src','img/3.gif');
}
function autoShow(){
 setT1 = setTimeout('showImg2()',1000);
 setT2 = setTimeout('showImg3()',2000);
 setT3 = setTimeout('showImg1()',3000);
}

function clearTimeSet(){
 clearTimeout(setT1);
 clearTimeout(setT2);
 clearTimeout(setT3);
}

var int = setInterval("autoShow()",3000);

$('#stop').click(function(){
 clearTimeSet();
 int = window.clearInterval(int); 
})

$('li').mouseover(function(){

 if($(this).attr('id') == '1'){
   
  int = window.clearInterval(int);
  clearTimeSet();
  showImg1(); 
   
 }else if($(this).attr('id') == '2'){
  
  int = window.clearInterval(int); 
  clearTimeSet();
  showImg2();
  
 }else if($(this).attr('id') == '3'){
  
  int = window.clearInterval(int);
  clearTimeSet();
  showImg3(); 
 }
})

$('li').mouseout(function(){
 int = setInterval("autoShow()",3000);
})

</script>
</body>
</html>
                                                                                                                            

                                                                                                                             李汉团   Mar 9,2012

转载于:https://my.oschina.net/adamboy/blog/43699

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用jQuery实现图片自动轮播的示例代码: ```html <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8"> <meta name="author" content="Your Name"> <title>图片自动轮播</title> <style> .slideshow { position: relative; width: 500px; height: 300px; overflow: hidden; } .slideshow img { position: absolute; top: 0; left: 0; width: 100%; height: 100%; opacity: 0; transition: opacity 1s ease-in-out; } .slideshow img.active { opacity: 1; } </style> <script src="js/jquery-1.12.3.min.js"></script> <script> $(document).ready(function() { var images = $(".slideshow img"); var currentIndex = 0; function showImage(index) { images.removeClass("active"); images.eq(index).addClass("active"); } function nextImage() { currentIndex++; if (currentIndex >= images.length) { currentIndex = 0; } showImage(currentIndex); } setInterval(nextImage, 2000); }); </script> </head> <body> <div class="slideshow"> <img src="image1.jpg" alt="Image 1"> <img src="image2.jpg" alt="Image 2"> <img src="image3.jpg" alt="Image 3"> </div> </body> </html> ``` 这段代码使用了jQuery库来实现图片自动轮播。首先,我们定义了一个包含图片的容器div,并设置其宽度和高度。然后,我们使用CSS来控制图片的显示和过渡效果。每张图片都有一个active类,通过添加或移除这个类来控制图片的显示和隐藏。 在JavaScript部分,我们使用了jQuery的ready函数来确保文档加载完成后执行代码。我们获取了所有的图片元素,并定义了一个变量来跟踪当前显示的图片索引。showImage函数用于显示指定索引的图片,nextImage函数用于切换到下一张图片。最后,我们使用setInterval函数来定时调用nextImage函数,实现自动轮播效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值