2017-10-11 67 views
1

在我的导航栏中遇到了一些麻烦。出于某种原因,我无法弄清楚,当有人点击搜索按钮时,打开的窗口将扩展到窗口边缘。css&jquery:打开div展开过去的屏幕边缘

完整的示例可在这里:https://codepen.io/NaughtySquid/pen/MEXGpM

下面是完整的代码,首先将HTML:

<nav class="navigation-main"> 
    <div class="container group"> 
    <div class="right-menu"> 
     <div id="search-button" class="toggle-nav search-box"> 
     <a href="#"><img src="http://worldartsme.com/images/search-button-clipart-1.jpg" width="14" height="14" alt="" /></a> 
     <div class="toggle-content"> 
      <form method="get" action="{:url}index.php?module=search"> 
      <input type="hidden" name="module" value="search" /> Search <input type="text" class="search-field ays-ignore" name="q" placeholder="Search Articles…" /> 
      <input type="submit" style="position: absolute; left: -9999px; width: 1px; height: 1px;" /> 
      </form> 
     </div> 
     </div> 
    </div> 
    </div> 
</nav> 

这里的CSS:

body { 
    background: #ffffff; 
    color: #404040; 
    font-family: 'Ubuntu', sans-serif; 
    margin: 47px auto; 
    padding: 0px; 
    word-wrap: break-word; 
} 
.navigation-main { 
     position: fixed; 
     top: 0; 
     width: 100%; 
     height: 49px; 
     z-index: 999; 
     background-color: #222; 
    margin: 0 !important; 
} 
.header-navbar { 
     list-style: none; 
     padding: 0; 
     margin: 0; 
} 
.header-search { 
    padding: 9px 0; 
    margin-right: 5px; 
} 
.header-search .search-field { 
    width: 200px; 
    background-color: #333; 
    border: 1px solid #5c5c5c; 
    outline: none; 
    line-height: 19px; 
    height: 30px; 
    color: #fff; 
    margin: 0; 
} 
/* toggle menus */ 
.right-menu{float: right; right: 0px;} 
.toggle-content 
{ 
    display: none; 
    white-space: nowrap; 
    position: absolute; 
    right: 0; 
    background: #222; 
    box-shadow: 1px 1px 1px black; 
    padding: 5px; 
    word-wrap: normal; 
    z-index: 999999; 
} 
.toggle-active {display: block;} 
.toggle-content ul 
{ 
    list-style: none; 
    list-style-type: none; 
    margin: 0; 
    padding: 0; 
    color: #999; 
} 
.toggle-content li 
{ 
    margin: 0; 
    padding: 0; 
} 
.toggle-content a 
{ 
    display: block; 
    color: #999; 
    padding: 5px 
    !important; 

} 
.toggle-content a:hover {color: #fff;} 
/* search box */ 
.search-box {float: left; color: #999; line-height: 29px; background-color: #383838;} 
.search-box:hover{background-color: #4B4B4B;} 
.search-box a {padding: 10px; height: 29px; display: block; color: #999;} 
.search-box:hover{cursor: pointer;} 
#search-button .toggle-content {width: 100%; border-top: 1px solid black; text-align: center; left: 0px;} 
    input, textarea, select { 
     background: #fcfcfc; 
     border: 1px solid #ddd; 
     color: #444444; 
     margin-bottom: 7px; 
     padding: 7px; 
     width: 100%; 
     -webkit-box-sizing: border-box; 
      -moz-box-sizing: border-box; 
       box-sizing: border-box; 
     border-radius: .2em; 
    } 

最后JQuery的:

$(document).ready(function() { 
    $(document).on("click", ".toggle-nav > a", function(event) { 
    event.preventDefault(); 
    event.stopPropagation(); 
    var $toggle = $(this) 
     .closest(".toggle-nav") 
     .children(".toggle-content"); 
    if ($toggle.hasClass("toggle-active")) { 
     $($toggle).removeClass("toggle-active"); 
    } else { 
     $(".toggle-content").removeClass("toggle-active"); 
     $($toggle).addClass("toggle-active"); 
    } 
    }); 

    $(document).click(function(e) { 
    if (
     !$(e.target).is("#search-button .toggle-content .search-field") && 
     !$(e.target).is("#search-button .toggle-content") 
    ) { 
     $(".toggle-content").removeClass("toggle-active"); 
    } 
    }); 
}); 

我真的很喜欢内容被屏幕边缘完美地绑定。

我已经花了最后两天的时间与Firefox检查器,切换事情和调整的东西,我只是不能确定什么是推动它。

+0

它是正确的宽度,该字段为100%,但你把搜索文本旁边这么领域熄灭 – DaFois

+0

如果您删除“搜索”文本,它仍然扩展到窗口边缘之外。文本不是问题。 – NaughtySquid

+0

不,如果你删除输入的填充 – DaFois

回答

0

使用下面的CSS来修复指定的盒子的溢出:

max-width:你在像素;

选择的这个宽度不应该让箱子扩大过去的窗口的宽度。

0

$(document).ready(function() { 
 
    $(document).on("click", ".toggle-nav > a", function(event) { 
 
    event.preventDefault(); 
 
    event.stopPropagation(); 
 
    var $toggle = $(this) 
 
     .closest(".toggle-nav") 
 
     .children(".toggle-content"); 
 
    if ($toggle.hasClass("toggle-active")) { 
 
     $($toggle).removeClass("toggle-active"); 
 
    } else { 
 
     $(".toggle-content").removeClass("toggle-active"); 
 
     $($toggle).addClass("toggle-active"); 
 
    } 
 
    }); 
 

 
    $(document).click(function(e) { 
 
    if (
 
     !$(e.target).is("#search-button .toggle-content .search-field") && 
 
     !$(e.target).is("#search-button .toggle-content") 
 
    ) { 
 
     $(".toggle-content").removeClass("toggle-active"); 
 
    } 
 
    }); 
 
});
body { 
 
    background: #ffffff; 
 
    color: #404040; 
 
    font-family: 'Ubuntu', sans-serif; 
 
    margin: 47px auto; 
 
    padding: 0px; 
 
    word-wrap: break-word; 
 
} 
 
.navigation-main { 
 
     position: fixed; 
 
     top: 0; 
 
     width: 100%; 
 
     height: 49px; 
 
     z-index: 999; 
 
     background-color: #222; 
 
    margin: 0 !important; 
 
} 
 
.header-navbar { 
 
     list-style: none; 
 
     padding: 0; 
 
     margin: 0; 
 
} 
 
.header-search { 
 
    padding: 9px 0; 
 
    margin-right: 5px; 
 
} 
 
.header-search .search-field { 
 
    width: 200px; 
 
    background-color: #333; 
 
    border: 1px solid #5c5c5c; 
 
    outline: none; 
 
    line-height: 19px; 
 
    height: 30px; 
 
    color: #fff; 
 
    margin: 0; 
 
} 
 
/* toggle menus */ 
 
.right-menu{float: right; right: 0px;} 
 
.toggle-content 
 
{ 
 
    display: none; 
 
    white-space: nowrap; 
 
    position: absolute; 
 
    right: 0; 
 
    background: #222; 
 
    box-shadow: 1px 1px 1px black; 
 
    padding: 5px; 
 
    word-wrap: normal; 
 
    z-index: 999999; 
 
} 
 
.toggle-active {display: block;} 
 
.toggle-content ul 
 
{ 
 
    list-style: none; 
 
    list-style-type: none; 
 
    margin: 0; 
 
    padding: 0; 
 
    color: #999; 
 
} 
 
.toggle-content li 
 
{ 
 
    margin: 0; 
 
    padding: 0; 
 
} 
 
.toggle-content a 
 
{ 
 
    display: block; 
 
    color: #999; 
 
    padding: 5px 
 
    !important; 
 

 
} 
 
.toggle-content a:hover {color: #fff;} 
 
/* search box */ 
 
.search-box {float: left; color: #999; line-height: 29px; background-color: #383838;} 
 
.search-box:hover{background-color: #4B4B4B;} 
 
.search-box a {padding: 10px; height: 29px; display: block; color: #999;} 
 
.search-box:hover{cursor: pointer;} 
 
#search-button .toggle-content {width: 100%; border-top: 1px solid black; text-align: center; left: 0px;} 
 
    input, textarea, select { 
 
     background: #fcfcfc; 
 
     border: 1px solid #ddd; 
 
     color: #444444; 
 
     margin-bottom: 7px; 
 
     padding: 7px; 
 
     width: calc(100% - 80px); 
 
     -webkit-box-sizing: border-box; 
 
      -moz-box-sizing: border-box; 
 
       box-sizing: border-box; 
 
     border-radius: .2em; 
 
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> 
 
<nav class="navigation-main"> 
 
    <div class="container group"> 
 
    <div class="right-menu"> 
 
     <div id="search-button" class="toggle-nav search-box"> 
 
     <a href="#"><img src="http://worldartsme.com/images/search-button-clipart-1.jpg" width="14" height="14" alt="" /></a> 
 
     <div class="toggle-content"> 
 
      <form method="get" action="{:url}index.php?module=search"> 
 
      <input type="hidden" name="module" value="search" /> Search <input type="text" class="search-field ays-ignore" name="q" placeholder="Search Articles…" /> 
 
      <input type="submit" style="position: absolute; left: -9999px; width: 1px; height: 1px;" /> 
 
      </form> 
 
     </div> 
 
     </div> 
 
    </div> 
 
    </div> 
 
</nav>

+0

你能解释你改变了什么吗? – NaughtySquid

+0

我已经把宽度:calc(100% - 80px)... 80px或多或少是搜索词的宽度 – DaFois

0

看到这个笔https://codepen.io/anon/pen/PJaajr

我增加了以下内容:

.toggle-content {box-sizing: border-box;} 

,并在包裹跨度搜索标签

<span class="search-label">Search</span> 

然后添加CSS样式它

.search-label { 
    width:60px; 
    display:inline-block; 
} 

然后改变

input, textarea, select {width: calc(100% - 64px);/*additional 4px to account for whitespace*/}