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

时间:2021-02-26 05:53:04

body
{
font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI",Tahoma,Helvetica,Sans-Serif,"Microsoft YaHei", Georgia,Helvetica,Arial,sans-serif,宋体, PMingLiU,serif;
font-size: 10.5pt;
line-height: 1.5;
}
html, body
{

}
h1 {
font-size:1.5em;
font-weight:bold;
}
h2 {
font-size:1.4em;
font-weight:bold;
}
h3 {
font-size:1.3em;
font-weight:bold;
}
h4 {
font-size:1.2em;
font-weight:bold;
}
h5 {
font-size:1.1em;
font-weight:bold;
}
h6 {
font-size:1.0em;
font-weight:bold;
}
img {
border:0;
max-width: 100%;
}
blockquote {
margin-top:0px;
margin-bottom:0px;
}
table {
border-collapse:collapse;
border:1px solid #bbbbbb;
}
td {
border-collapse:collapse;
border:1px solid #bbbbbb;
}

用 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>