【问题标题】:Hide useless scrollbars that show up on Windows only隐藏仅在 Windows 上显示的无用滚动条
【发布时间】:2016-05-30 19:29:52
【问题描述】:

我的导航栏中有一个引导下拉菜单,其中显示了通知列表。这是非常标准的代码,除了我在ul 元素上设置了max-height: 300px;overflow-y: scroll;。在 Mac 上的 Chrome 或 Firefox 中查看时,直到高度达到 > 300px 并且ul 溢出滚动时才会出现滚动条。但在 Windows(Chrome 或 IE)上,垂直滚动条始终存在,这真的很烦人。有什么方法可以关闭 Windows 上的滚动条或将其隐藏到真正需要它之前?

这是下拉菜单的代码:

<li class="dropdown notifier">
    <div class="dropdown-toggle" style="width:initial;">
        <div class="dropdown-link nav-dropdown-link" id="dropdownMenuNotifications" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
            <i class="fa" ng-class="unreadNotices > 0 ? 'fa-bell-o red' : 'fa-bell-o'" style="position:absolute;right:5px;"></i>&nbsp;
            <div class="counter" ng-if="unreadNotices > 0">{{unreadNotices}}</div>
        </div>
        <ul class="dropdown-items dropdown-menu account" aria-labelledby="dropdownMenuNotifications">
            <li>
                <a href="#" class="title"><span>Notifications</span></a>
            </li>
            <li ng-if="notices.length === 0">
                <a href="#" style="cursor:default;">
                    <div class="notice-text">
                        <span>No notifications at the moment</span>
                    </div>
                </a>
            </li>
            <li ng-repeat-start="n in notices" ng-if="!n.read">
                <a href="#" class="unread" ng-if="n.action" ng-click="markRead(n, $index);setTab(n.action)">
                    <div class="icon">
                        <i class="fa" ng-class="n.icon"></i>
                    </div>
                    <div class="notice-text">
                        <span>{{n.title}}</span>
                        <br />
                        {{n.text}}
                    </div>
                    <div class="delete-notice">
                        <i class="fa fa-times" ng-click="$event.stopPropagation();delete(n, $index)"></i>
                    </div>
                </a>
                <a href="#" class="unread" ng-if="!n.action" ng-click="markRead(n, $index)">
                    <div class="icon">
                        <i class="fa" ng-class="n.icon"></i>
                    </div>
                    <div class="notice-text">
                        <span>{{n.title}}</span>
                        <br />
                        {{n.text}}
                    </div>
                    <div class="delete-notice">
                        <i class="fa fa-times" ng-click="$event.stopPropagation();delete(n, $index)"></i>
                    </div>
                </a>
            </li>
            <li ng-repeat-end="n in notices" ng-if="n.read">
                <a href="#" ng-if="n.action" ng-click="setTab(n.action)">
                    <div class="icon">
                        <i class="fa" ng-class="n.icon"></i>
                    </div>
                    <div class="notice-text">
                        <span>{{n.title}}</span>
                        <br />
                        {{n.text}}
                    </div>
                    <div class="delete-notice">
                        <i class="fa fa-times" ng-click="$event.stopPropagation();delete(n, $index)"></i>
                    </div>
                </a>
                <a href="#" ng-if="!n.action">
                    <div class="icon">
                        <i class="fa" ng-class="n.icon"></i>
                    </div>
                    <div class="notice-text">
                        <span>{{n.title}}</span>
                        <br />
                        {{n.text}}
                    </div>
                    <div class="delete-notice">
                        <i class="fa fa-times" ng-click="$event.stopPropagation();delete(n, $index)"></i>
                    </div>
                </a>
            </li>
        </ul>
    </div>
</li>

这是下拉菜单的 CSS:

.notifier {
    &:hover {
        background: initial!important;
    }
    .dropdown-toggle {
        .dropdown-link {
            padding: 5px 7px 5px 0px;
            i {
                margin-top: 0;
                font-size: 1.5em;
            }
        }
        .dropdown-items {
            max-height: 300px;
            overflow-y: scroll;
            li {
                font-size: 0.9em;
                a {
                    position: relative;
                    max-width: 300px;
                    white-space: normal;
                    border-bottom: 2px solid #EEE;
                    &.unread {
                        background: rgba(92, 184, 92, 0.07);
                        color: #333;
                    }
                    &.title {
                        text-align: center;
                        background: #FFF;
                        cursor: default;
                        &:hover {
                            background: #FFF;
                        }
                    }
                    &:hover {
                        background: #F7F7F7;
                    }
                    .notice-text {
                        margin-left: 24px;
                        margin-right: 15px;
                        span {
                            font-size: 1.1em;
                            font-weight: 700;
                        }
                    }
                    .delete-notice {
                        position: absolute;
                        top: 5px;
                        right: 0;
                        font-size: .9em;
                        &:hover {
                            color: #C9302C;
                        }
                    }
                }
            }
        }
    }
}
.counter {
    position: absolute;
    top: 0px;
    right: 5px;
    padding: 0px 5px 0px 5px;
    border-radius: 5px;
    font-size: .5em;
    font-weight: 700;
    color: #FFF;
    background: #C9302C;
}

【问题讨论】:

  • 你试过overflow-y: auto;吗?
  • overflow: scroll 将始终显示滚动条,即使在高度或宽度时内容较少
  • @philu 滚动条在内容不像容器那么高时就没有用了。

标签: html css windows scrollbar


【解决方案1】:

将您的滚动更改为:

overflow-y: auto;

将其设置为auto 将仅在需要时显示滚动条,而scroll 则建议始终存在滚动条。

您可以使用this example 使用不同的overflow 属性或在w3schools 上阅读更多信息。

-

尽管您可以考虑使用-ms-overflow-style 属性,您可以在windows dev center 中找到该属性:

-ms-overflow-style: auto | none | scrollbar | -ms-autohiding-scrollbar

【讨论】:

  • 这在 Windows 上不起作用。要查看它,请转到 Windows 机器上的 this example 链接(在 Chrome 和 Edge 上测试),然后单击“自动”。滚动条始终存在。
  • 刚刚开始工作(2021 年 5 月):overflow-y: auto; Windows Chrome 上隐藏了滚动条
【解决方案2】:

在 Windows 中,滚动条不会自动隐藏。要仅在需要时以及用户将鼠标悬停在元素上时显示滚动条,您可以使用以下 sn-p 中显示的 css:

.myContainer {
  overflow-y: hidden !important;
}

.myContainer:hover {
  overflow-y: auto !important;
}
<div class="myContainer" style="width:150px;height:150px">
  <ul>hover me</ul>
  <ul>hover me</ul>
  <ul>hover me</ul>
  <ul>hover me</ul>
  <ul>hover me</ul>
  <ul>hover me</ul>
  <ul>hover me</ul>
  <ul>hover me</ul>
</div>

【讨论】:

  • 这真的取决于我的分辨率。我在 mac 上实现了一个网站,隐藏滚动条是 mac 中的默认行为,但在 windows 中,分辨率较低 (1366 x 768),此解决方案不起作用,而在较高分辨率 (1920 x 1080) 中它可以。
猜你喜欢
  • 2019-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-14
  • 2014-01-20
  • 1970-01-01
相关资源
最近更新 更多