javascript - 动画后定义文本的高度和宽度

标签 javascript jquery html css

下面的代码在加载时为文本“hello Your World”设置动画。一切似乎都很好,但是当它返回到左侧面板时,我想要 “缩小到左侧面板后,我无法定义高度、宽度和字体大小,就像在 javascript 中一样”

jsfiddle

HTML:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container"> <span class="theText">
        H<span>ello</span>
  <span class="placeholder"></span>
  Y<span>our</span>
  <span class="placeholder"></span>
  W<span>orld</span>
  </span>
</div>

JavaScript:

 window.onload = function() {

$(function() {
  FixMargins();

var phWidth = $(".placeholder").width();
var height = $(".container").height();
containerHeight = $(".theText").height();
var newTopMargin = (height - containerHeight) / 2;

$(".theText").animate({
  transform: 2
}, {
  step: function(now, fx) {
    var newscale = 1 + +now;
   $(this).css({
     'transform': "scale(" + newscale + "," + newscale + ")",
               "opacity":-1 + now
               });
    $(this).css("margin-top",-150 + (newTopMargin+150) / 2 * now);
    $(".placeholder").css({
      "width": phWidth - phWidth * now/2
    });
    FixMargins();
  },
  duration: 3000
}).promise().done(function() {
  $(this).delay(3000);
  var textTop = $(".theText").css("margin-top").split('p')[0];
  var textLeft = $(".theText").css("margin-left").split('p')[0];
  $(this).animate({
    transform: 2
  }, {
    step: function(now, fx) {
      console.log(textLeft - textLeft * now);
      var newscale = 3 - +now;
      $(this).css('transform', "scale(" + newscale + "," + newscale + ")");

      $(".theText").css("marginLeft", textLeft - textLeft / 2 * now);
      $(".theText").css("marginTop", textTop - textTop / 2 * now);
    },
    duration: 3000


    }).promise().done(function() {

        $(this).find('span').css({
          "position":"absolute"
        }).animate({
          "width":0,
          "opacity":0
        });
      });
    });



});

function FixMargins() {

  width = $(".container").width();
  containerWidth = $(".theText").width();
  leftMargin = (width - containerWidth) / 2;
  $(".theText").css("marginLeft", leftMargin);

}

};

CSS:

.container {
  width: 500px;
  height: 300px;
  position: absolute;
  overflow: hidden;
  white-space: nowrap;
  border:1px solid red;
}
.theText {
  position: absolute;
  margin-top:-150px;
  opacity:0;
}
.placeholder {
  width: 200px;
  display: inline-block;
}

最佳答案

您需要在 jQuery 逻辑中控制这些属性。在您的情况下,您缺少要在第二个动画上设置动画的属性(“top”、“left”和“font-size”):

JS:

$(function () {
    FixMargins();

    var phWidth = $(".placeholder").width();
    var height = $(".container").height();
    var containerHeight = $(".theText").height();
    var newTopMargin = (height - containerHeight) / 2;

    $(".theText").animate({
        transform: 2
    }, {
        step: function (now, fx) {
            var newscale = 1 + now;
            $(this).css({
                'transform': "scale(" + newscale + "," + newscale + ")",
                    "opacity": -1 + now
            });
            $(this).css("margin-top", -150 + (newTopMargin + 150) / 2 * now);
            $(".placeholder").css({
                "width": phWidth - phWidth * now / 2
            });
            FixMargins();
        },
        duration: 3000
    }).promise().done(function () {
        $(this).delay(3000);
        var textTop = $(".theText").css("margin-top").split('p')[0];
        var textLeft = $(".theText").css("margin-left").split('p')[0];

        $(this).animate({
            "top": "10%",
            "left": "3%",
            fontSize : '30px',

            transform: 2
        }, {
            step: function (now, fx) {
                var newscale = 3 - +now;
                $(this).css('transform', "scale(" + newscale + "," + newscale + ")");

                $(".theText").css("marginLeft", textLeft - textLeft / 2 * now);
                $(".theText").css("marginTop", textTop - textTop / 2 * now);
            },
            duration: 3000
        }).promise().done(function () {

            $(this).find('span').css({
                "position": "absolute"
            }).animate({
                "width": 0,
                "opacity": 0
            }, function() {
                $(this).parent().animate({
                    "letter-spacing": "0.75em"
                });
            });
        });
    });



});

function FixMargins() {

    width = $(".container").width();
    containerWidth = $(".theText").width();
    leftMargin = (width - containerWidth) / 2;
    $(".theText").css("marginLeft", leftMargin);

}

关于javascript - 动画后定义文本的高度和宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31111167/

相关文章:

html - CSS 无法识别输入框?

html - IE 中的 CSS 固定标题表

javascript - react 状态变化不会导致重新渲染

javascript - 在 ember 应用程序中设置焦点

javascript - Redux-saga 无法读取react-native

jquery - 限制与可拖动和可放置连接的可排序中的元素数量

javascript - Safari 5 扩展 XMLHttpRequest 错误 : INVALID_STATE_ERR: DOM Exception 11

jquery - textArea和CodeMirror : How to fill text area and highlight?

javascript - 如何让按钮漂浮在剑道小部件上

javascript - 使用 AngularJS 中的链接覆盖单击表