VUE 列表滾動

方法1:

    //無縫滾動
                const boxH = this.$refs.bottombtn.offsetHeight; // 父元素的高
                      const boxulH = this.$refs.boxul.offsetHeight; // 被滾動的高
                      const cha = boxulH - boxH;
                      let num = 0;
                      clearInterval(this.timers2);
                      this.timers2 = setInterval(() => {
                        if (num <= -cha) {
                          num = 0;
                        }
                        num -= 1;
                        this.$refs.boxul.style.marginTop = num + "px";
                      }, 20);

方法2:

//列表高度滾動
  fan() {
          const boxH = this.$refs.box.offsetHeight; // 父元素的高
          const boxulH = this.$refs.boxul.offsetHeight; // 被滾動的高
          const boxliH = this.$refs.boxli.offsetHeight + 9; // 每次滾動的高度 //9是      magrin值
          const cha = boxulH - boxH - 9; // 9是magrin值
          console.log(boxH, boxulH, boxliH, cha)
          let num = 0;
          clearInterval(this.timers2);
          this.timers2 = setInterval(() => {
            if (num <= -cha) { // 判斷如果滾動到頂部
              num = 0;
              this.$refs.boxul.style.transition = '0s all'; // 重置被滾動元素的高度
              this.$refs.boxul.style.marginTop = num + 'px';
              setTimeout(() => { // 重置高度后向上滾動一格否則中間間隔時間較長
                this.$refs.boxul.style.transition = '1s all';
                num -= boxliH;
                this.$refs.boxul.style.marginTop = num + 'px';
              }, 200)
            } else { // 正常滾動
              this.$refs.boxul.style.transition = '1s all';
              num -= boxliH;
              this.$refs.boxul.style.marginTop = num + 'px';
            }
          }, 2000);
        }
                //作者:gaochengxin
                //采納請點贊謝謝
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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