vue3中饼状图自动轮播高亮展示

最终效果

环状图自动轮播

1.给echarts设置id

2.轮播函数

<script setup>
let timers = ref(null)
const pos = ref(0)
const categoryLength = ref()

function handleAutoLoop() {
  nextTick(() => {
    // 图表的初始化
    const myChart = echarts.init(document.getElementById('manyLineCharts'))
    const initTimer = () => {
      if (timers) {
        timers = null
        clearInterval(timers)
      }
      timers = setInterval(() => {
        myChart.dispatchAction({
          type: 'downplay',
          seriesIndex: 0
        })

        pos.value++
        if (pos.value > categoryLength.value) {
          pos.value = 0
        }
        myChart.dispatchAction({
          type: 'highlight',
          seriesIndex: 0,
          dataIndex: pos.value
        })

        myChart.dispatchAction({
          type: 'showTip',
          seriesIndex: 0,
          dataIndex: pos.value
        })
      }, 2000)
    }
    initTimer()
    const stopShowTip = param => {
      clearInterval(timers)
      //取消当前轮播的高亮效果
      myChart.dispatchAction({
        type: 'downplay',
        seriesIndex: 0,
        dataIndex: pos.value
      })
    }
    // 鼠标移入
    myChart.on('mouseover', param => {
      stopShowTip(param)
    })
    // 鼠标移出
    myChart.on('mouseout', () => {
      initTimer()
    })
    // 鼠标在目标内不断移动
    myChart.on('mousemove', param => {
      stopShowTip(param)
    })
    // 鼠标移出整个图标时触发
    myChart.on('globalout', () => {
      initTimer()
    })
  })
}

onMounted(() => {
  handleAutoLoop()
})
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
A: 可以借助Vue3的transition组件和transform属性来实现片自动左右轮。具体实现步骤如下: 1. 在模板使用transition包裹片列表,并设置transition的name属性。 2. 使用v-for指令遍历片列表,并使用transform属性将片按照水平方向平移。 3. 定义一个计算属性来获取当前应该显示的片索引。 4. 定义一个定时器,每隔一段时间更新计算属性的索引值,从而实现片的自动左右轮。 5. 在transition组件添加动画效果,使片轮时能呈现渐变动画效果。 具体代码实现示例如下: ```html <template> <div class="carousel"> <transition name="slide"> <ul> <li v-for="(item, index) in images" :key="index" :style="`transform: translateX(-${currentIndex * 100}%)`"> <img :src="item.src" alt=""> </li> </ul> </transition> </div> </template> <script> export default { data() { return { images: [ {src: 'http://example.com/image1.jpg'}, {src: 'http://example.com/image2.jpg'}, {src: 'http://example.com/image3.jpg'} ], currentIndex: 0, timer: null } }, computed: { // 获取当前应该显示的片索引 currentImageIndex() { return this.currentIndex % this.images.length } }, mounted() { // 启动定时器,每隔3秒更新片索引 this.timer = setInterval(() => { this.currentIndex++ }, 3000) }, destroyed() { clearInterval(this.timer) } } </script> <style> .carousel { width: 600px; overflow: hidden; } ul { display: flex; list-style: none; margin: 0; padding: 0; transition: transform 0.5s; } img { width: 600px; height: 300px; object-fit: cover; } .slide-enter-active, .slide-leave-active { transition: transform 0.5s ease-in-out; } .slide-enter, .slide-leave-to { transform: translateX(100%); } </style> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值