gpt4 book ai didi

html - 如何留下伴随滚动条的菜单?

转载 作者:行者123 更新时间:2023-11-28 06:25:01 24 4
gpt4 key购买 nike

如何根据滚动离开设置的菜单?

我做了一个片段来说明代码。当我滚动他所在位置的页面时,我会打开菜单。我希望菜单能够跟踪滚动页面。

我该怎么做?

		var nav = document.getElementById("menu");
var showMenu = document.getElementById("abrir");
var hideMenu = document.getElementById("fechar");
showMenu.addEventListener("click", function () {
nav.classList.add("show");
});

hideMenu.addEventListener("click", function () {
nav.classList.remove("show");
});
	html, body{
padding: 0;
margin: 0;
width: 100%;
height: 100%;
}

#site{
position: absolute;
width: 100%;
height: 500px;

}

#fundo_site{
position: absolute;
width: 100%;
height: auto;
}

#menu{
position: absolute;
left: -292px;
height:100%;
width:15%;
transition: left 0.5s linear;
}

#img_menu{
width: 100%;
height: 100%;
}

#menu.show{
left:0;
}

#bt_menu{
width:20%;
height:20%;
margin: 15px;
}


p{
font-family: 'Raleway', sans-serif;
font-size: 20px;
color: black;
left: 10px;
top: -5px;
}
<!DOCTYPE html>
<html>
<head>
<title>Meu Site</title>

<link href='https://fonts.googleapis.com/css?family=Raleway:200' rel='stylesheet' type='text/css'>



</head>
<body>


<div id="site">
<img id="fundo_site" src="http://i.stack.imgur.com/gINau.jpg.jpg">
<a id="abrir" style="position:absolute;"> <img id="bt_menu" src="img/bt.png"></a>
</div>



<div id="menu">
<p id="fechar" style="position:absolute;"><strong>X</strong></p>
<img id="img_menu" src="http://i.stack.imgur.com/DiAvt.jpg.jpg">
</div>

</body>
</html>

最佳答案

如果你想要的是将菜单固定在一个位置,这样即使你滚动它也总是可见的,你需要做的是将它的位置从 absolute 更改为 fixed 这样就可以了:

var nav = document.getElementById("menu");
var showMenu = document.getElementById("abrir");
var hideMenu = document.getElementById("fechar");
showMenu.addEventListener("click", function () {
nav.classList.add("show");
});

hideMenu.addEventListener("click", function () {
nav.classList.remove("show");
});
html, body{
padding: 0;
margin: 0;
width: 100%;
height: 100%;
}

#site{
position: absolute;
width: 100%;
height: 500px;

}

#fundo_site{
position: absolute;
width: 100%;
height: auto;
}

#menu{
/* LINE CHANGED */
position: fixed;
left: -292px;
height:100%;
width:15%;
transition: left 0.5s linear;
}

#img_menu{
width: 100%;
height: 100%;
}

#menu.show{
left:0;
}

#bt_menu{
width:20%;
height:20%;
margin: 15px;
}


p{
font-family: 'Raleway', sans-serif;
font-size: 20px;
color: black;
left: 10px;
top: -5px;
}
<!DOCTYPE html>
<html>
<head>
<title>Meu Site</title>

<link href='https://fonts.googleapis.com/css?family=Raleway:200' rel='stylesheet' type='text/css'>



</head>
<body>


<div id="site">
<img id="fundo_site" src="http://i.stack.imgur.com/gINau.jpg.jpg">
<a id="abrir" style="position:absolute;"> <img id="bt_menu" src="img/bt.png"></a>
</div>



<div id="menu">
<p id="fechar" style="position:absolute;"><strong>X</strong></p>
<img id="img_menu" src="http://i.stack.imgur.com/DiAvt.jpg.jpg">
</div>

</body>
</html>

关于html - 如何留下伴随滚动条的菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35348680/

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