最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

前端實(shí)現(xiàn)列表無縫自動(dòng)滾動(dòng)效果實(shí)例代碼(div列表+table表格)

 更新時(shí)間:2026年02月02日 09:07:09   作者:被妖怪抓走的師傅  
這篇文章主要介紹了前端實(shí)現(xiàn)列表無縫自動(dòng)滾動(dòng)效果(div列表+table表格)的兩種方法,一種是使用CSS和JavaScript結(jié)合,通過渲染兩次列表并使用@keyframes實(shí)現(xiàn)平滑滾動(dòng),另一種是針對(duì)表格滾動(dòng),通過復(fù)制表格內(nèi)容并使用CSS屬性實(shí)現(xiàn)無縫滾動(dòng),需要的朋友可以參考下

一、div列表滾動(dòng)

現(xiàn)在需要實(shí)現(xiàn)一個(gè)效果,內(nèi)容無限、平滑、無閃動(dòng)地向上滾動(dòng);當(dāng)鼠標(biāo)懸停到列表時(shí),滾動(dòng)暫停并高亮當(dāng)前項(xiàng);鼠標(biāo)移出繼續(xù)滾動(dòng)。這是在大屏項(xiàng)目中經(jīng)常見到的一種展示數(shù)據(jù)的方式,本文為具體的實(shí)現(xiàn)方式。

實(shí)現(xiàn)原理:

在使用了js控制之后發(fā)現(xiàn)有閃動(dòng)的現(xiàn)象,然后就換了種方式,我的想法是:把原始列表渲染兩次(A + A),用 CSS @keyframes 把整個(gè)雙倍內(nèi)容從 translateY(0) 平滑移動(dòng)到 translateY(-50%)(即移動(dòng)一份內(nèi)容的高度),動(dòng)畫無限循環(huán)。因?yàn)橄掳氩糠质巧习氩糠值膹?fù)制,循環(huán)在視覺上是連續(xù)的、無斷點(diǎn)的——沒有 JS 在中間做“重置”,因此無閃動(dòng)。話不多說直接上代碼
html:

<template>
  <div class="scroll-list" @mouseenter="pauseScroll" @mouseleave="resumeScroll">
    <div class="scroll-content" ref="scrollContent">
      <div
        v-for="item in typicalCaseList"
        :key="'a' + item.id"
        class="scroll-item"
      >
        <span>{{ item.text }}</span>
      </div>
      <!-- 復(fù)制一份 -->
      <div
        v-for="item in typicalCaseList"
        :key="'b' + item.id"
        class="scroll-item"
      >
        <span>{{ item.text }}</span>
      </div>
    </div>
  </div>
</template>

當(dāng)上半段滾出視窗時(shí),下半段的內(nèi)容正好銜接上,使得動(dòng)畫循環(huán)時(shí)不會(huì)出現(xiàn)斷裂。注意 key 的不同,避免 Vue 復(fù)用 DOM 導(dǎo)致復(fù)制失敗
js:

<script>
export default {
  data() {
    return {
      typicalCaseList: [
        // ... 你的數(shù)據(jù)
      ]
    };
  },
  mounted() {
    const list = this.$refs.scrollContent;
    // 根據(jù)列表長(zhǎng)度動(dòng)態(tài)設(shè)置動(dòng)畫時(shí)長(zhǎng)(可按需調(diào)整策略)
    const itemCount = this.typicalCaseList.length;
    const duration = Math.max(8, itemCount * 0.8); // 秒,示例:最少 8s
    list.style.setProperty('--duration', `${duration}s`);
  },
  methods: {
    pauseScroll() {
      this.$refs.scrollContent.style.animationPlayState = 'paused';
    },
    resumeScroll() {
      this.$refs.scrollContent.style.animationPlayState = 'running';
    }
  }
};
</script>

@mouseenter / @mouseleave 控制動(dòng)畫的播放狀態(tài),零延遲暫停/恢復(fù)

css:

<style scoped>
.scroll-list {
  height: 228px;
  margin: 0px 8px;
  overflow: hidden;
  position: relative;
}

.scroll-content {
  display: flex;
  flex-direction: column;
  animation: scroll-up var(--duration) linear infinite;
}

.scroll-item {
  height: 43px;
  font-size: 16px;
  color: #D0DEEE;
  padding: 0 10px;
  transition: all 0.3s;
  border-bottom: 1px dashed rgba(69,174,253,0.43);
  display: flex;
  flex-direction: row;
  align-items: center;
}

.scroll-item:hover {
  color: #03A7FA;
}

/* 真正無縫滾動(dòng)動(dòng)畫 */
@keyframes scroll-up {
  0% {
    transform: translateY(0);
  }
  100% {
    transform: translateY(-50%);
  }
}
</style>

translateY(-50%):把整個(gè)容器往上移 50%(即整份內(nèi)容的一半),正好把第一份內(nèi)容滾出,顯示第二份(與第一份相同),循環(huán)接著第一份。他之所以能解決閃爍的原因是因?yàn)閯?dòng)畫是連續(xù)的無限循環(huán),瀏覽器合成層平滑過渡,動(dòng)畫結(jié)束后 CSS 自動(dòng)從 0% 開始新一輪,但因?yàn)楫嬅鎯?nèi)容與起始狀態(tài)一致,用戶看不到跳變(沒有 JS 重置 scrollTop 的那一幀)

拓展:

還可以根據(jù)需求進(jìn)行一些部分的拓展,比如

  • 動(dòng)態(tài)速度或者是時(shí)長(zhǎng)更精確的計(jì)算
    通過計(jì)算單個(gè).scroll-item的高度,計(jì)算原始列表總高度來獲取更準(zhǔn)確的值
const itemHeight = this.$refs.scrollContent.querySelector('.scroll-item').offsetHeight;
const H = itemHeight * this.typicalCaseList.length;
const speed = 30; // px/s
const duration = H / speed;
this.$refs.scrollContent.style.setProperty('--duration', `${duration}s`);
  • 只在數(shù)據(jù)高度超出容器時(shí)啟用滾動(dòng)
    如果列表很短(內(nèi)容總高度 <= 你設(shè)定的總高度),不需要滾動(dòng)。判斷邏輯:
if (content.scrollHeight <= container.clientHeight) {
  // 不啟用動(dòng)畫:移除 animation
  content.style.animation = 'none';
} else {
  content.style.animation = `scroll-up var(--duration) linear infinite`;
}

二、table表格滾動(dòng)

table表格的滾動(dòng)主要是實(shí)現(xiàn)思路為復(fù)制一個(gè)表格內(nèi)容,讓其形成無縫滾動(dòng)的感覺,結(jié)合css實(shí)現(xiàn)效果

<div class="table-scroll-wrapper">
  <table class="table-box-right">
    <thead>
      <tr>
        <th>姓名</th>
        <th>城市</th>
        <th>年齡</th>
      </tr>
    </thead>
  </table>

  <div class="table-body-wrapper">
    <table class="table-box-right scroll-content">
      <!-- 可以通過判斷來實(shí)現(xiàn)達(dá)到多少條數(shù)據(jù)時(shí),觸發(fā)滾動(dòng)動(dòng)畫 -->
      <tbody :class="{ scrollable: tableData.length > 5 }">
        <!-- 克隆兩次實(shí)現(xiàn)無縫滾動(dòng) -->
        <tr v-for="item in tableData" :key="item.f_id">
          <td>{{ item.name }}</td>
          <td>{{ item.city }}</td>
          <td>{{ item.age }}</td>
        </tr>
        <tr v-for="item in tableData" :key="'clone-' + item.f_id">
          <td>{{ item.name }}</td>
          <td>{{ item.city }}</td>
          <td>{{ item.age }}</td>
        </tr>
      </tbody>
    </table>
  </div>
</div>

css部分實(shí)現(xiàn)滾動(dòng)

.table-scroll-wrapper {
    width: 100%;
    color: #fff;
    padding: 20px;
}

.table-box-right {
    width: 100%;
    border-collapse: collapse;
    table-layout: fixed;
}

.table-box-right th,
.table-box-right td {
    border: 1px solid #0058af;
    text-align: center;
    padding: 6px 8px;
    font-size: 14px;
}

/* 固定表頭 */
.table-box-right thead th {
    background: #103e5f;
    position: sticky;
    top: 0;
    z-index: 2;
    color: #37eef4;
}
.table-box-right td {
    background: #01204c;
}

/* 滾動(dòng)容器 */
.table-body-wrapper {
    max-height: 200px;
    overflow: hidden;
    position: relative;
}

.scroll-content tr {
    display: table; /* 保持表格樣式 */
    width: 100%;
    table-layout: fixed;
}

@keyframes scrollUp {
    0% { transform: translateY(0); }
    100% { transform: translateY(-50%); } /* 移動(dòng)一半,實(shí)現(xiàn)無縫滾動(dòng) */
}

.scroll-content tbody.scrollable {
    display: block; 
    animation: scrollUp 15s linear infinite; // 控制滾動(dòng)速度 數(shù)值越大滾動(dòng)越慢
}

.table-body-wrapper:hover .scroll-content tbody.scrollable {
    animation-play-state: paused;
}

/* 小于X條的 tbody 不加動(dòng)畫,默認(rèn)不滾動(dòng) */
.scroll-content tbody:not(.scrollable) {
    display: table-row-group; /* 保持正常表格布局 */
    animation: none;
}

上述是通過純css樣式控制當(dāng)數(shù)據(jù)大于你規(guī)定的條數(shù)時(shí),表格開始滾動(dòng),鼠標(biāo)劃入停止動(dòng)畫,離開繼續(xù)動(dòng)畫,控制css屬性這樣就能簡(jiǎn)單的實(shí)現(xiàn)無縫滾動(dòng)效果

總結(jié)

到此這篇關(guān)于前端實(shí)現(xiàn)列表無縫自動(dòng)滾動(dòng)效果(div列表+table表格)的文章就介紹到這了,更多相關(guān)前端列表無縫自動(dòng)滾動(dòng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • JavaScript自動(dòng)設(shè)置IFrame高度的小例子

    JavaScript自動(dòng)設(shè)置IFrame高度的小例子

    JavaScript自動(dòng)設(shè)置IFrame高度的小例子,需要的朋友可以參考一下
    2013-06-06
  • JavaScript禁止頁面操作的示例代碼

    JavaScript禁止頁面操作的示例代碼

    本篇文章是對(duì)JavaScript禁止頁面操作的示例代碼進(jìn)行了介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助
    2013-12-12
  • js頁面加載后執(zhí)行的幾種方式小結(jié)

    js頁面加載后執(zhí)行的幾種方式小結(jié)

    在實(shí)際應(yīng)用中往往需要在頁面加載完畢之后再去執(zhí)行相關(guān)的js代碼,之所以這么操作是有道理的,如果是操作dom元素,如果相關(guān)元素沒有加載完成,而去執(zhí)行js代碼,可能會(huì)導(dǎo)致錯(cuò)誤
    2020-01-01
  • Web?Components入門教程示例詳解

    Web?Components入門教程示例詳解

    這篇文章主要為大家介紹了Web?Components入門教程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05
  • js中使用使用原型(prototype)定義方法的好處詳解

    js中使用使用原型(prototype)定義方法的好處詳解

    下面小編就為大家?guī)硪黄猨s中使用使用原型(prototype)定義方法的好處詳解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-07-07
  • 最全的package.json解析

    最全的package.json解析

    從我們接觸前端開始,每個(gè)項(xiàng)目的根目錄下一般都會(huì)有一個(gè)package.json文件,這個(gè)文件定義了當(dāng)前項(xiàng)目所需要的各種模塊,以及項(xiàng)目的配置信息,本文就詳細(xì)的來介紹一下
    2021-07-07
  • JavaScript實(shí)現(xiàn)網(wǎng)頁動(dòng)態(tài)生成表格

    JavaScript實(shí)現(xiàn)網(wǎng)頁動(dòng)態(tài)生成表格

    這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)網(wǎng)頁動(dòng)態(tài)生成表格,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • setTimeout 函數(shù)在前端延遲搜索實(shí)現(xiàn)中的作用詳解

    setTimeout 函數(shù)在前端延遲搜索實(shí)現(xiàn)中的作用詳解

    這篇文章主要為大家介紹了setTimeout 函數(shù)在前端延遲搜索實(shí)現(xiàn)中的作用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • javascript獲取網(wǎng)頁寬高方法匯總

    javascript獲取網(wǎng)頁寬高方法匯總

    本文給大家匯總介紹了下javascript獲取網(wǎng)頁寬高的方法,以及各個(gè)瀏覽器下不同方法獲取到的值的對(duì)比,有需要的小伙伴可以參考下。
    2015-07-07
  • javascript加載導(dǎo)出3mf文件

    javascript加載導(dǎo)出3mf文件

    3MF是一種開放標(biāo)準(zhǔn)的文件格式,專門用于三維制造和打印,這篇文章主要介紹了如何使用JavaScript實(shí)現(xiàn)加載導(dǎo)出3mf文件,感興趣的可以了解下
    2024-11-11

最新評(píng)論

小金县| 诸暨市| 广昌县| 酉阳| 阆中市| 秦皇岛市| 突泉县| 隆安县| 长沙县| 洛浦县| 新源县| 涿鹿县| 凯里市| 鲜城| 侯马市| 乐昌市| 墨竹工卡县| 潼南县| 合川市| 普宁市| 彩票| 永寿县| 木里| 绥宁县| 孙吴县| 兴城市| 奉化市| 武平县| 安泽县| 武强县| 沁阳市| 铜山县| 沙洋县| 宝鸡市| 定安县| 定陶县| 彭阳县| 新巴尔虎右旗| 东方市| 宜君县| 赤壁市|