1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| <template> <div> <div class="notice" ref="container"> <p ref="notice">{{notice}}</p> </div> </div> </template> <script> import { throttle } from '@/utils' export default { data () { return { defaultTxt: '测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试。。。。。测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试。。。。。测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试。。。。。测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试。。。。。测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试测试环境用于测试。。。。。', notice: '' } }, mounted () { this.getStyle() window.addEventListener('resize', throttle(this.getStyle, 1000, true)) }, methods: { getStyle () { this.notice = this.defaultTxt this.$nextTick(() => { let containerW = this.$refs.container.clientWidth let noticeW = this.$refs.notice.clientWidth console.log(containerW, noticeW) if (noticeW > containerW) { this.notice = this.defaultTxt + this.defaultTxt this.$refs.notice.className = 'run' this.$refs.notice.style.animationDuration = noticeW / 100 + 's' // 匀速 } else { this.notice = this.defaultTxt this.$refs.notice.className = '' this.$refs.notice.style.animationDuration = 0 } }) } }, destroyed () { window.onresize = null } } </script> <style lang="scss" scoped> .notice { position: relative; width: 100%; overflow: hidden; height: 30px; line-height: 30px; background: rgb(247, 225, 225); color: #666; @keyframes run { from { transform: translateX(0); } to { transform: translateX(-50%); } } p { position: absolute; top: 0; left: 0; white-space: nowrap; } .run { animation: 20s run linear infinite; } } </style>
|