添加 div 元素,然后使用 JQUERY 从右到左设置动画

Append div element and then animate from right to left using JQUERY

我想输入一条消息,然后在单击提交按钮调用时 'shoot' 它将显示在屏幕右侧。但是,我希望文本从右到左为 animate/slide。 我该怎么做?

$('.submmit').click(function() {
  var c = "<div class='movethis' class='width: 40px;height: 50px;position: absolute;right: 0;'>" + $(".mytxt").val() + "</div>";
  $(".putbullet").append(c).animate({ "left": "0px" }, "slow");
  $(".movethis").animate({ "left": "0px" }, "slow");
  $(".movethis").css('right', '');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <input type="text" name="txt" class="mytxt">
  <input type="button" class="submmit" value="Shoot">
</div>
<div class="areaofcontent" style="width:68%;float:right; background-color:#eee;height:400px;overflow-y:scroll;">
  <div class="putbullet"></div>
</div>

问题是因为您在 .putbullet 元素上调用 animate(),而不是您附加的 .movethis。您还需要在 style 属性中设置样式,而不是 class - 或者更好的是,将它们放在外部样式表中。

最后,您还需要在 .putbullet 上设置 position: relative,以便附加元素在动画时包含在其中。试试这个:

$('.submmit').click(function() {
  $('<div class="movethis">' + $(".mytxt").val() + '</div>').appendTo('.putbullet').animate({
    "left": "0px"
  }, "slow");
});
.putbullet {
  position: relative;
}
.movethis {
  width: 40px;
  height: 50px;
  position: absolute;
  right: 0;
}
.areaofcontent {
  width: 68%;
  float: right;
  background-color: #eee;
  height: 400px;
  overflow-y: scroll;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <input type="text" name="txt" class="mytxt">
  <input type="button" class="submmit" value="Shoot">
</div>
<div class="areaofcontent">
  <div class="putbullet"></div>
</div>