Home >Web Front-end >JS Tutorial >JS realizes sliding door animation effect

JS realizes sliding door animation effect

php中世界最好的语言
php中世界最好的语言Original
2018-04-18 15:59:152525browse

This time I will bring you JS to realize the sliding door dynamic effect. What are the precautions for JS to realize the sliding door dynamic effect. The following is a practical case, let's take a look.

"Sliding door" implementation method one: change the picture width

html css code

<body>
 <p class="box">
  <ul>
   <!-- <li>![](images/slidepic2.jpg)</li> -->
   <li></li>
   <li></li>
   <li></li>
   <li></li>
   <li></li>
  </ul>
 </p>
</body>
<style>
  *{
   padding: 0;
   margin: 0;
  }
  .box{
   /*收缩状态:缩放时每个图片的大小240px 所以总大小1200px*/
   /*展开状态:当前图片宽度800px 其他图片宽度100px*/
   width: 1200px;
   height: 500px;
   border:1px solid red;
   margin: 50px auto;
  }
  .box ul{
   list-style: none;
   width: 1210px;
  }
  /*设置每一张图片的大小和float: left*/
  .box ul li{
   width: 240px;
   height: 500px;
   /*background: url(images/slidepic2.jpg);*/
   float: left;
  }
 </style>

jQuery implementation

<script src = &#39;jquery-3.2.1.js&#39;></script>
<script>
 $(function(){
  //1遍历每一张li 获取每个元素设置对应的图片
  var lis = $('li');
  lis.each(function(index, element){
   //通过设置背景图片名称改变图片的显示
   var imgName = "images/slidepic" + (index + 2) +".jpg ";
   $(element).css('background', "url('"+ imgName +"')")
  });
  //2.展开状态
  //鼠标滑入改变对应图片宽度800 其他图片(兄弟)改为100
  lis.mouseenter(function(){
   // console.log(this); 当前的li DOM元素
   //当前的图片的宽度变为800
   $(this).stop().animate({width: 800});
   //其他图片的宽度变为100
   $(this).siblings('li').stop().animate({width: 100});
  });
  //3鼠标滑出是全部显示为收缩状态
  lis.mouseout(function(){
   lis.stop().animate({width: 240});
  });
 })
</script>

"Sliding door" implementation method two: change the offset value of the image

html css code

<body>
 <p class="picList">
  <ul>
   <li>![](images/slidepic8.jpg)</li>
   <li>![](images/slidepic3.jpg)</li>
   <li>![](images/slidepic4.jpg)</li>
   <li>![](images/slidepic5.jpg)</li>
   <li>![](images/slidepic7.jpg)</li>
  </ul>
 </p>
</body>
<style>
  *{
   background-color: #aaa;
   padding: 0;
   margin: 0;
  }
  ul{list-style: none;}
  .picList{
   width: 1000px;
   height: 400px;
   /*border:1px solid #eee;*/
   margin:100px auto;
   position: relative;
   overflow: hidden;
  }
  /*设置定位属性 所有图片覆盖在起始位置*/
  .picList ul li{
   position: absolute;
   width: 1000px;
   height: 400px;
   top: 0;
  }
  img{
   width: 100%;
   height: 400px;
   cursor: pointer;
  }
</style>

jQuery implementation

<script src = &#39;jquery-3.2.1.js&#39;></script>
<script >
 $(function(){
  //1获取所有的图片 设置初始的收缩状态left:i*200
  var lis = $('li');
  for(var i = 0; i < lis.length; i++){
   lis.eq(i).css({left:i*200 + 'px' });
  }
  //2.设置hover内置函数,实现鼠标滑入展开滑出收缩效果
  lis.hover(function(){
   var index = $(this).index(); //DOM对象转换jQuery对象
   //2.1鼠标滑入后,当前图片的前面图片偏移位置减小到 j*100位置
   for(var j = 0; j <= index; j++){
    lis.eq(j).stop().animate({left: j*100 + 'px'},300);
   }
   //2.2鼠标滑入后,当前图片的后面图片偏移位置扩大到 500+j*100位置
   for(var j = index + 1; j < lis.length; j++){
    lis.eq(j).stop().animate({left: 500+j*100 + 'px'},300);
   }
  },function(){ //2.3鼠标滑出后,所有图片恢复到原来的位置 i*200
   for(var i = 0; i < lis.length; i++){
    lis.eq(i).stop().animate({left: i*200 + 'px'},300);
   }
  });
 })
</script>

NOTE: During the implementation of method 1, pay attention to the settings of width and image naming.

Tip: The jQuery code is used here, and the javaScript code can be implemented in the same way. Just modify the traversal process and built-in function methods, and rewrite the animation function (the previous notes have an encapsulated animate function, which can be directly introduced and used) .

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:



The above is the detailed content of JS realizes sliding door animation effect. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn