Rumah > hujung hadapan web > tutorial js > js实现文字左右滑动的代码

js实现文字左右滑动的代码

不言
Lepaskan: 2018-08-20 17:59:56
asal
3109 orang telah melayarinya

本篇文章给大家带来的内容是关于js实现文字左右滑动的代码,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        * {
            border: 0;
            padding: 0;
            margin: 0;
            box-sizing: border-box;
        }

        p#d1, p#d2 {
            width: 102px;
            height: 30px;
            margin: 50px auto;
            border: 1px solid black;
            overflow: hidden;
        }

        p#d3, p#d4 {
            width: 300px;
            height: 100px;
            margin: 50px auto;
            border: 1px solid black;
            overflow: hidden;

        }
    </style>
</head>
<body>
<p id="d1">abcdefghijklmnopqrstuvwxyz123456789</p>
<p id="d2">987654321zyxwvutsrqponmlkjihgfedcba</p>
<p id="d3">
    <p class="pd">
        <p>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1</p>
        <p>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa2</p>
        <p>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa3</p>
        <p>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4</p>
        <p>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa5</p>
        <p>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa6</p>
        <p>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa7</p>
        <p>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa8</p>
        <p>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa9</p>
        <p>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa10</p>
    </p>

</p>
<p id="d4">
    <p class="pd2">
        <p>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1</p>
        <p>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa2</p>
        <p>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa3</p>
        <p>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4</p>
        <p>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa5</p>
        <p>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa6</p>
        <p>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa7</p>
        <p>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa8</p>
        <p>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa9</p>
        <p>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa10</p>
    </p>

</p>
<script>
    //向左转
    var d1 = document.getElementById("d1");
    d1.innerHTML += d1.innerHTML;
    setInterval(function () {
        document.title = d1.scrollLeft;
        d1.scrollLeft++;
        if (d1.scrollLeft >= d1.scrollWidth / 2) {
            d1.scrollLeft = 0;
        }
    }, 20);
    //向右转
    var d2 = document.getElementById("d2");
    d2.innerHTML += d2.innerHTML;
    d2.scrollLeft = d2.scrollWidth / 2;
    setInterval(function () {
        document.title = d2.scrollLeft;
        d2.scrollLeft--;
        if (d2.scrollLeft <= 0) {
            d2.scrollLeft = d2.scrollWidth / 2;
        }
    }, 20);
    //向上转
    var d3 = document.getElementById("d3");
    var pd = d3.getElementsByClassName("pd")[0];
    pd.innerHTML += pd.innerHTML;
    setInterval(function () {
        d3.scrollTop++;
        if (d3.scrollTop >= d3.scrollHeight / 2) {
            d3.scrollTop = 0;
        }
    }, 20);
    //向下转
    var d4 = document.getElementById("d4");
    var pd2 = d4.getElementsByClassName("pd2")[0];
    pd2.innerHTML += pd2.innerHTML;
    d4.scrollTop = d4.scrollHeight / 2;
    setInterval(function () {
        d4.scrollTop--;
        if (d4.scrollTop <= 0) {
            d4.scrollTop = d4.scrollHeight / 2;
        }
    }, 20);

</script>
</body>
</html>
Salin selepas log masuk

相关推荐:

js获取行间样式和当前样式的代码

js实现链式栈的代码实例

Atas ialah kandungan terperinci js实现文字左右滑动的代码. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan