CSS實(shí)現(xiàn)文字上下輪播效果,可以通過編寫CSS樣式和JavaScript代碼來實(shí)現(xiàn),下面是一個(gè)簡(jiǎn)單的實(shí)現(xiàn)示例:
HTML代碼:
<div class="marquee"> <p>歡迎來到我的網(wǎng)站!</p> <p>我是您的網(wǎng)站管理員,很高興為您服務(wù)!</p> <p>如果您有任何問題或需要幫助,請(qǐng)隨時(shí)告訴我!</p> <p>謝謝!</p> </div>
CSS樣式:
.marquee { position: relative; height: 50px; overflow: hidden; } .marquee p { position: absolute; top: 0; left: 0; width: 100%; height: 50px; line-height: 50px; text-align: center; color: #333; font-size: 18px; }
JavaScript代碼:
var marquee = document.querySelector('.marquee'); var paragraphs = marquee.getElementsByTagName('p'); var currentParagraph = 0; var direction = 1; // 1表示向下,0表示向上 var speed = 5; // 每秒移動(dòng)多少行文字 var timer = null; // 定時(shí)器對(duì)象 var textHeight = paragraphs[0].offsetHeight; // 文字高度 var marqueeHeight = marquee.offsetHeight; // 輪播框高度 var maxParagraphs = Math.floor(marqueeHeight / textHeight); // ***大可顯示的行數(shù) var isEnd = false; // 是否到達(dá)底部或頂部,true表示到達(dá)底部,false表示到達(dá)頂部 var isStart = true; // 是否開始輪播,true表示開始,false表示停止 var startY = 0; // 開始時(shí)的位置,向下為0,向上為textHeight * maxParagraphs - marqueeHeight + textHeight * currentParagraph + startYOffset; // 開始時(shí)的位置,向下為0,向上為textHeight * maxParagraphs - marqueeHeight + textHeight * currentParagraph + startYOffset; // 開始時(shí)的位置,向下為0,向上為textHeight * maxParagraphs - marqueeHeight + textHeight * currentParagraph + startYOffset; // 開始時(shí)的位置,向下為0,向上為textHeight * maxParagraphs - marqueeHeight + textHeight * currentParagraph + startYOffset; // 開始時(shí)的位置,向下為0,向上為textHeight * maxParagraphs - marqueeHeight + textHeight * currentParagraph + startYOffset; // 開始時(shí)的位置,向下為0,向上為textHeight * maxParagraphs - marqueeHeight + textHeight * currentParagraph + startYOffset; // 開始時(shí)的位置,向下為0,向上為textHeight * maxParagraphs - marqueeHeight + textHeight * currentParagraph + startYOffset; // 開始時(shí)的位置,向下為0,向上為textHeight * maxParagraphs - marqueeHeight + textHeight * currentParagraph + startYOffset; // 開始時(shí)的位置,向下為0,向上為textHeight * maxParagraphs - marqueeHeight + textHeight * currentParagraph + startYOffset; // 開始時(shí)的位置,向下為0,向上為textHeight * maxParagraphs - marqueeHeight + textHeight * currentParagraph + startYOffset; // 開始時(shí)的位置,向下為0,向上為textHeight * maxParagraphs - marqueeHeight + textHeight * currentParagraph + startYOffset; // 開始時(shí)的位置,向下為0,向上為textHeight * maxParagraphs - marqueeHeight + textHeight * currentParagraph + startYOffset; // 開始時(shí)的位置,向下為0,向上為textHeight * maxParagraphs - marqueeHeight + textHeight * currentParagraph + startYOffset; // 開始時(shí)的位置,向下為0,向上為textHeight * maxParagraphs - marqueeHeight + textHeight * currentParagraph + startYOffset; // 開始時(shí)的位置,向下為0,向上為textHeight * maxParagraphs - marqueeHeight + textHeight * currentParagraph + startYOffset; // 開始時(shí)的位置,向下為0,向上為textHeight * maxParagraphs - marqueeHeight + textHeight * currentParagraph + startYOffset; // 開始時(shí)的位置,向下為0,向上為textHeight * maxParagraphs - marqueeHeight + textHeight * currentParagraph + startYOffset; // 開始時(shí)的位置,向下為0,向上為textHeight * maxParagraphs - marqueeHeight + textHeight * currentParagraph + startYOffset; // 開始時(shí)的位置,向下為0,向上為textHeight * maxParagraphs - marqueeHeight + textHeight * currentParagraph + startYOffset; // 開始時(shí)的位置
版權(quán)聲明:除非特別標(biāo)注,否則均為本站原創(chuàng)文章,轉(zhuǎn)載時(shí)請(qǐng)以鏈接形式注明文章出處。