Home > Web Front-end > JS Tutorial > How to achieve sliding door effect in js

How to achieve sliding door effect in js

王林
Release: 2020-03-10 10:35:13
forward
1906 people have browsed it

How to achieve sliding door effect in js

1. Technologies required to implement sliding doors

1. Simple basic HTML knowledge

2. Simple basic CSS styles

3. Basic javascript knowledge

(Recommended tutorial: javascript tutorial)

2. Implementation method

HTML

<div id="container">
  <img src="images/20190503222903.png"/><!--图片可以自己修改-->
  <img src="images/20190503222943.png"/>
  <img src="images/20190503223003.png"/>
  <img src="images/20190503223514.png"/>
</div>
Copy after login

CSS

*{
  margin: 0;
  padding: 0;
  background-color: #ccc;
}
p{
  text-align: center;
}
#container{
  width: 1130px;
  height: 350px;
  margin: 0 auto;
  border-right:1px solid #FF0000;
  border-bottom:1px solid #FF0000;
  overflow: hidden;
  position: relative;
}
#container img{
  width:500px;
  height:350px;
  display: block;
  position: absolute;
  border-bottm:1px solid #FF0000;
}
Copy after login

JS

//加载dom树
window.onload = function(){
//定义盒子
var box=document.getElementById(&#39;container&#39;);
//定义图片
var imgs=box.getElementsByTagName(&#39;img&#39;);
//图片宽度
var imgWidth = imgs[0].offsetWidth;
//隐藏宽度
var exposeWidth = 210;
//盒子宽度
var boxWidth = imgWidth + (imgs.length -1) * exposeWidth;
box.style.width=&#39;px&#39;;
//设置每道门的初始位置
function SetImgsPos(){
for(var i = 1;i<imgs.length;i++){
      imgs[i].style.left = imgWidth + exposeWidth*(i -1)+ &#39;px&#39;;
      }
    }
    SetImgsPos();
     //计算每道门应该移动的距离
    var translate = imgWidth - exposeWidth;
    //为每道门绑定事件
    for(var i=0;i<imgs.length;i++){
      //使用立即调用的函数表达式,为了获得不同的i值
      (function(i){
        imgs[i].onmouseover = function(){
          SetImgsPos();
          //打开门
          for(var j=1;j<=i;j++){
            imgs[j].style.left = parseInt(imgs[j].style.left,10) - translate + &#39;px&#39;;
          }
        }
      })(i);
    }
  }
Copy after login

For more programming-related content, please pay attention to the Programming Introduction column on the php Chinese website!

The above is the detailed content of How to achieve sliding door effect in js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
js
source:jb51.net
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template