gpt4 book ai didi

javascript - 调整div大小时如何保持用户的滚动位置

转载 作者:行者123 更新时间:2023-11-28 02:22:55 25 4
gpt4 key购买 nike

我想为我正在处理的网站做一个很酷的菜单效果。我有一个 div 作为主要内容的部分。当用户打开菜单时,主要内容 div 将调整大小并移开,显示菜单。但是,当我使用我编写的代码执行此操作时,它总是会丢失我在页面上的滚动位置。有什么办法可以在页面缩小和再次展开时保持我在页面上的位置?以下是我所拥有的。提前致谢!

function shrinkPage() {

var element = document.getElementById("mock-body");
element.classList.toggle("mock-body-on-burger");

var z = document.getElementById("mock-body-container");
z.classList.toggle("mock-body-container-on-burger");

var x = document.getElementById("body");
x.classList.toggle("body-on-burger");
};
body {
margin: 0;
background:#000;
}

.body-on-burger {
max-width: 100%;
overflow-x:hidden;
}

.mock-body-container{
height:100vh;
}

.mock-body-container-on-burger {
height:100vh;
transform: scale(0.4) translate(130%);
overflow: hidden;
}

.mock-body-size-change{
overflow: scroll;
}

.mock-body {
position:relative;
background: #fff;
margin-left: 50px;
}

.container {
position: fixed;
height:50px;
width:50px;
cursor: pointer;
}

.container #icon {
width: 16px;
height: 8px;
position: relative;
margin: 0px auto 0;
top: 40%;
}

.container #icon .bars {
height: 1px;
background: #fff;
}

.myDiv {
height:500px;
}

.one {
background:red;
}

.two {
background:green;
}

.three {
background:blue;
}
<body id="body">



<div class="menu-activator" onclick="shrinkPage()">
<div class="container usd">
<div id="icon">
<div class="bars first"></div>
</div>
</div>
</div>


<div id="mock-body-container" class="mock-body-container">
<div id="mock-body" class="mock-body">
<div class="myDiv one"></div>
<div class="myDiv two"></div>
<div class="myDiv three"></div>
</div>
</div>
</body>

最佳答案

请看下面的片段。注意 overflow 属性是如何使用的。

您必须滚动 mock-body-container 才能保持其滚动位置。您正在滚动 body,因此当您缩放 mock-body-container 时,body 中没有任何内容可滚动,并且您松开了滚动位置.

function shrinkPage() {

var element = document.getElementById("mock-body");
element.classList.toggle("mock-body-on-burger");

var z = document.getElementById("mock-body-container");
z.classList.toggle("mock-body-container-on-burger");

var x = document.getElementById("body");
x.classList.toggle("body-on-burger");
};
body {
margin: 0;
background:#000;
}

.body-on-burger {
max-width: 100%;
overflow-x:hidden;
}

.mock-body-container{
height:100vh;
overflow:auto;
}

.mock-body-container-on-burger {
height:100vh;
transform: scale(0.4) translate(130%);
}

.mock-body-size-change{
overflow: scroll;
}

.mock-body {
position:relative;
background: #fff;
margin-left: 50px;
}

.container {
position: fixed;
height:50px;
width:50px;
cursor: pointer;
}

.container #icon {
width: 16px;
height: 8px;
position: relative;
margin: 0px auto 0;
top: 40%;
}

.container #icon .bars {
height: 1px;
background: #fff;
}

.myDiv {
height:500px;
}

.one {
background:red;
}

.two {
background:green;
}

.three {
background:blue;
}
<body id="body">



<div class="menu-activator" onclick="shrinkPage()">
<div class="container usd">
<div id="icon">
<div class="bars first"></div>
</div>
</div>
</div>


<div id="mock-body-container" class="mock-body-container">
<div id="mock-body" class="mock-body">
<div class="myDiv one"></div>
<div class="myDiv two"></div>
<div class="myDiv three"></div>
</div>
</div>
</body>

关于javascript - 调整div大小时如何保持用户的滚动位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48104296/

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