用 JS 点击左右按钮 使图片切换 - 最精简版-ljx2380000-ChinaUnix博客

用 JS 点击左右按钮 使图片切换 - 最精简版-ljx2380000-ChinaUnix博客

   代码部分:  

点击(此处)折叠或打开

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>实用的图片切换</title>
  6. <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.js"></script>
  7. <!--<script type="text/javascript" src="jquery_change.js"></script>-->
  8. <style type="text/css">
  9.  body,ul,li { padding:0; margin:0}
  10.  ul,li { list-style:none}
  11.  .img-scroll { position:relative; margin:20px auto; width:440px;}
  12.  .img-scroll .prev,.img-scroll .next { position:absolute; display:block; width:50px; height:100px; background-color:#000;
  13.  top:0; color:#FFF; text-align:center; line-height:100px}
  14.  .img-scroll .prev { left:0;cursor:pointer;}
  15.  .img-scroll .next { right:0;cursor:pointer;}
  16.  .img-list { position:relative; width:320px; height:100px; margin-left:60px; overflow:hidden}
  17.  .img-list ul { width:9999px;}
  18.  .img-list li { float:left; display:inline; width:100px; margin-right:10px; height:100px; background-color:#BDBDDF; text-align:center; line-height:100px;}
  19. </style>
  20. </head>
  21. <body>
  22. <div class="img-scroll">
  23.     <span class="prev">prev</span>
  24.     <span class="next">next</span>
  25.     <div class="img-list">
  26.         <ul>
  27.             <li><img style="width:100px;height:100px;" src="./img/cat.jpg"></li>
  28.             <li><img style="width:100px;height:100px;" src="./img/katong.jpg"></li>
  29.             <li><img style="width:100px;height:100px;" src="./img/meinv.jpg"></li>
  30.             <li><img style="width:100px;height:100px;" src="./img/mm.jpg"></li>
  31.             <li><img style="width:100px;height:100px;" src="./img/慢慢.jpg"></li>
  32.             <li><img style="width:100px;height:100px;" src="./img/美食杰-糖糖.jpg"></li>
  33.             <li><img style="width:100px;height:100px;" src="./img/未命名44.jpg"></li>
  34.         </ul>
  35.     </div>
  36. </div>
  37. <script type="text/javascript">
  38.  function DY_scroll(wraper,prev,next,img,speed,or)
  39.  {
  40.   var wraper = $(wraper);
  41.   var prev = $(prev);
  42.   var next = $(next);
  43.   var img = $(img).find('ul');
  44.   var w = img.find('li').outerWidth(true);
  45.   var s = speed;
  46.   next.click(function()
  47.        {
  48.         img.animate({'margin-left':-w},function()
  49.                   {
  50.                    img.find('li').eq(0).appendTo(img);
  51.                    img.css({'margin-left':0});
  52.                    });
  53.         });
  54.   prev.click(function()
  55.        {
  56.         img.find('li:last').prependTo(img);
  57.         img.css({'margin-left':-w});
  58.         img.animate({'margin-left':0});
  59.         });
  60.   if (or == true)
  61.   {
  62.    ad = setInterval(function() { next.click();},s*1000);
  63.    wraper.hover(function(){clearInterval(ad);},function(){ad = setInterval(function() { next.click();},s*1000);});

  64.   }
  65.  }
  66.  DY_scroll('.img-scroll','.prev','.next','.img-list',3,false);// true为自动播放,不加此参数或false就默认不自动
  67. </script>
  68. </body>
  69. </html>




posted @ 2014-12-03 15:39  Tim&Blog  阅读(716)  评论(0编辑  收藏  举报