Home>Article>Web Front-end> How to implement text scrolling using javascript

How to implement text scrolling using javascript

藏色散人
藏色散人 Original
2021-09-14 11:24:55 7160browse

How to implement text scrolling in javascript: 1. Create an HTML sample file; 2. Add script tags; 3. Implement vertical text through "$(".txtBox").addClass("txtBox_4");" Just arrange and scroll or set the text to arrange and scroll horizontally.

How to implement text scrolling using javascript

The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.

How to use javascript to achieve text scrolling?

js to achieve text scrolling effect

## Before the small The editor has already introduced some commonly used js animation effects to you. Here, I will introduce to you an animation effect that may not be commonly used. This animation effect is related to text, and, although not commonly used, almost everyone has seen it.

I believe that everyone uses Kugou Music or NetEase Cloud Music to search and listen to music. After a long time, you will definitely see the scrolling effect of lyrics on the lyrics page. This animation effect is the same as it, but it is somewhat different from these music lyrics effects, adding the effect of vertical text arrangement and scrolling.


Before the introduction, let us first understand some ways of vertical text arrangement:

writing-mode attribute

1. Value:

( 1) writing-mode: horizontal-tb | vertical-rl | vertical-lr | lr-tb | tb-rl
(2) Compatible writing: -webkit-writing-mode: horizontal-tb | vertical-rl | vertical -lr | lr-tb | tb-rl
(Note: lr-tb and tb-rl are not compatible in IE)
Default value: normal
Applicable to: except table-row-group, table - All elements except column-group, table-row, table-column
Inheritance: yes
Animation: no
Calculated value: specific value

2, writing-mode Introduction to the value

(1) horizontal-tb: horizontal top-down writing method. That is, left-right-top-bottom (similar to IE private value lr-tb)
(2) vertical-rl: vertical writing from right to left. That is, top-bottom-right-left (similar to IE private value tb-rl)
(3) vertical-lr: vertical writing from left to right. That is, top-bottom-left-right
(4) lr-tb: left-right, top-bottom. The content of the object flows horizontally from left to right, with the next row below the previous row. All glyphs are vertical and upward. This layout is used in Roman languages (IE)
(5) tb-rl: top-bottom, right-left. Content in an object flows vertically from top to bottom and from right to left. The next vertical row is to the left of the previous vertical row. Full-width characters are (6) pointed straight up, half-width characters such as Latin letters or katakana are rotated 90 degrees clockwise. This layout is commonly used in East Asian languages (IE)

html:

 

css:

body,html{ width:100%; height:100%; position: relative; background: #232226; overflow-y: auto; overflow-x: hidden;}.topBox,.topBoxTxt{ width:100%; height:auto; position: relative;}.topBoxTxt{ text-align: center; height:auto;; color:#fff; font-size: 0.36rem; padding-top:55px;}.txtBox{ width: 5.6rem; height: 5.2rem; margin:0 auto; overflow-y: scroll;} .txtBox>li{ opacity: 0.5; height:0.74rem;} .txtBox>li:first-child{ padding-top:60px; }.txtBox>li.hotColor{ opacity: 1;}.txtBox_4{ width:4.6rem; height: 7.5rem; overflow-x: auto; white-space: nowrap; display: block;}.txtBox_4>li{ writing-mode:tb-rl; writing-mode:vertical-rl; -webkit-writing-mode: vertical-rl; height: 0; line-height: 0.75rem; word-wrap:break-word;}.txtBox_4>li{ width:0.4rem; text-align: center; margin:0 0.1rem; display: inline-table; position: relative;}

index.js:

var freq=10;//滚动频率var fraction=9/10;// 水平文字高亮显示行在歌词显示区域中的固定位置百分比var frac=3/10;// 垂直文字高亮显示行在歌词显示区域中的固定位置百分比 var timer=true;//定时器var num=-1;//当前行下标var time;//滚动距离var eul = document.getElementById("txtBox");var lis=[ {"offset":3000, "text":"我总是轻描淡写告诉你我的愿望"}, {"offset":6000, "text":"也给你千言万语都说不尽的目光"}, {"offset":9000, "text":"这世界总有人在忙忙碌碌寻宝藏"}, {"offset":12000, "text":"却误了浮世骄阳也错过人间万象"}, {"offset":15000, "text":"古城里长桥上"}, {"offset":18000, "text":"人如海车成行"}, {"offset":21000, "text":"你笑得像光芒"}, {"offset":24000, "text":"蓦然把我照亮"}, {"offset":27000, "text":"风轻扬夏未央"}, {"offset":30000, "text":"林荫路单车响"}, {"offset":33000, "text":"原来所谓爱情"}, {"offset":36000, "text":"是这模样"}, {"offset":39000, "text":""} ]var count=lis.length%7+5;//文字水平排列滚动function hor(){ for (var i = 0; i (eul.scrollHeight-eul.clientHeight*(1-fraction))){ scrollTop=eul.scrollHeight-eul.clientHeight; }else{ scrollTop=ep.offsetTop=eul.clientHeight*fraction; } // 如用户拖动滚动条导致当前显示行超出显示区域范围,下一行直接定位到当前显示行 if (eul.scrollTop > (scrollTop + eul.clientHeight*fraction)|| (eul.scrollTop + eul.clientHeight*fraction) < scrollTop){ eul.scrollTop = scrollTop; }else { var step = Math.ceil(Math.abs(eul.scrollTop - scrollTop)/(time/freq)); scrollT(eul.scrollTop, scrollTop, step); } }function scrollT(crt, dst, step){ if(Math.abs(crt - dst) < step){ return; } if(crt < dst){ eul.scrollTop += step; crt += step; } else { eul.scrollTop -= step; crt -= step; } setTimeout(scrollT.bind(this, crt, dst, step), freq); };//文字垂直排列滚动function ver(){ console.log(eul) for (var i = 0; i  (eul.scrollWidth - eul.clientWidth*(1-frac))){ scrollLeft = eul.scrollWidth - eul.clientWidth; } else { scrollLeft = ep.offsetLeft - eul.clientWidth*frac; } // 如用户拖动滚动条导致当前显示行超出显示区域范围,下一行直接定位到当前显示行 if (eul.scrollLeft > (scrollLeft + eul.clientWidth*frac)|| (eul.scrollLeft + eul.clientWidth*frac) < scrollLeft){ eul.scrollLeft = scrollLeft; } else { var step = Math.ceil(Math.abs(eul.scrollLeft - scrollLeft)/(time/freq)); scrollL(eul.scrollLeft, scrollLeft, step); } }function scrollL(crt, dst, step){ if(Math.abs(crt - dst) < step){ return; } if(crt < dst){ eul.scrollLeft += step; crt += step; } else { eul.scrollLeft -= step; crt -= step; } setTimeout(scrollL.bind(this, crt, dst, step), freq); }function common(lineno,fn){ if (lineno ==0) { time = lis[lineno].offset; } else { time = lis[lineno].offset - lis[lineno-1].offset; } timer = setTimeout(fn.bind(this, lineno+1), time); num=lineno; //若滚动到最后一行,则从头开始,并把每一行文字均取消高亮 if(lineno==lis.length-1){ for(var i=0;i<(eul.children).length-1;i++){ eul.children[i].setAttribute("class", ""); } lineno=0; timer = setTimeout(fn.bind(this, lineno), time); } if (lineno > 0) { eul.children[lineno-1].setAttribute("class", ""); } var ep = eul.children[lineno]; ep.setAttribute("class", "hotColor"); }

Recommended learning: 《

javascript basic tutorial

The above is the detailed content of How to implement text scrolling using javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn