图片左右轮播


  
  
  1. <div class="imageRotation">
  2. <div class="imageBox">
  3. <a href="http://www.itxueyuan.org" target="_blank"><img src="images/1.jpg" /></a>
  4. <a href="http://www.itxueyuan.org" target="_blank"><img src="images/2.jpg" /></a>
  5. <a href="http://www.itxueyuan.org" target="_blank"><img src="images/3.jpg" /></a>
  6. <a href="http://www.itxueyuan.org" target="_blank"><img src="images/4.jpg" /></a>
  7. <a href="http://www.itxueyuan.org" target="_blank"><img src="images/5.jpg" /></a>
  8. </div>
  9. <div class="titleBox">
  10. <p class="active"><span>第一张图片标题</span></p>
  11. <p>第二张图片标题</p>
  12. <p>第三张图片标</p>
  13. <p>第四张图片标题</p>
  14. <p>第五张图片标题</p>
  15. </div>
  16. <div class="icoBox">
  17. <span class="active" rel="1">1</span>
  18. <span rel="2">2</span>
  19. <span rel="3">3</span>
  20. <span rel="4">4</span>
  21. <span rel="5">5</span>
  22. </div>
  23. </div>


说明:每个图片轮播都必须放在 class="imageRotation" 的容器中,所有图片放在 class="imageBox" 的容器中,标题放在 class="titleBox" 的容器中,图标放在 class="icoBox" 的容器中。图片、标题和图标的数目要相等。



CSS代码



  
  
  1. .imageRotation{
  2. height:270px;
  3. width:570px;
  4. overflow:hidden; /*--超出容器的所有元素都不可见--*/
  5. position:relative; /*--相对定位--*/
  6. border:10px solid #eee;
  7. bodrer-radius:5px;
  8. -webkit-border-radius:5px;
  9. -moz-border-radius:5px;
  10. }
  11. /*-------------图片容器---------------*/
  12. .imageBox{
  13. position:absolute; /*--固定定位--*/
  14. height:270px;
  15. top:0px;
  16. left:0px;
  17. overflow:hidden;
  18. }
  19. .imageBox img {
  20. display:block;
  21. height:270px;
  22. width:570px;
  23. float:left;
  24. border:none;
  25. }
  26. /*-------------标题容器---------------*/
  27. .titleBox{
  28. position:absolute; /*--固定定位--*/
  29. bottom:0px;
  30. width:570px;
  31. height:40px;
  32. overflow:hidden;
  33. }
  34. .titleBox p{
  35. position:absolute; /*--固定定位--*/
  36. bottom:-40px;
  37. width:550px;
  38. height:40px;
  39. margin:0px;
  40. padding:0px 10px;
  41. line-height:40px;
  42. z-index:1;
  43. background-color:#000;
  44. color:#fff;
  45. font-family:"微软雅黑","yahei";
  46. opacity:0.5;
  47. -moz-opacity:0.5;
  48. -webkit-opacity:0.5;
  49. filter:alpha(opacity=50);
  50. }
  51. .titleBox p span{
  52. opacity:1;
  53. -moz-opacity:1;
  54. -webkit-opacity:1;
  55. filter:alpha(opacity=100);
  56. }
  57. .titleBox p.active{
  58. bottom:0px;
  59. }
  60. /*-------------图标容器---------------*/
  61. .icoBox{
  62. position:absolute; /*--固定定位--*/
  63. bottom:14px;
  64. right:15px;
  65. width:76px;
  66. height:12px;
  67. text-align:center;
  68. line-height:40px;
  69. z-index:2;
  70. }
  71. .icoBox span{
  72. display:block;
  73. float:left;
  74. height:12px;
  75. width:12px;
  76. margin-left:3px;
  77. overflow:hidden;
  78. background:url("images/ico.png") 0px 0px no-repeat;
  79. cursor:pointer;
  80. }
  81. .icoBox span.active {
  82. background-position:0px -12px;
  83. cursor:default;
  84. }


javascript代码


  
  
  1. $(document).ready(function() {
  2. $(".imageRotation").each(function(){
  3. // 获取有关参数
  4. var imageRotation = this, // 图片轮换容器
  5. imageBox = $(imageRotation).children(".imageBox")[0], // 图片容器
  6. titleBox = $(imageRotation).children(".titleBox")[0], // 标题容器
  7. titleArr = $(titleBox).children(), // 所有标题(数组)
  8. icoBox = $(imageRotation).children(".icoBox")[0], // 图标容器
  9. icoArr = $(icoBox).children(), // 所有图标(数组)
  10. imageWidth = $(imageRotation).width(), // 图片宽度
  11. imageNum = $(imageBox).children().size(), // 图片数量
  12. imageReelWidth = imageWidth*imageNum, // 图片容器宽度
  13. activeID = parseInt($($(icoBox).children(".active")[0]).attr("rel")), // 当前图片ID
  14. nextID = 0, // 下张图片ID
  15. setIntervalID, // setInterval() 函数ID
  16. intervalTime = 4000, // 间隔时间
  17. imageSpeed =500, // 图片动画执行速度
  18. titleSpeed =250; // 标题动画执行速度
  19. // 设置 图片容器 的宽度
  20. $(imageBox).css({'width' : imageReelWidth + "px"});
  21. // 图片轮换函数
  22. var rotate=function(clickID){
  23. if(clickID){ nextID = clickID; }
  24. else{ nextID=activeID<=3 ? activeID+1 : 1; }
  25. // 交换图标
  26. $(icoArr[activeID-1]).removeClass("active");
  27. $(icoArr[nextID-1]).addClass("active");
  28. // 交换标题
  29. $(titleArr[activeID-1]).animate(
  30. {bottom:"-40px"},
  31. titleSpeed,
  32. function(){
  33. $(titleArr[nextID-1]).animate({bottom:"0px"} , titleSpeed);
  34. }
  35. );
  36. // 交换图片
  37. $(imageBox).animate({left:"-"+(nextID-1)*imageWidth+"px"} , imageSpeed);
  38. // 交换IP
  39. activeID = nextID;
  40. }
  41. setIntervalID=setInterval(rotate,intervalTime);
  42. $(imageBox).hover(
  43. function(){ clearInterval(setIntervalID); },
  44. function(){ setIntervalID=setInterval(rotate,intervalTime); }
  45. );
  46. $(icoArr).click(function(){
  47. clearInterval(setIntervalID);
  48. var clickID = parseInt($(this).attr("rel"));
  49. rotate(clickID);
  50. setIntervalID=setInterval(rotate,intervalTime);
  51. });
  52. });
  53. });
这三个配置可以更改

intervalTime = 4000,  // 间隔时间
imageSpeed =500,  // 图片动画执行速度
titleSpeed =250;  // 标题动画执行速度




第二个版本

<!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>JS图片轮播特效-左右切换</title>
 
<style type="text/css">
.imageRotation{
 height:270px;
 width:570px;
 overflow:hidden;  /*--超出容器的所有元素都不可见--*/
 position:relative;  /*--相对定位--*/
 border:10px solid #eee;
 bodrer-radius:5px;
 -webkit-border-radius:5px;
 -moz-border-radius:5px;
 }
/*-------------图片容器---------------*/
.imageBox{
 position:absolute;  /*--固定定位--*/
 height:270px;
 top:0px;
 left:0px;
 overflow:hidden;
 }
.imageBox img {
 display:block;
 height:270px;
 width:570px;
 float:left;
 border:none;
 }
/*-------------标题容器---------------*/
.titleBox{
 position:absolute;
 bottom:0px;
 width:570px;
 height:40px;
 overflow:hidden;
 }
.titleBox p{
 position:absolute;
 bottom:-40px;
 width:550px;
 height:40px;
 margin:0px;
 padding:0px 10px;
 line-height:40px;
 z-index:1;
 border-top:1px solid #000;
 background-color:#000;
 color:#fff;
 font-family:"微软雅黑","yahei";
 opacity:0.5;
 -moz-opacity:0.5;
 -webkit-opacity:0.5;
 filter:alpha(opacity=50);
 }
.titleBox p span{
 opacity:1;
 -moz-opacity:1;
 -webkit-opacity:1;
 filter:alpha(opacity=100);
 }
.titleBox p.active{
 bottom:0px;
 }
/*-------------图标容器---------------*/
.icoBox{
 position:absolute;  /*--固定定位--*/
 bottom:14px;
 right:15px;
 width:76px;
 height:12px;
 text-align:center;
 line-height:40px;
 z-index:2;
 }
.icoBox span{
 display:block;
 float:left;
 height:12px;
 width:12px;
 margin-left:3px;
 overflow:hidden;
 background:url("images/ico.png") 0px 0px no-repeat;
 cursor:pointer;
 }
.icoBox span.active {
 background-position:0px -12px;
 cursor:default;
 }
</style>
</head>
<body>
<div class="imageRotation"> 
 <div class="imageBox">
 <a href="http://www.itxueyuan.org" target="_blank"><img src="images/1.png" /></a>
 <a href="http://www.itxueyuan.org" target="_blank"><img src="images/2.png" /></a>
 <a href="http://www.itxueyuan.org" target="_blank"><img src="images/3.png" /></a>
 <a href="http://www.itxueyuan.org" target="_blank"><img src="images/4.png" /></a>
        <a href="http://www.itxueyuan.org" target="_blank"><img src="images/5.png" /></a>
 </div>
    <div class="titleBox">
     <p class="active"><span>枫叶飘飘</span></p>
        <p>夜晚的灯光</p>
        <p>雨滴</p>
        <p>余辉</p>
        <p>座椅</p>
    </div>
 <div class="icoBox">
 <span class="active" rel="1">1</span>
 <span rel="2">2</span>
 <span rel="3">3</span>
 <span rel="4">4</span>
        <span rel="5">5</span>
 </div>
</div>
 
<script type="text/javascript" src="http://www.itxueyuan.org/uploads/javascript/jquery.js"></script>
<script type="text/javascript">
 
$(document).ready(function() {
 $(".imageRotation").each(function(){
 // 获取有关参数
 var imageRotation = this,  // 图片轮换容器
 imageBox = $(imageRotation).children(".imageBox")[0],  // 图片容器
 titleBox = $(imageRotation).children(".titleBox")[0],  // 标题容器
 titleArr = $(titleBox).children(),  // 所有标题(数组)
 icoBox = $(imageRotation).children(".icoBox")[0],  // 图标容器
 icoArr = $(icoBox).children(),  // 所有图标(数组)
 imageWidth = $(imageRotation).width(),  // 图片宽度
 imageNum = $(imageBox).children().size(),  // 图片数量
 imageReelWidth = imageWidth*imageNum,  // 图片容器宽度
 activeID = parseInt($($(icoBox).children(".active")[0]).attr("rel")),  // 当前图片ID
 nextID = 0,  // 下张图片ID
 setIntervalID,  // setInterval() 函数ID
 intervalTime = 4000,  // 间隔时间
 imageSpeed =500,  // 图片动画执行速度
 titleSpeed =250;  // 标题动画执行速度
 // 设置 图片容器 的宽度
 $(imageBox).css({'width' : imageReelWidth + "px"});
 // 图片轮换函数
 var rotate=function(clickID){
 if(clickID){ nextID = clickID; }
 else{ nextID=activeID<=4 ? activeID+1 : 1; }
 // 交换图标
 $(icoArr[activeID-1]).removeClass("active");
 $(icoArr[nextID-1]).addClass("active");
 // 交换标题
 $(titleArr[activeID-1]).animate(
 {bottom:"-40px"},
 titleSpeed,
 function(){
 $(titleArr[nextID-1]).animate({bottom:"0px"} , titleSpeed);
 }
 );
 // 交换图片
 $(imageBox).animate({left:"-"+(nextID-1)*imageWidth+"px"} , imageSpeed);
 // 交换IP
 activeID = nextID;
 }
 setIntervalID=setInterval(rotate,intervalTime);
 $(imageBox).hover(
 function(){ clearInterval(setIntervalID); },
 function(){ setIntervalID=setInterval(rotate,intervalTime); }
 ); 
 $(icoArr).click(function(){
 clearInterval(setIntervalID);
 var clickID = parseInt($(this).attr("rel"));
 rotate(clickID);
 setIntervalID=setInterval(rotate,intervalTime);
 });
 });
});
</script>
 
</body>
</html>








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值