【问题标题】:Floating Input Labels Using GreenSock (GSAP)使用 GreenSock (GSAP) 的浮动输入标签
【发布时间】:2017-11-29 17:57:58
【问题描述】:

我正在尝试使用 GreenSock 创建浮动输入标签。如果输入字段有值,我希望能够将标签保留在顶部,如果没有,则将标签返回原位。

现在动画只在一个输入元素上运行一次。需要帮助弄清楚如何为每个输入执行此操作。

// SETUP
const tl = new TimelineMax();
const container = $(".input-container");

// CLICK EVENT
container.on("click", function() {

  // VARIABLES
  const label = $(this).find(".label");


  // TWEEN
  tl.to(label, 0.35, {left: "5", top: "1", transform: "scale(.75)", color: "#333"});  

});

// FOCUSOUT EVENT
container.on("focusout", function() {

  const input = $(this).find(".input");
  if (input.val() === "") {
    tl.reverse();
  }

});

这是我目前所拥有的:[Demo Link]1

【问题讨论】:

标签: javascript jquery animation gsap


【解决方案1】:

问题是,您对所有标签使用相同的TimelineMax 对象。每个输入/容器都需要自己的时间线。

您也许可以像这样将 Timeline 实例直接附加到 DOM:

// SETUP
const container = $(".input-container");

// CLICK EVENT
container.on("click", function() {

  // VARIABLES
  const label = $(this).find(".label");

  this.tl = new TimelineMax();
  // TWEEN
  this.tl.to(label, 0.35, {left: "5", top: "1", transform: "scale(.75)", color: "#333"});  

});

// FOCUSOUT EVENT
container.on("focusout", function() {

  const input = $(this).find(".input");
  if (input.val() === "") {
    this.tl.reverse();
  }

});

Updated CodePen

【讨论】:

  • 啊,从那以后,我比我想象的更近了。谢谢!
  • 我尝试将它从 .on('click', 更改为 .on('focus' 但无法让它发挥作用?再次感谢!
  • 使用focusin 而不仅仅是focus
  • 你是最棒的,看来我需要处理我的 jquary/JS
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-04
  • 2021-10-27
  • 2021-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-20
相关资源
最近更新 更多