JS JavaScript 实现文字上下滚动效果


  • Lorem ipsum dolor sit amet.
  • Amet hic ipsum offices vitae!
  • Debits quart qua sapient venial.
  • Ilium nobs object oddity sapient?
  • Et laudanum nemo official omnibus.
  • Lorem ipsum dolor sit amet.
  • Dolore hic magnam nemo quasi.
  • Expedita nemo non optio performed?
  • Asperities culpa impedit in quos.
  • Cumque illum performed similique volutes.
  • Lorem ipsum dolor sit amet.
  • Eveniet optio, vitae. Fuga, illum.
  • Diagnosis fugiat laborum officiis quibusdam?
  • Cumque neque qua tempora volutes?
  • Lorem ipsum dolor sit amet.
  • Consecrated incidunt quo rem voluptatem.
  • Culpa distinctio natus pariatur velit.
  • Dolores neque similique sint ut!
  • Accuses esse itaque nihil rescinds.
  • Lorem ipsum dolor sit amet.
  • Fugit iure nesciunt placeat poor.
  • Aperiam atque cupidity numquam rescinds!
  • Illum molest officia sed veniam!
  • Enum fugiat in labore optio?

css样式代码

#scroll {
            margin: 0;
            padding: 2px 0;
            list-style: none;
            width: 300px;
            height: 200px;
            overflow: hidden;
            border: 1px #e5e5e5 solid;
            font-size: 16px;
            color: #f0f0f0;
            background-image: url("../images/p.png");
        }
 
        #scroll li {
            margin: 0;
            padding: 0 4px;
            font-size: 12px;
            height: 18px;
            line-height: 18px;
            width: 600px;
        }

方法:

dMarquee(id) {
                let speed = 20; //速度
                let stop = 1000; //停止时间
 
                let ul = document.getElementById(id);//获取ul元素
                let rows = ul.getElementsByTagName('li').length;//获取li个数
                let height = ul.getElementsByTagName('li')[0].offsetHeight;//取得第一个li的高度
                ul.innerHTML += ul.innerHTML;//获取所有li
                let timeID = false;//setTime的时间标示
                let play = function () { //移动方法
                    ul.scrollTop++;//移动滚动条
                    //移动到底的时候恢复为零
                    if (ul.scrollTop === rows * height) {
                        ul.scrollTop = 0;
                    }
                    //每次滚动
                    if (ul.scrollTop % height === 0) {
                        timeID = setTimeout(play, stop);
                    } else {
                        timeID = setTimeout(play, speed);
                    }
                }
                timeID = setTimeout(play, stop);
                ul.onmouseover = function () {
                    clearTimeout(timeID);
                }
                ul.onmouseout = play;
            }

你可能感兴趣的:(javascript,html,前端)