亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

為什么一定要用math.ceil向上取整才正確?


<!DOCTYPE html>
<html lang="en">
<head>
? ?<meta charset="UTF-8">
? ?<title>緩沖動畫效果</title>
? ?<style type="text/css">
#div1{
? ? ? ? ? ?width: 200px;
? ? ? ? ? ?height: 200px;
? ? ? ? ? ?position: absolute;
? ? ? ? ? ?top: 0px;
? ? ? ? ? ?left: -200px;
? ? ? ? ? ?background-color: #b10d1c;
? ? ? ?}
? ? ? ?#share{
? ? ? ? ? ?width: 30px;
? ? ? ? ? ?height: 50px;
? ? ? ? ? ?position: absolute;
? ? ? ? ? ?left: 200px;
? ? ? ? ? ?top: 75px;
? ? ? ? ? ?background-color: #3c6dcb;
? ? ? ? ? ?color: #ffffff;
? ? ? ? ? ?text-align: center;
? ? ? ? ? ?cursor: pointer;
? ? ? ?}
? ?</style>
? ?<script type="text/javascript">
window.onload=function () {
var odiv1=document.getElementById('div1'),
share=document.getElementById('share');
odiv1.onmouseover=function(){
startMove(10,0);
? ? ? ? ? ?}//最好使用匿名函數來調用函數
// ? ? ? ? ? ?odiv1.onmouseout="startMove(10,0)";
odiv1.onmouseout=function(){
startMove(10,-200);
? ? ? ? ? ?}
var timer=null
? ? ? ? ? ?function startMove(speed,itarget) {
clearInterval(timer);
timer=setInterval(function () {
speed=(itarget-odiv1.offsetLeft)/10;
if(odiv1.offsetLeft==itarget){
clearInterval(timer)
? ? ? ? ? ? ? ? ? ?}
else {
if(odiv1.offsetLeft>itarget){
speed=(odiv1.offsetLeft-itarget)/10;
// ? ? ? ? ? ? ? ? ? ? ? ? ? ?speed=speed>0?Math.ceil(speed):Math.floor(speed);
speed=Math.ceil(speed);
odiv1.style.left=odiv1.offsetLeft+(-speed)+'px';
? ? ? ? ? ? ? ? ? ? ? ?}
else{
speed=(itarget-odiv1.offsetLeft)/10;
// ? ? ? ? ? ? ? ? ? ? ? ? ? ?speed=speed>0?Math.ceil(speed):Math.floor(speed);
speed=Math.ceil(speed);
odiv1.style.left=odiv1.offsetLeft+speed+'px';
? ? ? ? ? ? ? ? ? ? ? ?}


? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?},30);
? ? ? ? ? ?}
? ? ? ?}

</script>
</head>
<body>
<div id="div1"><span id="share">分享</span> </div>
</body>
</html>



正在回答

1 回答

如果(目標值-當前值)/10 ?不能整除的話。運動到最后,是1~9之間的數字來除以10,結果是個小于1的小數,如果向下取整,那就是0,speed=0,意味著不再發生變化。也就是說,如果用Math.floor(speed),最后不到10個像素的時候,就不會動了。

0 回復 有任何疑惑可以回復我~
#1

Zohar 提問者

非常感謝!
2016-06-01 回復 有任何疑惑可以回復我~
#2

小西瓜籽

執行一次后 offsetLeft=-190 那 190/20=9.5 也不是整數 不加下面取整代碼時,程序一直執行到offsetLeft=-10時才不能執行是為什么,求解答
2016-06-23 回復 有任何疑惑可以回復我~
#3

飛舞的墨 回復 小西瓜籽

得到小數之后,小數再除小數,后面的數簡直是沒法說了。反正我用代碼沒找出一點規律,反而內存溢出了。
2016-06-24 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

為什么一定要用math.ceil向上取整才正確?

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號