首頁  >  文章  >  web前端  >  js單張圖片平移切換效果實例分享

js單張圖片平移切換效果實例分享

小云云
小云云原創
2018-01-24 16:01:342128瀏覽

本文主要為大家詳細介紹了js實現單張圖片平移切換效果,一張圖移動到左邊以後,從底部移回最右,等待下一次循環,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能幫助大家。

本文參考了JQuery實現圖片切換(自動切換+手動切換)

#由於個人不需要手動切換功能,因此把那部分的內容刪了,主要是增加了無縫切換的效果。

原理也很簡單,大概是一張圖移動到左邊以後,從底部移回最右,等待下一次循環。


<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <script src="js/jquery-1.10.1.min.js"></script>
</head>
<body> 
 <p class="wrapper"> 
  <h1>jquer实现图片切换</h1> 
  <p id="focus"> 
   <ul> 
    <!-- 这里有三个z-index的设置 -->
     <li><p class="switch_pic" style="z-index: 3;background: url(&#39;imgs/01.jpg&#39;) center center;background-size: cover;"></p></li> 
     <li><p class="switch_pic" style="z-index: 2;background: url(&#39;imgs/02.jpg&#39;) center center;background-size: cover;"></p></li> 
     <li><p class="switch_pic" style="z-index: 1;background: url(&#39;imgs/03.jpg&#39;) center center;background-size: cover;"></p></li> 
     <li><p class="switch_pic" style="background: url(&#39;imgs/04.jpg&#39;) center center;background-size: cover;"></p></li>
     <li><p class="switch_pic" style="background: url(&#39;imgs/meiko2.jpg&#39;) center center;background-size: cover;"></p></li>
     <li><p class="switch_pic" style="background: url(&#39;imgs/meiko7.jpg&#39;) center center;background-size: cover;"></p></li>
   </ul> 
   </p> 
  </p> 
  <script type="text/javascript">
  $(function() { 
  var sWidth = $("#focus").width(); 
  var len = $("#focus ul li").length; 
  var index = 0; 
  var picTimer; 
  var $pics = $("#focus ul li").find(&#39;.switch_pic&#39;);//获取所有图片

   showPics(index); //网页打开立即执行一次动画
   picTimer = setInterval(function() { 
    index++; 
    if(index == len) {index = 0;} 
    showPics(index); 
   },3000);//3000毫秒的间隔

  //显示图片函数,根据接收的index值显示相应的内容 
  function showPics(index) { //普通切换 
   var nowLeft = -sWidth; //每次移动固定量

   var $pic = $pics.eq(index);//获取当前要移出的图片
   var $nexPic = $pics.eq((index+1)%len);//当前要移入的图片
   var $nexnexPic = $pics.eq((index+2)%len);//下一个要移入的图片
   $nexPic.css("left",sWidth);//下一个图片移动到最右

   //当前要移出的图片开始左移,模式设为平滑"linear",速度和间隔一样
   $pic.animate({"left":nowLeft},3000,"linear",function(){
    // 当前图片完全移出后,重新设置z-index
    $pic.css("z-index",1);
    $nexPic.css("z-index",3);
    $nexnexPic.css("z-index",2);
   });
   //当前要移入的图片同时左移
   $nexPic.animate({"left":0},3000,"linear");
  } 
 }); 
</script>
<style type="text/css">
 *{margin:0;padding:0;} 
 body{font-size:12px;color:#222;font-family:Verdana,Arial,Helvetica,sans-serif;background:#f0f0f0;} 
 .clearfix:after{content: ".";display: block;height: 0;clear: both;visibility: hidden;} 
 .clearfix{zoom:1;} 
 ul,li{list-style:none;} 
 img{border:0;} 
 .wrapper{width:800px;margin:0 auto;padding-bottom:50px;} 
 h1{height:50px;line-height:50px;font-size:22px;font-weight:normal;font-family:"Microsoft YaHei",SimHei;margin-bottom:20px;} 

 #focus{width:450px;height:350px;overflow:hidden;position:relative;} 
 #focus ul{height:380px;position:absolute;} 
 #focus ul li{float:left;width:450px;height:350px;overflow:hidden;position:absolute;background:#000;} 
 #focus ul li p{position:absolute;overflow:hidden;width: 450px;height: 350px;} 
</style>
</body>

相關推薦:

JavaScript實作滑鼠滾輪控制頁面圖片切換功能範例

########################## ###實例詳解jQuery自動或手動圖片切換效果############JavaScript實作滑鼠滾輪控制頁面圖片切換的範例程式碼###########利用JavaScript製作圖片切換的方法介紹############簡單實作JavaScript圖片切換效果#######

以上是js單張圖片平移切換效果實例分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn