2017-04-19 76 views
1

我想输入一条消息,然后当点击提交按钮调用'shoot'时,它将显示在屏幕的右侧。不过,我希望文字从右至左进行动画/滑动。 我该怎么做?追加div元素,然后使用JQUERY从右到左动画

$('.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>

+0

有一个错字:'类='宽度:'。应该是'style ='width:'[JSFiddle](https://jsfiddle.net/3wq54apd/) – Rajesh

+0

在文本框内滑动? – Sharmila

回答

2

的问题是因为你的.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>

+0

这是有道理的,它的工作原理!非常感谢! –