vue实现轮播话术效果
需求需要一个这样的效果
轮播话术,首先想到的是操作dom方法
mounted() {
//获取div元素,定位轮播位置 const parentDiv = this.$refs.shufflingText for (let i = 0; i < 100; i++) { this.shuffling(parentDiv, '恭喜您获得了XX奖品' + i, i) } }, shuffling(parentDiv, text, i) { let left = i * 200 + 400 let div = document.createElement('div') div.style.position = 'absolute' div.style.width = '200px' div.style.height = '30px' // div.style.backgroundImage = 'url("/static/img/tooltip.png")' div.innerHTML = text div.style.top = this.stochastic() + 'px' // console.log(div.style.top) div.style.left = left + 'px' div.classList.add('shufText') parentDiv.appendChild(div) // debugger let timer = setInterval(() => { left-- div.style.left = left + 'px' if (left < -200) { parentDiv.removeChild(div) //当弹幕移动出了视频时,销毁该元素 clearInterval(timer) //关闭计时器 } }, 20) this.timers.push(timer) },
该方法实在太low,后面直接使用vue数据驱动的原理
<!-- 轮播图话术 -->
<div class="shufflingText" ref="shufflingText">
<div v-for="(item,index) in shuffList" :key="index" :class="['commonText',index%2===1?'shufText':'shufText2']">{{item.message}}</div>
</div>
原理是遍历数组,每隔一段时间push一条数据到shuffList
startShuffList() { this.timer2 = setTimeout(() => { if (this.index < this.shuffList2.length) { this.shuffList.push(this.shuffList2[this.index]) this.index++ } else { this.index = 0 } this.startShuffList() }, 2000) },
此时实现轮播效果的是css
.shufflingText { position: absolute; z-index: 100; top: 2.38rem; width: 100vw; height: 0.5rem; .commonText { position: absolute; font-size: 0.12rem; padding: 0.03rem 0.1rem; color: #fff; // height: 0.06rem; // line-height: 0.6rem; background: rgba(0, 0, 0, 0.4); border-radius: 0.3rem; padding-left: 0.05rem; animation: barrage 10s linear 0s; animation-fill-mode: forwards; word-break: keep-all; /* 不换行 */ white-space: nowrap; /* 不换行 */ overflow: hidden; /* 内容超出宽度时隐藏超出部分的内容 */ image { width: 48rpx; height: 48rpx; border-radius: 50%; margin-right: 10rpx; margin-top: 6rpx; } text { white-space: nowrap; } } .shufText { top: 0; } .shufText2 { top: 0.3rem; } } @keyframes barrage { from { left: 100%; transform: translateX(0); } to { left: -100%; transform: translateX(-100%); display: none; } }
end
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通