简体   繁体   English

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

[英]How to detect if there's a horizontal scroll bar?

So I'm making this WordPress theme, it's responsive except for this: When I size the window down (but bigger then 600px, because then the menu will change to a mobile version) and hover over a menu item's menu item. 因此,我正在制作此WordPress主题,除了以下内容外,它是响应式的:当我将窗口缩小时(但大于600像素,因为菜单将变为移动版本),并将鼠标悬停在菜单项的菜单项上。 It's sometimes to far to the side, so a scroll bar appears. 有时距离一旁很远,因此会出现一个滚动条。 My idea to deal with this problem is to detect when a (horizontal) scroll bar appears and then rearrange the menu. 我想解决此问题的想法是检测何时出现(水平)滚动条,然后重新排列菜单。 Problem is I don't have any clue how to detect if a scroll bar appears. 问题是我不知道如何检测滚动条是否出现。

I've tried a couple of things but it isn't close to working so there's no need to put the code in here. 我已经尝试了几件事,但是还差点没用,所以不需要在这里放代码。 I've tried some things with jquery, javascript and css but nothing seems to work. 我已经尝试过使用jquery,javascript和CSS进行一些操作,但是似乎没有任何效果。 Also this is my first theme, so I'm a newbie to these things. 这也是我的第一个主题,所以我是这些东西的新手。

EDIT After a request here is the html, css and jquery (which I used after an answer here. 编辑这里的一个请求后是html,css和jquery(我在这里的答案后使用。

html: 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: 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的:

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

Check with jquery if the width of the inner element is greater than the parents width. 使用jquery检查内部元素的宽度是否大于父元素的宽度。

if you have this structure: 如果您具有以下结构:

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

Check the width with jquery: 用jQuery检查宽度:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM