gpt4 book ai didi

javascript - 防止滚动但 div 需要位置 :absolute

转载 作者:太空宇宙 更新时间:2023-11-04 10:46:38 26 4
gpt4 key购买 nike

我的滑入式菜单仍有问题。据我所知,一切正常,但当用户向下滚动并通过汉堡图标打开菜单时,导航不可见,因为它也在滚动。所以我需要的是让 navi 变粘。但是当我将位置(类:导航)从“绝对”更改为“固定”时,导航超出了我的最大宽度(如果屏幕大小 > 最大宽度)。

  $(document).ready(function(){

$('input:checkbox').change(function(){
if($(this).is(":checked")) {
$(".content").animate({ left: '-200px' });
$(".content").css({"box-shadow": "0 0 5px 5px rgba(0,0,0,0.5)" });
} else {
$(".content").animate({ left: '0' });
$(".content").css({"box-shadow": "none" });
}
});

});
body {
background-color:red;
}
h1 {
padding: 10px 0 20px 0;
font-size: 1.7em;
}
p {
padding-bottom: 20px;
line-height: 18px;
}
.wrp {
position:relative;
max-width: 1200px;
margin: 0px auto;
padding:0 20px;
height:100%;
background-color:orange;
}
.header {
position:fixed;
top:0;
width:100%;
background-color: white;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.5);
z-index:1000000;
}
.logo {
float:left;
width: 200px;
border:solid red 1px;
}
.menu {
float:right;
border:solid black 1px;
position:relative;
}
.content2 {
padding:5px 10px;
background-color:blue;
color:white;
position: relative;
top: 0;
bottom: 100%;
left: 0;
}
.content {
/* Critical position and size styles */
min-height: 100%;
min-width: 100%;
position: relative;
top: 0;
bottom: 100%;
left: 0;
z-index: 1;
background-color:blue; /* Needs a background or else the nav will show through */
/* non-critical apperance styles */
background-size: 200%;
opacity: 0.5;
transition: right 0.5s;
}
.nav {
position:absolute;
width: 200px;
top: 0;
right: 0;
bottom: 0;
padding-top: 50px;
background-color:green;
opacity: 0.5;
}

/* hb icon */

.menu-icon {
padding: 18px 14px; /* better clickable > please consider in header */
cursor: pointer;
display: inline-block;
float: right;
user-select: none;
-moz-user-select: none;
-webkit-user-select: none;
}

.menu-icon .navicon {
background: #333;
display: block;
height: 2px;
position: relative;
width: 18px;
}

.menu-icon .navicon:before,
.menu-icon .navicon:after {
background: #333;
content: '';
display: block;
height: 100%;
position: absolute;
transition: all .2s ease-out;
width: 100%;
}

.menu-icon .navicon:before {
top: 5px;
}

.menu-icon .navicon:after {
top: -5px;
}

/* Nav Trigger */

.nav-trigger {
/* critical styles - hide the checkbox input */
position: absolute;
clip: rect(0, 0, 0, 0);
}

label[for="nav-trigger"] {
/* critical positioning styles */
right: 15px;
top:0;
z-index: 3;

}

.nav-trigger:checked ~ .menu-icon .navicon {
background: transparent;
}

.nav-trigger:checked ~ .menu-icon .navicon:before {
transform: rotate(-45deg);
}

.nav-trigger:checked ~ .menu-icon .navicon:after {
transform: rotate(45deg);
}

.nav-trigger:checked ~ .menu-icon:not(.steps) .navicon:before,
.nav-trigger:checked ~ .menu-icon:not(.steps) .navicon:after {
top: 0;
}

/* Make the Magic Happen */
.nav-trigger + label, .wrp {
transition: left 0.5s;
}

.nav-trigger + label {
right: 15px;
transition: right 0.5s;
}

.nav-trigger:checked + label {
right: 215px;
}

.nav-trigger:checked ~ .nav {
border: 4px solid red;
}

.nav-trigger:checked ~ .content {
left: -200px;
box-shadow: 0 0 5px 5px rgba(0,0,0,0.5);
background-color:red;
}
<link href="http://yui.yahooapis.com/3.18.1/build/cssreset/cssreset-min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="header">
<div class="wrp">
<div class="logo">Logo</div>
<div class="menu">
<input type="checkbox" class="nav-trigger" id="nav-trigger" />
<label for="nav-trigger" class="menu-icon"><span class="navicon"></span></label>
</div>
<div style="clear:both;"></div>
</div>
</div>
<div class="wrp">
<div class="content">
<h1>This is my content</h1>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>
<p>Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
<p>Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>
<p>Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
<p>Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>
<p>Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
<p>Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p>
</div>
<div class="nav">
<h1>Navi</h1>
</div>
</div>

Codepen 上的代码:http://codepen.io/anon/pen/NxeLzY

有人能帮忙吗?

最佳答案

在导航容器内放置一个子容器,例如:

<div class="nav">
<div class="nav-child">
<h1>Navi</h1>
</div>
</div>

然后设置样式,使子容器固定:

.nav-child{
position:fixed;
}

Codepen 解决方案请看这里:

http://codepen.io/anon/pen/mVazJq

关于javascript - 防止滚动但 div 需要位置 :absolute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35342391/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com