gpt4 book ai didi

javascript - 使 HTML 元素沿着特定滚动条移动,而不是沿着主滚动条移动

转载 作者:行者123 更新时间:2023-11-28 03:08:43 25 4
gpt4 key购买 nike

* {
box-sizing: border-box;
}

body {
font-family: Roboto, Arial, Helvetica, sans-serif;
background-color: #fffc2e;
}

#first_screen {
text-align: center;
padding: 10%;
color: black;
font-size: 50px;
height: 100vh;
}

/*Navigation Button*/
#tutorialnav {
z-index: 1;
height:100%;
width: 0;
position: fixed;
background-color: #111;
top: 0;
left: 0;
overflow-x: hidden;
transition: 0.2s;
padding-top: 80px;
filter: drop-shadow(5px 0px 1px black);
}

#tutorialnav a {
text-decoration: none;
display: block;
transition: 0s;
color: white;
white-space: nowrap;
}

.majornav {
padding: 8px 8px 8px 32px;
font-size: 25px;
}

.minornav {
padding: 4px 8px 4px 64px;
font-size: 16px;
}

#tutorialnav a:hover {
color: yellow;
}

#navbtn {
color: black;
position: sticky;
z-index: 2;
font-size: 36px;
cursor: pointer;
top: 10px;
left: 20px;
transition: 0.7s;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}

.noselect {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/*End Navigation Button*/
<!DOCTYPE html>
<html>

<title>3D Graphics Tutorial Homepage</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="UTF-8">
<meta name="description" content="3d Graphics Web Tutorials [Homepage]">
<meta name="author" content="Harsha Boyilla">
<meta name="keywords" content="3D, 3d, 3d graphics, 3D graphics, graphics, tutorial">
<link rel="stylesheet" href="styles.css">

<body>
<!-- Navigation Menu -->
<div id="navigationmenu" class="noselect">
<button id="navbtn">&#9776;</button>
<nav id="tutorialnav">
<a href="temp" class="majornav">Part 1</a>
<a href="temp" class="minornav">Section 1</a>
<a href="temp" class="minornav">Section 2</a>
<a href="temp" class="majornav">Part 2</a>
<a href="temp" class="minornav">Section 1</a>
<a href="temp" class="minornav">Section 2</a>
<a href="temp" class="majornav">Part 3</a>
<a href="temp" class="minornav">Section 1</a>
<a href="temp" class="minornav">Section 2</a>
<a href="temp" class="majornav">Part 4</a>
<a href="temp" class="minornav">Section 1</a>
<a href="temp" class="minornav">Section 2</a>
</nav>
</div>
<script type="text/javascript">
var navIsOpen = false;
window.onload = function()
{
document.getElementById("navbtn").onclick = function()
{
if(!navIsOpen)
{
document.getElementById("tutorialnav").style.width = "250px";
document.getElementById("navbtn").style.color = "white";
navIsOpen = true;
}

else if(navIsOpen)
{
document.getElementById("tutorialnav").style.width = "0px"
document.getElementById("navbtn").style.color = "black";
navIsOpen = false;
}
return false;
}
}
</script>

<article id="first_screen">
<p>Welcome to my website!</p>
<p>Placeholder text is placeholder text</p>
</article>
</body>

</html>

在我正在创建的网站上,我有一个侧边栏,用作网站的导航菜单。左上角有一个按钮,可以打开和关闭滚动条。如果侧边栏有太多项目,则会出现滚动条以便滚动侧边栏。我遇到的问题是,打开和关闭的按钮不会与侧边栏一起滚动,而是在我滚动主页时​​滚动。有没有办法来解决这个问题?这是我迄今为止拥有的 HTML 和 JavaScript。

    <div id="navigationmenu" class="noselect">
<button id="navbtn" onclick="openNav()">&#9776;</button>
<nav id="tutorialnav">
<a href="temp" class="majornav">Part 1</a>
<a href="temp" class="minornav">Section 1</a>
<a href="temp" class="minornav">Section 2</a>
<a href="temp" class="majornav">Part 2</a>
<a href="temp" class="minornav">Section 1</a>
<a href="temp" class="minornav">Section 2</a>
<a href="temp" class="majornav">Part 3</a>
<a href="temp" class="minornav">Section 1</a>
<a href="temp" class="minornav">Section 2</a>
<a href="temp" class="majornav">Part 4</a>
<a href="temp" class="minornav">Section 1</a>
<a href="temp" class="minornav">Section 2</a>
</nav>
</div>
<script type="text/javascript">
var navIsOpen = false;
window.onload = function()
{
document.getElementById("navbtn").onclick = function()
{
if(!navIsOpen)
{
document.getElementById("tutorialnav").style.width = "250px";
document.getElementById("navbtn").style.color = "white";
navIsOpen = true;
}

else if(navIsOpen)
{
document.getElementById("tutorialnav").style.width = "0px"
document.getElementById("navbtn").style.color = "black";
navIsOpen = false;
}
return false;
}
}
</script>

编辑:哎哟忘记了 CSS...就在这里

#tutorialnav {
z-index: 1;
height:100%;
width: 0;
position: fixed;
background-color: #111;
top: 0;
left: 0;
overflow-x: hidden;
transition: 0.2s;
padding-top: 80px;
filter: drop-shadow(5px 0px 1px black);
}

#tutorialnav a {
text-decoration: none;
display: block;
transition: 0s;
color: white;
white-space: nowrap;
}

.majornav {
padding: 8px 8px 8px 32px;
font-size: 25px;
}

.minornav {
padding: 4px 8px 4px 64px;
font-size: 16px;
}

#tutorialnav a:hover {
color: yellow;
}

#navbtn {
color: black;
position: sticky;
z-index: 2;
font-size: 36px;
cursor: pointer;
top: 10px;
left: 20px;
transition: 0.7s;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}

.noselect {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;

编辑 2:找到了一个解决方案 - 发布在下面。不幸的是,我需要 48 小时才能检查自己的解决方案,因此该线程将开放更长的时间。

最佳答案

好吧希望这就是你想要的。当它打开时,您需要在导航栏中有一个单独的滚动按钮,因为第一个按钮是基于整个屏幕的粘性按钮。

除此之外,我将按钮标签更改为 div,这样它们看起来就很漂亮。看看 onclick 函数是如何工作的。您设置按钮的方式正在寻找

函数 openNav(){...}在你的脚本标签中,但它不在那里,祝你好运。

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
box-sizing: border-box;
}

body {
font-family: Roboto, Arial, Helvetica, sans-serif;
background-color: #fffc2e;
}

#first_screen {
text-align: center;
padding: 10%;
color: black;
font-size: 50px;
height: 100vh;
}
/*Navigation Button*/

#tutorialnav {
z-index: 1;
height: 100%;
width: 0;
position: fixed;
background-color: #111;
top: 0;
left: 0;
overflow-x: hidden;
transition: 0.2s;
padding-top: 80px;
filter: drop-shadow(5px 0px 1px black);
}

#tutorialnav a {
text-decoration: none;
display: block;
transition: 0s;
color: white;
white-space: nowrap;
}

.majornav {
padding: 8px 8px 8px 32px;
font-size: 25px;
}

.minornav {
padding: 4px 8px 4px 64px;
font-size: 16px;
}

#tutorialnav a:hover {
color: yellow;
}

#navbtn {
color: black;
position: sticky;
z-index: 2;
font-size: 36px;
cursor: pointer;
top: 10px;
left: 20px;
transition: 0.7s;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}

#navbtncls {
color: blanchedalmond;
}

.noselect {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
top: 10px;
left: 10px;
position: -webkit-sticky;
position: sticky;
}
</style>
</head>

<body>
<div id="navigationmenu" class="noselect">

<div id="navbtnopn" onclick="openNav()">&#9776;</div>
<nav id="tutorialnav">

<div id="navbtncls" onclick="closeNav()">&#9776;</div>
<a href="#temp" class="majornav">Part 1</a>
<a href="#temp" class="minornav">Section 1</a>
<a href="#temp" class="minornav">Section 2</a>
<a href="#temp" class="majornav">Part 2</a>
<a href="#temp" class="minornav">Section 1</a>
<a href="#temp" class="minornav">Section 2</a>
<a href="#temp" class="majornav">Part 3</a>
<a href="#temp" class="minornav">Section 1</a>
<a href="#temp" class="minornav">Section 2</a>
<a href="#temp" class="majornav">Part 4</a>
<a href="#temp" class="minornav">Section 1</a>
<a href="#temp" class="minornav">Section 2</a>
</nav>
</div>
<br><br><br><br><br><br><br>
<br><br><br><br><br><br><br>
<br><br><br><br><br><br><br>
<br><br><br><br><br><br><br>
<br><br><br><br><br><br><br>
<p>hello</p>
<script type="text/javascript">
var navIsOpen = false;


function openNav() {
document.getElementById("tutorialnav").style.width = "250px";
navIsOpen = true;
}

function closeNav() {
document.getElementById("tutorialnav").style.width = "0px"
navIsOpen = false;
}
</script>
</body>

</html>

关于javascript - 使 HTML 元素沿着特定滚动条移动,而不是沿着主滚动条移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60359246/

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