js+transition實(shí)現(xiàn)字幕無縫滾動(dòng)效果

重點(diǎn)是延時(shí)器的使用和transition的效果添加和取消

使用setInterval定時(shí)器在定時(shí)器函數(shù)里面判斷,當(dāng)top值小于arr的長度(因?yàn)槲覍?shí)現(xiàn)的是兩條數(shù)據(jù)在一行滾動(dòng))在條件范圍內(nèi)實(shí)現(xiàn)無縫操作,重點(diǎn)操作setTimeout延時(shí)器的使用,是為了當(dāng)操作字幕瞬間到達(dá)第一條后過渡屬性不會(huì)因此覆蓋top為0的過渡效果,而延時(shí)后加上transition的為了實(shí)現(xiàn)第一條字幕top過渡到第二條


if (top <= - num) {

  top = 0

    cg_autopaly.style.transition = ''

    cg_autopaly.style.top = top + 'px'

    setTimeout(() => {

      top = -20

      cg_autopaly.style.transition = 'all 0.5s'

      cg_autopaly.style.top = top + 'px'

    }, 100);

  } else {

    top -= 20

    cg_autopaly.style.transition = 'all 0.5s'

    cg_autopaly.style.top = top + 'px'

  }

js代碼


sjAutoPlay (arr) {

  let cg_autopaly = document.getElementsByClassName('cg_autopaly')[0]

  let top = 0

  let num = Math.ceil(arr.length/2) * 20

  // console.log(top)

  this.times = setInterval(function () {

    // console.log(top)

    if (top <= - num) {

      top = 0

      cg_autopaly.style.transition = ''

      cg_autopaly.style.top = top + 'px'

      setTimeout(() => {

        top = -20

        cg_autopaly.style.transition = 'all 0.5s'

        cg_autopaly.style.top = top + 'px'

      }, 100);

    } else {

      top -= 20

      cg_autopaly.style.transition = 'all 0.5s'

      cg_autopaly.style.top = top + 'px'

    }

  }, 2000)

},

HTML代碼


<div class="cg_new_allinfo">

  <div class="cg_autopaly">

    <p v-for="(item, index) in setAllMonthList" :key="index">

      <span v-for="(items, index) in item" :key="index">

        {{items.staff_name}}本月已完成任務(wù)任務(wù){(diào){items.month_count}}個(gè);

      </span>

    </p>

  </div>

</div>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容