简体   繁体   English

HTML / CSS动画-JavaScript

[英]HTML/CSS Animation - JavaScript

I've been trying to animate a sliding door that is triggered on the click of a button. 我一直在尝试对滑动门进行动画处理,该滑动门是通过单击按钮触发的。

Here is my fiddle 这是我的小提琴

I've got two sides of the sliding door. 我有推拉门的两侧。 Left side is blue, right side is red. 左侧为蓝色,右侧为红色。 The left side should slide to the left and the right door should slide to the right. 左侧应滑动到左侧,而右侧门应滑动到右侧。

First of all, I'm trying to position the button to the middle of the door. 首先,我试图将按钮定位在门的中间。 I'm using 我正在使用

#button {
  z-index: 2;
  position: absolute;
  top: 50%;
  left: 50%;
}

but still the button appears kind of sideways 但是按钮仍然有点偏斜

But secondly when the button is clicked, both sides of the door should slide out at the same time, but unfortunately only the red door functions correctly. 但是其次,当单击按钮时,门的两侧应同时滑出,但不幸的是,只有红色门才能正常工作。

The blue door is stuck. 蓝色的门被卡住了。 What am I doing wrong? 我究竟做错了什么?

Defining same vars causing issues and for the left door you need to decrease value _pos-- 定义导致问题的相同变量,对于左门,您需要减小值_pos--

Solution for button 按钮解决方案

left: calc(50% - 38px);

 #container { width: 810px; } #button { z-index: 2; position: absolute; top: 50%; left: 50%; } #wrapper { z-index: 1; position: absolute; display: inline-block; width: 810px; } #left { display: inline-block; width: 400px; } #right { display: inline-block; width: 400px; } #myContainer { width: 400px; height: 300px; position: relative; background: yellow; } #myAnimationLeft { width: 400px; height: 300px; position: absolute; background-color: blue; } #myAnimationRight { width: 400px; height: 300px; position: absolute; background-color: red; } 
 <div id="container"> <div id="button"> <button onclick="myMove()">Click Me</button> </div> <div id="wrapper"> <div id="left"> <div id="myContainer"> <div id="myAnimationLeft"></div> </div> </div> <div id="right"> <div id="myContainer"> <div id="myAnimationRight"></div> </div> </div> </div> </div> <script> function myMove() { var _elem = document.getElementById("myAnimationLeft"); var _pos = 0; var _id = setInterval(_frame, 5); function _frame() { if (_pos == 410) { clearInterval(_id); } else { _pos--; _elem.style.right = _pos + 'px'; _elem.style.left = _pos + 'px'; } } var elem = document.getElementById("myAnimationRight"); var pos = 0; var id = setInterval(frame, 5); function frame() { if (pos == 410) { clearInterval(id); } else { pos++; elem.style.left = pos + 'px'; elem.style.right = pos + 'px'; } } } </script> 


JQuery Solution created on @shubhamagrawal's answer. 基于@shubhamagrawal的答案创建的JQuery解决方案。

 $(document).ready(function() { $('button').click(function() { $("#myAnimationLeft").offset({ left: 0 }) $("#myAnimationRight").offset({ left: $("#myAnimationRight").width() }) $("#myAnimationLeft").animate({ left: -$("#myAnimationLeft").width() }, 2000); $("#myAnimationRight").animate({ left: $("#myAnimationRight").width() }, 2000); }) }) 
 body, html { margin: 0; padding: 0; } #container { width: 810px; position:relative; overflow:hidden; height:300px; } #button { z-index: 2; position: absolute; top: calc(50% - 11px); left: calc(50% - 34px); } #wrapper { z-index: 1; position: absolute; display: inline-block; width: 810px; } #left { display: inline-block; width: 400px; } #right { display: inline-block; width: 400px; } #myContainer { width: 400px; height: 300px; position: relative; background: yellow; } #myAnimationLeft { width: 400px; height: 300px; position: absolute; background-color: blue; } #myAnimationRight { width: 400px; height: 300px; position: absolute; background-color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="container"> <div id="button"> <button>Click Me</button> </div> <div id="wrapper"> <div id="left"> <div id="myContainer"> <div id="myAnimationLeft"></div> </div> </div> <div id="right"> <div id="myContainer"> <div id="myAnimationRight"></div> </div> </div> </div> </div> 

You can use simple JQuery animation to do what you require. 您可以使用简单的JQuery animation执行所需的操作。

FIDDLE 小提琴

Here is the code: 这是代码:

 $("button").click(function() { $(".one").animate({ left: '0' }); $(".three").animate({ left: '200px' }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="one" style="background:#98bf21;height:100px;width:100px;position:absolute;left:100px;"></div> <div class="two" style="background:blue;height:100px;width:100px;position:absolute;left:100px;"> </div> <div class="three" style="background:red;height:100px;width:100px;position:absolute;left:100px;"></div> <button style="position:absolute;left:110px;top:50px;"> CLICK ME </button> 

You need to minus half the button width and height to bring it to the center. 您需要减去按钮的宽度和高度的一半,以使其居中。
If button's width is fixed, its correct to use calc(50% - 50px) as Icewine's answer. 如果按钮的宽度固定,则使用calc(50% - 50px)作为Icewine的答案是正确的。

For elements with dynamic widths and heights u can always use: 对于具有动态宽度和高度的元素,您可以始终使用:

position: absolute; 
top: 50%; 
left: 50%;
transform:translateX(-50%) translateY(-50%);

The above code will center the element even if you dont know the height and width of the element. 即使您不知道元素的高度和宽度,上面的代码也会将元素居中。

Example: 例:

 body { padding: 0px; margin: 0px; } div { width: 100px; height: 100px; position: absolute; top: 50%; left: 50%; transform: translateX(-50%) translateY(-50%); background: red; } 
 <div></div> 

As for animation, why not use classes and let the CSS handle the animation? 至于动画,为什么不使用类并让CSS处理动画呢?

 function myMove() { document.getElementById("myAnimationLeft").className = "DoorOpenLeft"; document.getElementById("myAnimationRight").className = "DoorOpenRight"; } 
 * { box-sizing: border-box; } body { margin: 0px; } #container { width: 100%; } #button { z-index: 2; position: absolute; top: 50%; left: 50%; display: inline-block; transform: translateX(-50%) translateY(-50%); } #wrapper { z-index: 1; position: absolute; width: 100%; overflow-x: hidden; clear: both; } #left { display: inline-block; width: 50%; float: left; } #right { display: inline-block; width: 50%; float: right; } #myContainer { width: 100%; height: 300px; position: relative; background: yellow; } #myAnimationLeft { width: 100%; height: 300px; left: 0px; position: absolute; background-color: blue; transition: all 1s ease; } #myAnimationRight { width: 100%; height: 300px; left: auto; right: 0px; position: absolute; background-color: red; transition: all 1s ease; } .DoorOpenLeft { left: -100% !important; } .DoorOpenRight { right: -100% !important; } 
 <div id="container"> <div id="button"> <button onclick="myMove()">Click Me</button> </div> <div id="wrapper"> <div id="left"> <div id="myContainer"> <div id="myAnimationLeft"></div> </div> </div> <div id="right"> <div id="myContainer"> <div id="myAnimationRight"></div> </div> </div> </div> </div> 

It should be like this 应该是这样

 function myMove() { var elem = document.getElementById("myAnimationRight"); var elem_l = document.getElementById("myAnimationLeft"); var elem_R = document.getElementById("myAnimationRight"); elem_l.className += " opened"; elem_R.className += " opened"; } 
 #container { width: 800px; } #button { z-index: 2; position: absolute; top: 50%; left: 50%; } #wrapper { z-index: 1; position: absolute; display:inline-block; width: 810px; overflow:hidden; } #left { display:inline-block; width: 400px; } #right { display:inline-block; width: 400px; } #myContainer { width: 400px; height: 300px; position: relative; background: yellow; } #myAnimationLeft { width: 400px; height: 300px; position: absolute; background-color: blue; transition:linear all 0.5s; left:0; } #myAnimationRight { width: 400px; height: 300px; position: absolute; background-color: red; transition:linear all 0.5s; right:0; } #myAnimationRight.opened{ right:-100%; transition:linear all 0.5s; } #myAnimationLeft.opened{ left:-100%; transition:linear all 0.5s; } 
 <div id="container"> <div id ="button"> <button onclick="myMove()">Click Me</button> </div> <div id="wrapper"> <div id ="left"> <div id ="myContainer"> <div id ="myAnimationLeft"></div> </div> </div> <div id ="right"> <div id ="myContainer"> <div id ="myAnimationRight"></div> </div> </div> </div> </div> 

You can handle animation via CSS and just add class opened to elements on button click. 您可以通过CSS处理动画,只需在按钮单击时将opened类添加到元素中即可。

For the button, you need to minus the width and height of the button to center it. 对于按钮,您需要减去按钮的宽度和高度以使其居中。 left: calc(50% - 50px); 左:calc(50%-50px); if button width is 100px; 如果按钮宽度为100像素;

Also, you need to set the parent div above button to position: relative; 另外,您需要将按钮上方的父div设置为position:relative; or the absolute wont work. 或绝对不会工作。 You should also set a height of the parent div while you are at it. 您还应该设置父div的高度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM