如何在没有滚动条的情况下创建水平可滑动菜单?

时间:2016-04-14 02:27:41

标签: html css scroll menu touch

我想在我的移动网站上设置一个可滑动的水平菜单,就像Google在其结果页面上使用一样。我创建了一个基本的html / css,它工作正常,但我无法摆脱滚动条......

以下是代码:

<div style=" -ms-overflow-style: -ms-autohiding-scrollbar; -webkit-user-select: none; display: block; height: 50px; overflow-y: hidden; padding: 0px; position: relative; -webkit-overflow-scrolling: touch; overflow-x: scroll;">
          <div style="display: inline-block; position: relative; white-space: nowrap; overflow: hidden;">
               <div style="display: inline-block; height: 40px; text-align: center; padding: 0 16px;">ITEM</div>
               <div style="display: inline-block; height: 40px; text-align: center; padding: 0 16px;">ITEM</div>
               <div style="display: inline-block; height: 40px; text-align: center; padding: 0 16px;">ITEM</div>
               <div style="display: inline-block; height: 40px; text-align: center; padding: 0 16px;">ITEM</div>
               <div style="display: inline-block; height: 40px; text-align: center; padding: 0 16px;">ITEM</div>
               <div style="display: inline-block; height: 40px; text-align: center; padding: 0 16px;">ITEM</div>
               <div style="display: inline-block; height: 40px; text-align: center; padding: 0 16px;">ITEM</div>
          </div>
     </div>

有什么想法吗?

See my menu here

See Google example here

2 个答案:

答案 0 :(得分:1)

您可以创建一个包含滚动<div>的容器<div>元素 此父<div>样式可以设置为:oveflow:hidden,其大小小于其子<div> - 其样式设置为overflow:scroll
这样滚动条就会隐藏起来,你不必担心用JavaScript做这件事;但是:

  • 建议您在<style>元素或单独的CSS文件中创建CSS类,您可以在其中定义这些类,并在HTML中使用这些类,如:<div class="menu">... < / LI>

根据上面的示例使用内联CSS呈现无法管理的代码,尤其是在较大的项目中

更新

以下是一个完整的例子:

<html>
    <head>
        <style>
            div
            {
                box-sizing:border-box;
                -moz-box-sizing:border-box;
                -webkit-box-sizing:border-box;
            }

            .scrollHider
            {
                display:inline-block;
                width:302px;
                height:162px;
                overflow:hidden;
                border:1px solid #AAA;
            }

            .menuBox
            {
                width:315px;
                height:175px;
                overflow:scroll;
                white-space:nowrap;
            }

            .menuSection
            {
                width:300px;
                height:160px;
                display:table-cell;
                overflow:hidden;
            }

            .menuItem
            {
                width:300px;
                height:40px;
                text-align:center;
                padding:10px;
                border:1px solid #AAA;
            }
        </style>
    </head>
    <body>
        <div class="scrollHider">
            <div class="menuBox">
                <div class="menuSection">
                    <div class="menuItem">ITEM 1</div>
                    <div class="menuItem">ITEM 2</div>
                    <div class="menuItem">ITEM 3</div>
                    <div class="menuItem">ITEM 4</div>
                </div>
                <div class="menuSection">
                    <div class="menuItem">ITEM 5</div>
                    <div class="menuItem">ITEM 6</div>
                    <div class="menuItem">ITEM 7</div>
                    <div class="menuItem">ITEM 8</div>
                </div>
            </div>
        </div>
    </body>
</html>

预览:(部分截图)

preview

预览(上图)显示了向右滚动的位置。复制&amp;将上面的代码粘贴到一个空的HTML文件中,另存为&amp;在Web浏览器中打开。如果您不使用手机/触摸屏,也可以单击它并使用键盘箭头滚动。

答案 1 :(得分:0)

首先将此链接放在您的头标签中。它来自purecss.io,它是一个小型的css模块库。我在大约40%的项目中使用纯净。

 <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.6.0/pure-min.css">

然后将其放入HTML页面的正文中。我建议稍后将css和js分成他们自己的文件。

<style>
    .custom-menu-wrapper {
        background-color: #808080;
        margin-bottom: 2.5em;
        white-space: nowrap;
        position: relative;
    }

    .custom-menu {
        display: inline-block;
        width: auto;
        vertical-align: middle;
        -webkit-font-smoothing: antialiased;
    }

    .custom-menu .pure-menu-link,
    .custom-menu .pure-menu-heading {
        color: white;
    }

    .custom-menu .pure-menu-link:hover,
    .custom-menu .pure-menu-heading:hover {
        background-color: transparent;
    }

    .custom-menu-top {
        position: relative;
        padding-top: .5em;
        padding-bottom: .5em;
    }

    .custom-menu-brand {
        display: block;
        text-align: center;
        position: relative;
    }

    .custom-menu-toggle {
        width: 44px;
        height: 44px;
        display: block;
        position: absolute;
        top: 3px;
        right: 0;
        display: none;
    }

    .custom-menu-toggle .bar {
        background-color: white;
        display: block;
        width: 20px;
        height: 2px;
        border-radius: 100px;
        position: absolute;
        top: 22px;
        right: 12px;
        -webkit-transition: all 0.5s;
        -moz-transition: all 0.5s;
        -ms-transition: all 0.5s;
        transition: all 0.5s;
    }

    .custom-menu-toggle .bar:first-child {
        -webkit-transform: translateY(-6px);
        -moz-transform: translateY(-6px);
        -ms-transform: translateY(-6px);
        transform: translateY(-6px);
    }

    .custom-menu-toggle.x .bar {
        -webkit-transform: rotate(45deg);
        -moz-transform: rotate(45deg);
        -ms-transform: rotate(45deg);
        transform: rotate(45deg);
    }

    .custom-menu-toggle.x .bar:first-child {
        -webkit-transform: rotate(-45deg);
        -moz-transform: rotate(-45deg);
        -ms-transform: rotate(-45deg);
        transform: rotate(-45deg);
    }

    .custom-menu-screen {
        background-color: rgba(0, 0, 0, 0.5);
        -webkit-transition: all 0.5s;
        -moz-transition: all 0.5s;
        -ms-transition: all 0.5s;
        transition: all 0.5s;
        height: 3em;
        width: 70em;
        position: absolute;
        top: 0;
        z-index: -1;
    }

    .custom-menu-tucked .custom-menu-screen {
        -webkit-transform: translateY(-44px);
        -moz-transform: translateY(-44px);
        -ms-transform: translateY(-44px);
        transform: translateY(-44px);
    }

    @media (max-width: 62em) {

        .custom-menu {
            display: block;
        }

        .custom-menu-toggle {
            display: block;
            display: none\9;
        }

        .custom-menu-bottom {
            position: absolute;
            width: 100%;
            border-top: 1px solid #eee;
            background-color: #808080\9;
            z-index: 100;
        }

        .custom-menu-bottom .pure-menu-link {
            opacity: 1;
            -webkit-transform: translateX(0);
            -moz-transform: translateX(0);
            -ms-transform: translateX(0);
            transform: translateX(0);
            -webkit-transition: all 0.5s;
            -moz-transition: all 0.5s;
            -ms-transition: all 0.5s;
            transition: all 0.5s;
        }

        .custom-menu-bottom.custom-menu-tucked .pure-menu-link {
            -webkit-transform: translateX(-140px);
            -moz-transform: translateX(-140px);
            -ms-transform: translateX(-140px);
            transform: translateX(-140px);
            opacity: 0;
            opacity: 1\9;
        }

        .pure-menu-horizontal.custom-menu-tucked {
            z-index: -1;
            top: 45px;
            position: absolute;
            overflow: hidden;
        }

    }
    </style>
    <div class="custom-menu-wrapper">
        <div class="pure-menu custom-menu custom-menu-top">
            <a href="#" class="pure-menu-heading custom-menu-brand">Brand</a>
            <a href="#" class="custom-menu-toggle" id="toggle"><s class="bar"></s><s class="bar"></s></a>
        </div>
        <div class="pure-menu pure-menu-horizontal pure-menu-scrollable custom-menu custom-menu-bottom custom-menu-tucked" id="tuckedMenu">
            <div class="custom-menu-screen"></div>
            <ul class="pure-menu-list">
                <li class="pure-menu-item"><a href="#" class="pure-menu-link">Home</a></li>
                <li class="pure-menu-item"><a href="#" class="pure-menu-link">About</a></li>
                <li class="pure-menu-item"><a href="#" class="pure-menu-link">Contact</a></li>
                <li class="pure-menu-item"><a href="#" class="pure-menu-link">Blog</a></li>
                <li class="pure-menu-item"><a href="#" class="pure-menu-link">GitHub</a></li>
                <li class="pure-menu-item"><a href="#" class="pure-menu-link">Twitter</a></li>
                <li class="pure-menu-item"><a href="#" class="pure-menu-link">Apple</a></li>
                <li class="pure-menu-item"><a href="#" class="pure-menu-link">Google</a></li>
                <li class="pure-menu-item"><a href="#" class="pure-menu-link">Wang</a></li>
                <li class="pure-menu-item"><a href="#" class="pure-menu-link">Yahoo</a></li>
                <li class="pure-menu-item"><a href="#" class="pure-menu-link">W3C</a></li>
            </ul>
        </div>
    </div>
    <script>
    (function (window, document) {
    document.getElementById('toggle').addEventListener('click', function (e) {
        document.getElementById('tuckedMenu').classList.toggle('custom-menu-tucked');
        document.getElementById('toggle').classList.toggle('x');
    });
    })(this, this.document);
    </script>