如何检测是否有水平滚动条?



所以我做这个WordPress主题,它是响应性的,除了这个:当我缩小窗口的大小(但大于600px,因为这样菜单会变成一个移动版本),并悬停在菜单项的菜单项。有时它太过靠近一侧,所以会出现一个滚动条。我处理这个问题的想法是检测(水平)滚动条出现时,然后重新排列菜单。问题是我没有任何线索如何检测滚动条是否出现。

我已经尝试了一些事情,但它不是接近工作,所以没有必要把代码在这里。我已经尝试了一些东西与jquery, javascript和css,但似乎没有工作。这也是我的第一个主题,所以我是这些东西的新手。

EDIT在请求之后这里是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');
        }
    });
});

使用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
        }
    });
});

最新更新