【问题标题】:TweenLite animation suddenly changing elements' position?TweenLite 动画突然改变元素的位置?
【发布时间】:2016-08-14 12:50:41
【问题描述】:

首先,您可以在JSFiddle 和问题下方找到我的代码的简化演示。我发现我的问题发生在我在谷歌浏览器中描述的方式,所以如果你打算尝试修复这个错误,请使用那个浏览器。如果代码没有很好地简化,我深表歉意;请认为这是一个更大项目的 sn-p。

我正在开发一个使用 JQuery 和 GreenSock 的 TweenLite 制作动画的 web 应用程序。

这个应用程序包含一些控制一切的菜单,这些菜单在使用bodyChange() 功能之间进行转换。这个函数有两个参数:

  • nextOrPrev,根据值运行一个或另一个动画 提供("next""prev")。只有"next" 动画完成了,但这并不重要。 "prev" 动画,尚未使用,只是发出一个 alert("prev")
  • bodyFunction。提供的函数将使用该菜单所需的元素填充正文,并将它们包装在 #bodyWrap 中。

在我为您提供的演示中,只有两个菜单:第一个,mainMenu,只有一个#playButton。点击后会调用bodyChange()函数,参数如下:("next", playSettingsBody)playSettings是第二个菜单。

这就是问题所在:当您单击playButton 时,按钮会在屏幕上向上移动a,然后执行TweenLite 动画。但是,我看不到为什么按钮会“跳起来”,而不是留在原地并执行动画。这可能是由于一个小错误。这是什么?

感谢您的帮助。

mainMenuBody();

function mainMenuBody() {
	$("body").append(
	//BUTTONS
	"<div id='playButton' class='mainButton'><div class='buttonText mainButtonText text'>PLAY</div></div>"
	);
	//WRAP
	$("body").wrapInner("<div id='bodyWrap'></div>");
	//BINDS
	$("#playButton").bind("click", function() {
		bodyChange("next", playSettingsBody);
	});	
}

function bodyChange(nextOrPrev, bodyFunction) {

	switch (nextOrPrev) {
		case "next":
			//ANIMATION AND BODY CHANGE
			TweenLite.to($("#bodyWrap"), .4, {
					ease: Power2.easeIn,
					transform: "rotateY(90deg)",
					onComplete: function(){
						$("body").empty();
					//NEW STUFF
						bodyFunction();
						TweenLite.from($("#bodyWrap"), .4, {
							ease: Power2.easeOut,
							transform: "rotateY(90deg)"
						});
					}
			});
			//END OF ANIMATION AND BODY CHANGE
			break;

		case "prev":
			alert("prev");
	}
}

function playSettingsBody() {
	$("body").append(
	"<p class='text' id='CYTText'>This is the second menu!</p>"
  );
}
body{
	background-image: url("../resources/pics/Vignette2.png");
    background-repeat: no-repeat;
	background-position: center center;
	background-attachment: fixed;
	background-color: #02BFC1;
	overflow:hidden;
	margin: 0;
	
	}

.text {
	color: #FFFFFF;
	font-family:Bebas Neue;
	-webkit-user-select: none;
	cursor: default;
	text-shadow: 3px 3px 2px rgba(0,0,0,0.2);

	}
  
 .mainButton {
	-webkit-transform:scale(1);
	width: 200px;
	height: 200px;
	border: 10px solid #F1F2F0;
	text-align:center;
	background-color: #F37C2B;
	/*background:#5F4A21;*/
	display: table;
	position: absolute;
	margin: auto;
	top: 150px;
    bottom: 0;
	-webkit-transition: all 0.5s ease;
	cursor: pointer;
	box-shadow: 0px 0px 30px rgba(0,0,0,0.4);
	}
  
 .mainButtonText {
	position: relative;
	display:table-cell;
	vertical-align:middle;
	-webkit-transform:scale(1);
	font-size: 90px;
	text-shadow: 4px 4px 2px rgba(0,0,0,0.2);
	-webkit-transition: all 0.5s ease;
	}
<script src="https://code.jquery.com/jquery-3.1.0.min.js" integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.19.0/TweenMax.min.js"></script>

【问题讨论】:

  • 我的编辑是你想要的吗?
  • 我可以对答案进行更改以使其成为更合适的答案吗?
  • @Dan 我试过你的方法,但它在主项目中不起作用。正如我在 GSAP 论坛中了解到的,这个问题实际上是由于 CSS 中的一个 querk 而与动画无关。我决定暂时不做动画,因​​为它对整个项目来说并不重要,并在稍后阶段解决问题。无论如何,谢谢你的回答:)
  • 我可以问一下我的答案有什么问题吗?
  • 这可能是我草率的改编,因为你的效果与我想要的不同。我将来会重新审视这个,它可能会更好地适应。

标签: javascript jquery css animation gsap


【解决方案1】:

这个问题是在你的.mainButton 类中引起的。你的代码看起来有点像这样。

.mainButton {
    position: absolute;
    top: 150px;
    bottom: 0;
    //rest
}

通过删除bottom: 0; 行,您的JSFiddle 现在可以按预期工作。但是,如果您删除行 top: 150px; 并保留在 bottom: 0 中,问题仍然存在。不幸的是,我无法对此作出解释。可能值得在 GSAP 论坛上发布一个问题,询问为什么在使用 bottom 定位时会发生这种情况,但在使用 top 时不会发生这种情况

编辑
由于您需要 bottom: 0 而我无法修复您的代码,因此我编写了一个使用 GSAP 插件 Timeline 的示例。您可以查看此JSFiddle 或下面的代码示例。

var tl = new TimelineMax();
tl.pause();
tl.fromTo($("#click"), 1, {rotationY: 0, ease: Power2.easeOut}, {rotationY: 90, transformOrigin:"right", ease: Power2.easeOut})
  .set($("#click2"), {css:{display: "table"}}, "-=0.6")
  .fromTo($("#click2"), 1, {rotationY: -90, ease: Power2.easeOut}, {rotationY: 0, transformOrigin:"left", ease: Power2.easeOut}, "-=0.6");

$("#click").click(function() {
  tl.play();
});

$("#click2").click(function() {
  tl.reverse();
});
* {
  margin: 0;
  padding: 0;
}

body {
  background-image: url("../resources/pics/Vignette2.png");
  background-repeat: no-repeat;
  background-position: center center;
  background-attachment: fixed;
  background-color: #02BFC1;
  overflow: hidden;
}

div.one, div.two {
  position: absolute;
  bottom: 0;
  height: 200px;
  width: 200px;
  background: #F37C2B;
  text-align: center;
  display: table;
  cursor: pointer;
  border: 10px solid #F1F2F0;
}
div.one .text, div.two .text {
  position: relative;
  display: table-cell;
  vertical-align: middle;
  color: #FFFFFF;
  font-family: Bebas Neue;
  font-size: 90px;
}
div.two {
  display: none;
  border-color: transparent;
  background: none;
}
div.two .text {
  font-size: 40px;
}
<script src="https://code.jquery.com/jquery-3.1.0.min.js" integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.19.0/TweenMax.min.js"></script>

<div id="click" class="one">
  <div class="text">
    Play
  </div>
</div>

<div id="click2" class="two">
  <div class="text">
    Second Menu
  </div>
</div>

【讨论】:

  • 我需要那个bottom: 0。此外,下一个菜单也在它应该在的位置之上。无论如何,谢谢你,如果我在这里找不到帮助,我会在 GSAP 论坛上发帖。
  • @JocaPinto 我用一个工作示例更新了我的答案,正如你所想的那样。如果不是,请告诉我
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-11
  • 1970-01-01
  • 2018-04-22
  • 2018-10-22
  • 1970-01-01
  • 2013-12-23
  • 2017-02-18
相关资源
最近更新 更多