admin管理员组文章数量:1435859
I would like to replace a div's contents with another via a slide animation; the first div slides to the left outside of the box (hidden), whilst the second slides in.
I tried this;
/
But it does not appear to be doing anything. What am I doing wrong?
var $oldBox = $("#signup .box[data-step=1]");
var $newBox = $("#signup .box[data-step=2]");
var outerWidth = $oldBox.outerWidth(true);
var posSlideOut = (2 > 1 ? -outerWidth : outerWidth);
var posSlideIn = (2 > 1 ? outerWidth : -outerWidth);
$.when($oldBox.animate({left: posSlideOut}, "slow"), $newBox.css("left", posSlideIn + "px").animate({"left": 0}, "slow"));
I would like to replace a div's contents with another via a slide animation; the first div slides to the left outside of the box (hidden), whilst the second slides in.
I tried this;
http://jsfiddle/DTcHh/1203/
But it does not appear to be doing anything. What am I doing wrong?
var $oldBox = $("#signup .box[data-step=1]");
var $newBox = $("#signup .box[data-step=2]");
var outerWidth = $oldBox.outerWidth(true);
var posSlideOut = (2 > 1 ? -outerWidth : outerWidth);
var posSlideIn = (2 > 1 ? outerWidth : -outerWidth);
$.when($oldBox.animate({left: posSlideOut}, "slow"), $newBox.css("left", posSlideIn + "px").animate({"left": 0}, "slow"));
Share
Improve this question
asked Sep 23, 2014 at 20:09
user429620user429620
3
- You're not triggering an animation by any means. You're just declaring it. – Radu Andrei Commented Sep 23, 2014 at 20:25
- Your code seems fine, but, it never works. I mean by that, it never execute. The only problem I can see right now, is that you never trigger the animation of the $oldBox. So, never gonna see the execution. – Academia Commented Sep 23, 2014 at 20:26
-
The default value for the css property
position
isstatic
, so changing theleft
property will not have any effect unless you changeposition
to something likerelative
– Stryner Commented Sep 23, 2014 at 20:40
2 Answers
Reset to default 3Here is my update to get the javascript working
jsfiddle
The main changes were that I added the $(document).on('click')
event to fire the animation and switched left
to margin-left
since you are not using relative or fixed positioning
This should get you in the right direction
Update:
Also, I added javascript to remove the "display: hidden;"
from your second div
GreenSock Animation Platform (GSAP) with TweenLite
/ TweenMax
. It provides much smoother transitions with far greater customization than jQuery or CSS3 transitions. In order to animate CSS properties with TweenLite / TweenMax, you'll also need their plugin called "CSSPlugin". TweenMax includes this automatically.
First, load the TweenMax library:
<script src="https://cdnjs.cloudflare./ajax/libs/gsap/1.18.0/TweenMax.min.js"></script>
Or the lightweight version, TweenLite:
<script src="https://cdnjs.cloudflare./ajax/libs/gsap/1.18.0/plugins/CSSPlugin.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/gsap/1.18.0/easing/EasePack.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/gsap/1.18.0/TweenLite.min.js"></script>
Then, call your animation:
var myObj= document.getElementById("myDiv");
// Syntax: (target, speed, {distance, ease})
TweenLite.to(myObj, .7, { x: 500, ease: Power3.easeOut});
You can also call it with an ID selector:
TweenLite.to("#myID", .7, { x: 500, ease: Power3.easeOut});
If you have jQuery loaded, you can use more advanced broad selectors, like all elements containing a specific class:
// This will parse the selectors using jQuery's engine.
TweenLite.to(".myClass", .7, { x: 500, ease: Power3.easeOut});
For full details, see: TweenLite Documentation
According to their website: "TweenLite is an extremely fast, lightweight, and flexible animation tool that serves as the foundation of the GreenSock Animation Platform (GSAP)."
本文标签: javascriptReplace div with leftin slide animationStack Overflow
版权声明:本文标题:javascript - Replace div with left-in slide animation - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745667812a2669386.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论