1. 程式人生 > 其它 >js操作文字橫向滾動

js操作文字橫向滾動

HTML:

<div id="scroll_div">
        <div id="scroll_begin">
            <span class="pad_right">這是第一條資訊的內容這是第一條資訊的內容這是第一條資訊的內容的斤斤計較斤斤計較斤斤計較斤斤計較斤斤計較斤斤計較斤斤計較斤斤計較斤斤計較斤斤計較斤斤計較斤斤計較斤斤計較斤斤計較斤斤計較接佳佳啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊</span>
        </div>
        <div id="scroll_end
"></div> </div>

CSS:

 <style>
      #scroll_div {
    height: 30px;
    line-height: 30px;
    overflow: hidden;
    white-space: nowrap;
    width: 800px;
    background-color: #23527c;
    color: #d8d8d8;
    margin: 1rem 0;
    text-align: center;
}
#scroll_begin,#scroll_end {
    display: inline;
}

JS:滑鼠移上去時滾動,滑鼠移除時停住

<script>
    function ScrollImgLeft() {
        var speed = 40;//初始化速度 也就是字型的整體滾動速度
        var MyMar = null;//初始化一個變數為空 用來存放獲取到的文字內容
        var scroll_begin = document.getElementById("scroll_begin");//獲取滾動的開頭id
        var scroll_end = document.getElementById("scroll_end");//獲取滾動的結束id
var scroll_div = document.getElementById("scroll_div");//獲取整體的開頭id scroll_end.innerHTML = scroll_begin.innerHTML;//滾動的是html內部的內容,原生知識! //定義一個方法 function Marquee() { if (scroll_end.offsetWidth - scroll_div.scrollLeft <= 0){ scroll_div.scrollLeft -= scroll_begin.offsetWidth; } else{ scroll_div.scrollLeft++; } } //滑鼠滑入這條公告欄的時候,清除上面的方法,讓公告欄暫停 scroll_div.onmouseover = function () { MyMar = setInterval(Marquee, speed); } //滑鼠滑出這條公告欄的時候,公告欄繼續移動 scroll_div.onmouseout = function () { clearInterval(MyMar); // scroll_div.scrollLeft =0; } } ScrollImgLeft(); </script>

JS:滑鼠移上去時滾動,滑鼠移除時回到最初的樣子,停住

<script>
    function ScrollImgLeft() {
        var speed = 40;//初始化速度 也就是字型的整體滾動速度
        var MyMar = null;//初始化一個變數為空 用來存放獲取到的文字內容
        var scroll_begin = document.getElementById("scroll_begin");//獲取滾動的開頭id
        var scroll_end = document.getElementById("scroll_end");//獲取滾動的結束id
        var scroll_div = document.getElementById("scroll_div");//獲取整體的開頭id
        scroll_end.innerHTML = scroll_begin.innerHTML;//滾動的是html內部的內容,原生知識!
        //定義一個方法
        function Marquee() {
            if (scroll_end.offsetWidth - scroll_div.scrollLeft <= 0){
                scroll_div.scrollLeft -= scroll_begin.offsetWidth;
            } else{
                scroll_div.scrollLeft++;
            }
        }
        //滑鼠滑入這條公告欄的時候,清除上面的方法,讓公告欄暫停
        scroll_div.onmouseover = function () {
            MyMar = setInterval(Marquee, speed);
        }
        //滑鼠滑出這條公告欄的時候,公告欄繼續移動
        scroll_div.onmouseout = function () {
            clearInterval(MyMar);
            scroll_div.scrollLeft =0;
        }
    }
    ScrollImgLeft();
</script>