导航条滚动(上下翻页,滑动翻页)

视频效果 

导航滚动条

html代码如下: 

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcss.com/jquery/3.3.0/jquery.min.js"></script>
    <style>
        body {
            background-color: #f1f1f1;
            margin: 0;
            padding: 0;
        }

        ::-webkit-scrollbar {
            display: none;
        }

        #listDiv {
            width: 100%;
            overflow-x: scroll;
            background-color: #ffffff;
        }

        #listDiv ul {
            display: flex;
            white-space: nowrap;
            padding-left: 20px;
        }

        #listDiv ul li {
            padding: 5px 15px 5px 15px;
            background-color: #ededed;
            margin-right: 10px;
            display: flex;
            justify-content: center;
            border-radius: 25px;
        }

        .prev,
        .next {
            position: fixed;
            top: 20px;
        }

        .prev {
            left: 0px;
            display: none;
        }

        .next {
            right: 0px;
        }
    </style>
</head>

<body>
    <svg t="1698737430930" class="prev" class="icon" viewBox="0 0 1024 1024" version="1.1"
        xmlns="http://www.w3.org/2000/svg" p-id="9839" width="26" height="26">
        <path
            d="M378.630968 512.004093 705.390034 177.12714c11.038404-11.33107 11.038404-29.692253 0-41.021276-11.07729-11.32186-28.988218-11.32186-40.027645 0L318.636572 491.503177c-11.07729 11.32186-11.07729 29.68202 0 41.002856L665.362388 887.895159c11.039427 11.320836 28.950356 11.320836 40.027645 0 11.038404-11.320836 11.038404-29.68202 0-41.013089L378.630968 512.004093z"
            fill="#8a8a8a" p-id="9840"></path>
    </svg>
    <svg t="1698728822266" class="next" class="icon" viewBox="0 0 1024 1024" version="1.1"
        xmlns="http://www.w3.org/2000/svg" p-id="2867" width="26" height="26">
        <path
            d="M711.6 488.624L355.2 152.976a29.36 29.36 0 0 0-42.352 0 31.408 31.408 0 0 0 0 43.552L647.76 512 312.848 827.36a31.408 31.408 0 0 0 0 43.552 29.36 29.36 0 0 0 42.352 0l356.4-335.648a36.32 36.32 0 0 0 0-46.64z"
            fill="#8a8a8a" p-id="2868" data-spm-anchor-id="a313x.search_index.0.i2.41143a816djp7O" class="selected">
        </path>
    </svg>
    <div id="listDiv">
        <ul id="list">
            <li class="item">李白</li>
            <li class="item">杜甫</li>
            <li class="item">杜牧</li>
            <li class="item">王安石</li>
            <li class="item">无承诺</li>
            <li class="item">曹雪芹</li>
        </ul>
    </div>
</body>
<script>
    $(document).ready(function () {
        var container = $("#listDiv");
        var leftIcon = document.querySelector('#listDiv');
        var totalWidth = $("#list").outerWidth(true);
        var prevIcon = document.getElementsByClassName("prev")[0];
        var nextIcon = document.getElementsByClassName("next")[0];
        $(".prev").click(function () {
            container.animate({ scrollLeft: "-=" + totalWidth }, "slow");
        });
        $(".next").click(function () {
            container.animate({ scrollLeft: "+=" + totalWidth }, "slow");
        });
        $("#listDiv").scroll(function () {
            let left = Math.trunc(leftIcon.scrollLeft);
            if (left == 0) {
                prevIcon.style.display = 'none';
            }
            if (left > 0) {
                prevIcon.style.display = 'block';
            }
            if (left < leftIcon.scrollWidth - totalWidth) {
                nextIcon.style.display = 'block';
            }
            if (left == leftIcon.scrollWidth - totalWidth) {
                nextIcon.style.display = 'none';
            }
        });
    })
</script>

</html>

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个简单的 jQuery 滑动导航条的示例: HTML 代码: ``` <nav> <ul> <li><a href="#section1">Section 1</a></li> <li><a href="#section2">Section 2</a></li> <li><a href="#section3">Section 3</a></li> </ul> </nav> <section id="section1"> <h2>Section 1</h2> <p>Content for section 1 goes here.</p> </section> <section id="section2"> <h2>Section 2</h2> <p>Content for section 2 goes here.</p> </section> <section id="section3"> <h2>Section 3</h2> <p>Content for section 3 goes here.</p> </section> ``` CSS 代码: ``` nav { position: fixed; top: 0; left: 0; width: 100%; background-color: #333; color: #fff; } nav ul { margin: 0; padding: 0; list-style: none; display: flex; justify-content: space-around; } nav ul li { margin: 0 10px; } nav ul li a { display: block; padding: 10px; text-decoration: none; color: #fff; } nav ul li a:hover { background-color: #555; } ``` jQuery 代码: ``` $(document).ready(function() { $('nav ul li a').click(function(e) { e.preventDefault(); // 阻止默认行为 var target = $(this).attr('href'); // 获取目标元素的 ID $('html, body').animate({ scrollTop: $(target).offset().top // 滑动到目标元素的位置 }, 1000); // 滑动时间为 1 秒 }); }); ``` 解释: 1. 在 HTML 中,我们创建了一个带有三个部分的导航条和三个部分的页面内容。每个部分都有一个唯一的 ID,可以通过链接中的 # 符号进行访问。 2. 在 CSS 中,我们创建了一个固定在页面顶部的导航条。 3. 在 jQuery 中,我们使用了 `.click()` 方法来监听导航链接的点击事件。当链接被点击时,我们使用 `.attr()` 方法获取目标元素的 ID,并使用 `.offset().top` 方法获取目标元素相对于文档顶部的位置。然后,我们使用 `.animate()` 方法滑动页面到目标位置。滑动时间为 1 秒。 4. 通过以上代码,当用户点击导航链接时,页面会平滑滑动到对应的部分。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晚时之秋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值