永远强大 發表於 2026-5-3 22:16:36

css3实现动态滚动播放列表功能

<p>需要自动滚动循环播放的列表,使用纯CSS实现基础循环功能</p>
<p style="text-align:center"><img alt="" height="217" src="https://img.jbzj.com/file_images/article/202409/2024091116584639.gif" width="552" /></p>
<div class="jb51code"><pre class="brush:css;">    .messages
      animation-name carousel
      animation-timing-function linear
      animation-iteration-count infinite
      .message-item
            cursor pointer
            margin-top 10px
      &amp;.stopPlay
            animation-play-state paused
            /**
            CSS3 animation-play-state 属性 值为paused时暂停动画,为running时继续动画
             */
    @keyframes carousel {
      0% {
            transform: translateY(0%)
      }
      100% {
            transform: translateY(-50%)
      }
    }</pre></div>
<p>完整代码</p>
<div class="jb51code"><pre class="brush:css;">&lt;template&gt;
    &lt;div&gt;
      &lt;div class="top-line"&gt;
            &lt;div class="box-title"&gt;
                &lt;span class="title"&gt;XXXX专题图&lt;/span&gt;
            &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="scroll-box"&gt;
            &lt;ul class="messages" @mouseover="stopAnim" @mouseout="runAnim" v-if="list.length"
                :style="{ animationDuration: animationDuration }" :class="{ stopPlay: animationPlayState }"&gt;
                &lt;li class="message-item" v-for="(item, index) in list" :key="index" @click="toDetail(item)"&gt;
                  &lt;div class="message-top"&gt;
                        &lt;span class="message-title"&gt;{{ item.name }}&lt;/span&gt;
                        &lt;span class="message-time"&gt;{{ item.startDate }}&lt;/span&gt;
                  &lt;/div&gt;
                  &lt;p class="message-content"&gt;{{ item.content }}&lt;/p&gt;
                &lt;/li&gt;
            &lt;/ul&gt;
            &lt;div class="none" v-else&gt;
                暂无内容
            &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/template&gt;</pre></div>
<div class="jb51code"><pre class="brush:css;">&lt;script&gt;
import test11List from '@/assets/test11List'
export default {
    data() {
      return {
            animationDuration: '',
            animationPlayState: false,
            list: test11List.list,
            category: ''
      };
    },
    mounted() {
      this.getData()
    },
    methods: {
      getData() {
            let data = this.list
            if (data.length &lt;= 2) {
                this.animationPlayState = true
                this.animationDuration = 2 + 's'//动画持续时间
            } else {
                // 跑马灯动画
                this.animationDuration = data.length * 2 + 's'
                this.list = this.list.concat(this.list)
            }
      },
      stopAnim() {//鼠标移入暂停动画
            this.animationPlayState = true
      },
      runAnim() {//鼠标移除继续动画
            if (this.list.length &gt; 2) {
                this.animationPlayState = false
            }
      }
    },
};
&lt;/script&gt;</pre></div>
<div class="jb51code"><pre class="brush:css;">&lt;style lang="stylus" scoped&gt;
    *{
      margin: 0
      padding: 0
    }
    ul{
      list-style: none
    }
    .scroll-box{
      width 100%
      height 800px
      overflow hidden
      border 2px solid red
    }
    .box-title
      line-height 34px
      font-size 16px
      background: blue
      color: #fff
      opacity 1
    .messages
      animation-name carousel
      animation-timing-function linear
      animation-iteration-count infinite
      .message-item
            cursor pointer
            margin-top 10px
      &amp;.stopPlay
            animation-play-state paused
            /**
            CSS3 animation-play-state 属性 值为paused时暂停动画,为running时继续动画
             */
    .none
      text-align center
      line-height 537px
      color #fff
    @keyframes carousel {
      0% {
            transform: translateY(0%)
      }
      100% {
            transform: translateY(-50%)
      }
    }
&lt;/style&gt;</pre></div>
頁: [1]
查看完整版本: css3实现动态滚动播放列表功能