vue 增加頁面頂部橫屏滾動元件
阿新 • • 發佈:2021-12-15
元件封裝好,直接呼叫即可。
程式碼如下:
/* * @desc: 滾動元件 */ <template> <div class="note-block" ref="noteBlocking" @mouseover="handStop" @mouseleave="handStart" v-show="noteContent" v-popover:notePopover> <div class="noteScrolling-block" ref="noteScrollingBlock" @mouseover="handStop" @mouseleave="handStart"> {{noteContent}}</div> <el-popover ref="notePopover" placement="top" title="" trigger="hover" :content="noteContent"></el-popover> </div> </template> <script> export default { name: "scroll-nav", data() { return { noteContent:'內容內容內容', } }, mounted() {this.startScroll() }, methods: { startScroll() { const that = this this.adWidth = this.$refs.noteBlocking.clientWidth this.con1 = this.$refs.noteScrollingBlock this.conWidth = this.$refs.noteScrollingBlock.clientWidth this.con1.style.right =`0px` this.count = 0 document.addEventListener("visibilitychange", function() { if (document.hidden === true) { that.handStop() } else { that.handStart() } }); this.scroll() }, handStop(){ this.scroll(true) }, handStart(){ this.scroll(false) }, scroll(flag){ clearTimeout(this.timeOutObj) if(flag) { return } this.timeOutObj = setTimeout(() => { this.count += 15 this.con1.style.right = `${this.count}px` if(this.adWidth + this.conWidth+ 20 < this.count){ this.con1.style.opacity = 0 this.count = 0 this.con1.style.right =`-0px` setTimeout(() => { this.con1.style.opacity = 1 clearTimeout(this.timeOutObj) this.scroll(flag) },2000) }else{ this.scroll(flag) } }, 500) } } } </script> <style lang="scss" scoped> .note-block{ position: absolute; left: 0; right: 0; overflow: hidden; cursor: pointer; width: 100%; height: 30px; background: #FFF0DF; font-family: SourceHanSansCN-Regular; font-size: 14px; color: #DA3438; letter-spacing: -0.35px; .noteScrolling-block{ height: 30px; line-height: 30px; transition: right 1s linear; display: inline-block; position: absolute; right: 0; white-space: nowrap; transform: translate(100%,0); cursor: pointer; } } </style>
樣式如下: