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

标签 javascript html scroll

* {
	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/

相关文章:

javascript - 有什么属性可以确保 HTML 表单无法提交吗?

javascript - AngularJS - 预加载应用程序设置

javascript - jQuery 表单验证不验证

javascript - 如何在某些标签(数字)后插入 Div 并创建 Rest 父级?

javascript - 强制鼠标滚轮滚动到最后一部分

javascript - Angular-modal-service 不显示模态

javascript - 在旧项目中工作并找到我不理解的代码

html - 使用日期输入删除 vuetify v-text 字段上的向下箭头

javascript - 自动滚动到最近的div

html - <div> 位置 :absolute; bottom: 0; not working as expected in IE7