滚动页面时进行水平滚动

问题描述 投票:0回答:1

我有网站上所有部分的导航栏。当它是移动视图时,此导航栏是带滚动的一行。

当我将页面向下滚动到该部分时,我想要自动滚动此导航栏。

我可以不使用滑块实现这一目标吗?

.wrapper {
  width: 360px;
  height: 720px;
  border: 1px solid #000;
}

ul {
  list-style-type: none;
  padding: 10px 0;
  margin: 10px 0;
  display: flex;
  background: #ccc;
}

li {
    margin: 0 8px;
  }
<div class="wrapper">
  <ul>
  <li>link</li>
  <li>link</li>
  <li>link</li>
  <li>link</li>
  <li>link</li>
  <li>link</li>
  <li>link</li>
  <li>link</li>
  <li>link</li>
  <li>link</li>
  <li>link</li>
  <li>link</li>
  <li>link</li>    
</ul>
</div>
navbar horizontal-scrolling
1个回答
0
投票
html, body {
            margin: 0px;
            padding: 0px;
        }

        header {
            width: 100%;
            position: fixed;
            background-color: rgba(0, 0, 0, 0.7);
        }

        nav {
            max-width: 700px;
            margin: 0 auto;
            padding: 4px;
        }

        nav a {
            color: white;
            padding: 2px;
        }

        .content-container {
            max-width: 700px;
            margin: 0 auto;
        }

        .section-content {
            padding: 10px 0px;
        }

        .flex-container {
            display: flex;
            justify-content: space-around;
        }

        .flex-column {
            width: 150px;
            height: 150px;
            background-color: yellow;
            margin: 2.5px;
            display: inline-block;
        }
<header>
            <nav>
                <a href="#first">Section1</a>
                <a href="#second">Section 2</a>
                <a href="#third">Section 3</a>
                <a href="#fourth">Section 4</a>
            </nav>
        </header>
        <div class="content-container">
            <div class="section-content" id="first">
                <h3>Section 1</h3>
                <div class="flex-container">
                    <p class="flex-column">Hello world!</p>
                    <p class="flex-column">Hello world!</p>
                    <p class="flex-column">Hello world!</p>
                    <p class="flex-column">Hello world!</p>
                </div>
            </div>
            <div class="section-content" id="second">
                <h3>Section 2</h3>
                <div class="flex-container">
                    <p class="flex-column">Hello world!</p>
                    <p class="flex-column">Hello world!</p>
                    <p class="flex-column">Hello world!</p>
                    <p class="flex-column">Hello world!</p>
                </div>
                <p>Item of div section 2.</p>
            </div>
            <div class="section-content" id="third">
                <h3>Section 3</h3>
                <p>Item of div section 3.</p>
            </div>
            <div class="section-content" id="fourth">
                <h3>Section 4</h3>
                <p>Item of div section 4.</p>
            </div>
        </div>

    <script type = "text/javascript">
                $(document).ready(function(){
                        $('a[href^="#"]').on('click', function (e) {
                            e.preventDefault();
                            var target = this.hash;
                            var $target = $(target);
                            $('html, body').animate({
                                'scrollTop': $target.offset().top
                            }, 1000, 'swing');
                        });
                    });
                </script>

Working code by me

© www.soinside.com 2019 - 2024. All rights reserved.