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

时间:2015-10-21 12:01:54

标签: javascript jquery html css wordpress

所以我正在制作这个WordPress主题,除此之外它响应:当我调整窗口大小(但是大于600px,因为那时菜单将更改为移动版本)并将鼠标悬停在菜单项的菜单项。它有时会偏向一侧,因此会出现一个滚动条。处理此问题的想法是检测何时出现(水平)滚动条,然后重新排列菜单。问题是我没有任何线索如何检测滚动条是否出现。

我尝试过几件事,但它并没有接近工作,所以不需要把代码放在这里。 我用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');
        }
    });
});

1 个答案:

答案 0 :(得分:0)

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