[英]Append div element and then animate from right to left using JQUERY
I want to type a message and then when click on submit button call 'shoot' it will be display at the right side of the screen. 我想输入一条消息,然后在单击“提交”按钮时单击“拍摄”,它将显示在屏幕的右侧。 However, I want the text to animate/slide in from right to left.
但是,我希望文本从右向左滑动/滑动。 How do i do it?
我该怎么做?
$('.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>
The issue is because you're calling animate()
on the .putbullet
element, not the .movethis
which you append. 问题是因为您是在
.putbullet
元素上调用animate()
,而不是在附加的.movethis
上调用。 You also need to set the styles in the style
attribute, not the class
- or better yet, put them in an external stylesheet. 您还需要在
style
属性中设置样式,而不是在class
设置样式-或者更好的是,将它们放在外部样式表中。
Finally, you'll also need to set position: relative
on the .putbullet
so that the appended elements are contained within it when they animate. 最后,您还需要在
.putbullet
上设置position: relative
,以便在动画时将附加元素包含在其中。 Try this: 尝试这个:
$('.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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.