【问题标题】:How to detect if there's a horizontal scroll bar?如何检测是否有水平滚动条?
【发布时间】:2015-10-22 16:14:03
【问题描述】:

所以我正在制作这个 WordPress 主题,除了这个之外它是响应式的:当我缩小窗口(但大于 600 像素,因为这样菜单将变为移动版本)并将鼠标悬停在菜单项的菜单项上时。有时它会偏远,所以会出现一个滚动条。我处理这个问题的想法是检测(水平)滚动条何时出现,然后重新排列菜单。问题是我不知道如何检测是否出现滚动条。

我已经尝试了几件事,但还不能正常工作,因此无需将代码放在这里。 我用 jquery、javascript 和 css 尝试了一些东西,但似乎没有任何效果。这也是我的第一个主题,所以我是这些东西的新手。

编辑这里的请求之后是html、css和jquery(我在这里回答后使用的。

html:

<div class="menu">
<ul class="nav-menu sf-js-enabled sf-arrows" style="touch-action: pan-y;">
<li class="current_page_item">
<a href="http://localhost/wordpress/">Home</a>
</li>
<li class="page_item page-item-23 page_item_has_children">
<a href="http://localhost/wordpress/groep-8-ers/" class="sf-with-ul">Groep 8-ers</a>
<ul class="children" style="display: none;">
<li class="page_item page-item-1801">
<a href="http://localhost/wordpress/groep-8-ers/andere-info/">Andere info</a>
</li>
<li class="page_item page-item-25 page_item_has_children">
<a href="http://localhost/wordpress/groep-8-ers/de-afdelingen/" class="sf-with-ul">De afdelingen</a>
<ul class="children" style="display: none;">
</li>
</ul>
</li>
<li class="page_item page-item-213 page_item_has_children">
<a href="http://localhost/wordpress/ouders/" class="sf-with-ul">Ouders</a>
<ul class="children" style="display: none;">
<li class="page_item page-item-215 page_item_has_children">
<a href="http://localhost/wordpress/ouders/algemene-schoolzaken/" class="sf-with-ul">Algemene schoolzaken</a>
<ul class="children" style="display: none;">
<li class="page_item page-item-1805 page_item_has_children">
<a href="http://localhost/wordpress/ouders/algemene-schoolzaken/test/" class="sf-with-ul">Test</a>
<ul class="children" style="display: none;">
<li class="page_item page-item-1810">
<a href="http://localhost/wordpress/ouders/algemene-schoolzaken/test/fffff/">fffff</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>

css:

.main-navigation {
    position: relative;
    float: left;
    width: 100%;
    display: block;
    clear: both;
    font-family: 'Lato', sans-serif;
    font-weight: 700;
    text-transform: uppercase;
    background: #F2F2F2;
    background: hsl(0, 0%, 95%);
}
.main-navigation ul {
    list-style: none;
    margin: 0;
    padding-left: 0;
}
.main-navigation li {
    float: left;
    position: relative;
}
.main-navigation a {
    display: block;
    padding: 1.1em 1em;
    font-size: 17px;
    font-size: 1.7rem;
    text-decoration: none;
    line-height: 1.3em;
    color: #000;
    color: hsl(0, 0%, 0%);
}
.main-navigation ul ul {
    position: absolute;
    left: 0;
    z-index: 99999;
    display: none;
    float: left;
    padding: 0;
    background: #CFCFCF;
    background: hsl(0, 0%, 81%);
}
.main-navigation ul ul ul {
    left: 100%;
    top: 0;
    background: #9E9E9E;
    background: hsl(0, 0%, 62%);
}

.main-navigation ul ul ul ul{
    background: #6D6D6D;
    background: hsl(0, 0%, 43%);
}

.main-navigation ul ul ul ul ul{
    background: #3D3D3D;
    background: hsl(0, 0%, 24%);
    color: white;
} 

.main-navigation ul ul a {
    width: 200px;
}
.main-navigation ul ul li {
}
.main-navigation li:hover > a {
    color: #000;
    color: hsl(0, 0%, 0%);
    background: #CFCFCF;
    background: hsl(0, 0%, 81%);
}
.main-navigation ul ul :hover > a {
    background: #9E9E9E;
    background: hsl(0, 0%, 62%);
}

.main-navigation ul ul ul :hover > a{
    background: #6D6D6D;
    background: hsl(0, 0%, 43%);
}

.main-navigation ul ul ul ul :hover > a{
    background: #3D3D3D;
    background: hsl(0, 0%, 24%);
    color: white;
}

.main-navigation ul li:hover > ul {
    display: block;
}
.main-navigation .current_page_item > a,
.main-navigation .current-menu-item > a,
.main-navigation .current_page_item > a:hover,
.main-navigation .current-menu-item > a:hover {
    background: #80F77E;
    background: hsla(119, 100%, 50%, 0.4);
    color: #000;
    color: hsl(0, 0%, 0%);
}

.main-navigation .current_page_ancestor {
    background: #4d4d4d;
    background: hsl(0, 0%, 30%);
}

.main-navigation ul ul .current_page_parent,
.main-navigation .current_page_parent .current_page_item > a {
    color: #fff;
    color: hsl(0, 0%, 100%);
    background: #313131;
    background: hsl(0, 0%, 19%);
} 

JQuery:

jQuery(document).ready(function($){
    $('a').each(function(){
        if ($(this).width > $(this).parent().width()) {
            $('.main-navigation').css('background-color', 'red');
        }
    });
});

【问题讨论】:

    标签: javascript jquery html css wordpress


    【解决方案1】:

    用jquery检查内部元素的宽度是否大于父元素的宽度。

    如果你有这个结构:

    <div class="nav">
        <ul>
            <li><a href="#">Test</li>
        </ul>
    </div>
    

    用jquery检查宽度:

    $(document).ready(function(){
        $('a').each(function(){
            if ($(this).width > $(this).parent().width()) {
                //do something when it has scrollbar
            }
        });
    });
    

    【讨论】:

    • 感谢您的回复。我已经尝试过你的解决方案。首先,当我将确切的代码放入其中时出现错误,因此我将其更改为: jQuery(document).ready(function($){ $('a').each(function(){ if ($(this).宽度 > $(this).parent().width()) { $('.main-navigation').css('background-color', 'red'); } }); }); (我尝试改变菜单的颜色,所以我知道它是否有效)但是当我让滚动条出现时它不会改变颜色。你能指出我犯的错误吗?谢谢你:)
    • 您的文档中是否包含了 jquery 库?
    • 是的,它已经包含在内,因为我之前将它与其他一些东西一起使用过 :)
    • HTML、CSS 和 jQuery。
    猜你喜欢
    • 1970-01-01
    • 2019-04-18
    • 2015-10-23
    • 1970-01-01
    • 2011-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多