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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何重置 JQueryUI 效果動畫?

如何重置 JQueryUI 效果動畫?

慕容森 2022-12-18 17:59:00
我有兩個不同的計時器條,每個條都在 5 秒內結束。但是當我嘗試停止并重置第一個計時器時,它不會重置,只會從停止的地方繼續。期望的行為: Timer1 啟動 用戶按下按鍵 Timer1 中途停止 Timer2 啟動 Timer2 到期 Timer1 重置為 100% 并啟動這是 timer1 啟動的方式:function StartTimer1(){        $("#timer-bar").show().width("100%");    $("#timer-bar").hide({            effect: "blind",            easing: "linear",            duration: 5000,            direction: "left",            complete: function () {                //do stuff            }        });}然后當有人按下一個鍵時,我跑 //Stop timer1     $("#timer-bar").stop(true, false);//Start timer2     $("#answer-timer-bar").hide({        effect: "blind",        easing: "linear",        duration: 5000,        direction: "left",        complete: function () {            //do stuff        }    });然后在 timer2 到期后,我按下了另一個再次調用 StartTimer1() 的鍵。即使我在開始動畫之前將寬度重置為 100%,它也會從它第一次停止的地方繼續。我閱讀了有關隊列的信息并認為這是我的答案,但我嘗試為每個計時器選項添加唯一的隊列名稱,但每當我這樣做時,計時器由于某種原因永遠不會啟動。當我將隊列設置為 true 時也是如此。我在調用 Stop() 時嘗試了 clearQueue 和 jumpToEnd 參數的不同選項,但似乎無法獲得我想要的行為。任何幫助,將不勝感激。謝謝。JSFiddle:https ://jsfiddle.net/58rzg6w0/7
查看完整描述

4 回答

?
慕容3067478

TA貢獻1773條經驗 獲得超3個贊

隱藏和顯示方法上的動畫?

根據我的研究和這篇 stackoverflow 帖子,如果我們使用and$(selector).animate()而不是, 我們將對動畫有更多的控制。$(selector).hide()$(selector).show()


將容器元素用于動畫

我已經分離了 css 樣式并為動畫本身添加了疊加層以防止容器折疊(如果是,元素將沒有高度display: none)。這可能有用或沒有用,這取決于您的目標。您仍然可以隱藏它們,因此使用此方法沒有任何缺點。將動畫與容器分開可能很有用。


恢復動畫的時間問題

如果其中一個計時器恰好在動畫期間停止并恢復,則動畫仍將具有相同的持續時間“更短的距離”,從而導致動畫速度變慢。


但是,如果我們重置 的動畫timer1,就像您在示例中所做的那樣,那么這不是問題,否則,我們必須根據動畫進度和總動畫長度調整動畫持續時間(您可以在下面的腳本)。


只是為了好玩 - 顯示時間

通常情況下,計時器會顯示時間,因此我將動畫的持續時間添加到疊加層上。這只是您可能想要添加的簡單添加(加上樣式)。動畫的持續時間可以通過數據持續時間(以毫秒為單位)在計時器元素上設置。


$('document').ready(function() {

  ResetAll();

});


function TimeLeft(id, initialTime) {

  const $timer = $('#' + id);

  const $overlay = $timer.find('.overlay');

  const percentageToGo = (parseInt($overlay.css('width')) / parseInt($timer.css('width')));

  return percentageToGo * initialTime;

}


function StartTimer(id, onComplete = null) {

  const $timer = $('#' + id);

  const $overlay = $timer.find('.overlay');

  const duration = $timer.data('duration');

  const time = TimeLeft(id, duration);

  $overlay.animate({

    width: '0%'

  }, {

    duration: time,

    easing: 'linear',

    step: function() {

      $overlay.html(Math.round(TimeLeft(id, duration)) / 1000);

    },

    complete: function() {

      // $timer.css('display', 'none'); // remove all comments to hide the timer element completly

      $overlay.html('');

      if (onComplete && typeof onComplete === 'function') {

        onComplete();

      }

    }

  });

}


function StopTimer(id) {

  $('#' + id + ' .overlay').stop(true, false);

}


function ResetTimer(id) {

  const $timer = $('#' + id);

  const $overlay = $timer.find('.overlay');

  $overlay.stop(true).css('width', '100%');

  // $timer.css('display', 'block'); // remove all comments to hide the timer element completly

  $overlay.html(Math.round(TimeLeft(id, $timer.data('duration'))) / 1000);

}


function StopTimer1AndStartTimer2() {

  ResetTimer('timer1');

  ResetTimer('timer2');

  StartTimer('timer2', function() {

    // $('#timer2').css('display', 'none'); // remove all comments to hide the timer element completly

    StartTimer('timer1');

  });

}


function ResetAll() {

  ResetTimer('timer1');

  ResetTimer('timer2');

  // $('.timer').css('display', 'block'); // remove all comments to hide the timer element completly

}

.timer {

  position: relative;

  height: 30px;

  width: 100%;

  z-index: 1;

}


.overlay {

  color: white;

  position: absolute;

  width: 100%;

  top: 0;

  left: 0;

  bottom: 0;

  z-index: -1;

}


#timer1 .overlay {

  background-color: red;

}


#timer2 .overlay {

  background-color: blue;

}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.1/jquery.min.js"></script>

<div id="timer1" data-duration="5000" class="timer">

  <div class="overlay"></div>

</div>

<div id="timer2" data-duration="3000" class="timer">

  <div class="overlay"></div>

</div>

<p>This first button is probably what you want, the rest is extra:</p>

<button onclick="StopTimer1AndStartTimer2();">Stop Timer1 - Reset Timer1 - Start Timer2 - on complete Start Timer2</button>

<button onclick="StartTimer('timer1');">Start/Resume Timer1</button>

<button onclick="StartTimer('timer2');">Start/Resume Timer2</button>

<button onclick="StartTimer('timer1'); StartTimer('timer2');">Start/Resume All</button>

<button onclick="StopTimer('timer1');">Stop Timer1</button>

<button onclick="StopTimer('timer2');">Stop Timer2</button>

<button onclick="StopTimer('timer1'); StopTimer('timer2');">Stop All</button>

<button onclick="ResetTimer('timer1');">Reset Timer1</button>

<button onclick="ResetTimer('timer2');">Reset Timer2</button>

<button onclick="ResetAll();">Reset All</button>


查看完整回答
反對 回復 2022-12-18
?
料青山看我應如是

TA貢獻1772條經驗 獲得超8個贊

您可以在開頭.clone() timer1 并在 timer2 效果結束時使用.replaceWith()將其重置為初始值:

var initialValue = null;

function StartTimer1() {

    if (initialValue == null) {

        initialValue = $("#timer1").clone();

    } else {

        $("#timer1").replaceWith(initialValue);

    }

    $("#timer1").show().width("100%");


    $("#timer1").hide({

        effect: "blind",

        easing: "linear",

        duration: 5000,

        direction: "left",

        complete: function () {

        }

    });

}


function StopTimer1(){

    $("#timer1").stop(true, false);


    $("#timer2").hide({

        effect: "blind",

        easing: "linear",

        duration: 2000,

        direction: "left",

        complete: function () {

            StartTimer1();

        }

    });

}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">

<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>


<div id="timer1" style="height:30px; width:100%; background-color:red"></div>

<div id="timer2" style="height:30px; width:100%; background-color:blue"></div>

Click First button, then click second, then click first again

<button onclick="StartTimer1();">Start Timer1</button><button onclick="StopTimer1();">Stop Timer1 and Start Timer2</button>



查看完整回答
反對 回復 2022-12-18
?
三國紛爭

TA貢獻1804條經驗 獲得超7個贊

用于.stop(true, true)去除占位符 ( .stop( [clearQueue ] [, jumpToEnd ] ))。jumpToEnd將隱藏它,所以再次顯示它,并在 timer2 結束后回調它:


 function StartTimer1() {

  $("#timer1").hide({

    effect: "blind",

    easing: "linear",

    duration: 5000,

    direction: "left",

    complete: function() {}

  });

}


function StopTimer1() {

  $("#timer1").stop(true, true).show();


  $("#timer2").hide({

    effect: "blind",

    easing: "linear",

    duration: 2000,

    direction: "left",

    complete: function() {

      StartTimer1()

    }

  });

}

按下按鈕后立即重置紅色條的示例:

function StartTimer1() {

  $("#timer1").hide({

    effect: "blind",

    easing: "linear",

    duration: 5000,

    direction: "left",

    complete: function() {}

  });

}


function StopTimer1() {

  $("#timer1").stop(true, true).show();


  $("#timer2").hide({

    effect: "blind",

    easing: "linear",

    duration: 2000,

    direction: "left",

    complete: function() {

      StartTimer1()

    }

  });

}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<div id="timer1" style="height:30px; width:100%; background-color:red">


</div>



<div id="timer2" style="height:30px; width:100%; background-color:blue">


</div>


Click First button, then click second, then click first again

<button onclick="StartTimer1();">

Start Timer1

</button>



<button onclick="StopTimer1();">

Stop Timer1 and Start Timer2

</button>

如果你想在藍色完成動畫后重置紅色條:


藍色條完成后使用.stop(true, false);并重置樣式 ( )。.attr("style"


function StartTimer1() {

  $("#timer1").hide({

    effect: "blind",

    easing: "linear",

    duration: 5000,

    direction: "left",

    complete: function() {}

  });

}


function StopTimer1() {

  $("#timer1").stop(true, false);


  $("#timer2").hide({

    effect: "blind",

    easing: "linear",

    duration: 2000,

    direction: "left",

    complete: function() {

      $("#timer1").attr("style", "height:30px; width:100%; background-color:red");

      StartTimer1();

    }

  });

}

function StartTimer1() {

  $("#timer1").hide({

    effect: "blind",

    easing: "linear",

    duration: 5000,

    direction: "left",

    complete: function() {}

  });

}


function StopTimer1() {

  $("#timer1").stop(true, false);


  $("#timer2").hide({

    effect: "blind",

    easing: "linear",

    duration: 2000,

    direction: "left",

    complete: function() {

      $("#timer1").attr("style", "height:30px; width:100%; background-color:red");

      StartTimer1();

    }

  });

}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<div id="timer1" style="height:30px; width:100%; background-color:red">


</div>



<div id="timer2" style="height:30px; width:100%; background-color:blue">


</div>


Click First button, then click second, then click first again

<button onclick="StartTimer1();">

Start Timer1

</button>



<button onclick="StopTimer1();">

Stop Timer1 and Start Timer2

</button>


查看完整回答
反對 回復 2022-12-18
?
慕桂英3389331

TA貢獻2036條經驗 獲得超8個贊

重置動畫:


// Remove style

$("#timer-bar").removeAttr('style');

// Set initial width

$("#timer-bar").show().width("100%");

要么:


$('#timer-bar').stop(true).css('width', '100%');



查看完整回答
反對 回復 2022-12-18
  • 4 回答
  • 0 關注
  • 155 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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