2017-04-19 58 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

這是有道理的,它的工作原理!非常感謝! –