gpt4 book ai didi

javascript - 如何保持列表框中滚动条的滚动位置

转载 作者:太空宇宙 更新时间:2023-11-03 19:30:34 26 4
gpt4 key购买 nike

After Submitting the form the scroll goes up how will retain the position of the scroll ? this code does not work can anyone help thank you in advance or advice to do.

      <script type="text/javascript" src="js/jquery-2.1.4.min.js"></script>

<style>
.container {
border:2px solid #ccc;
width:600px;
height: 1000px;
overflow-y: scroll;
}
</style>

<script>
var scrollPos=$("form").scrollTop();
onclick="javascript: submit()

</script>
<form method="post" name="form">

<div id="Html2" style="position:absolute;left:0x;top:60px;width:250px;height:100px;z-index:12" class="container">
<input onclick="javascript: submit()" type="checkbox">Checkbox1
<br>
<input onclick="javascript: submit()" type="checkbox">Checkbox1
<br>
<input onclick="javascript: submit()" type="checkbox">Checkbox1
<br>
<input onclick="javascript: submit()" type="checkbox">Checkbox1
<br>
<input onclick="javascript: submit()" type="checkbox">Checkbox1
<br>
<input onclick="javascript: submit()" type="checkbox">Checkbox1
<br>

<script> $("form").scrollTop(scrollPos);</script>

</div>
</form>


</script>
</div>
</form>

最佳答案

这就是我在纯 Javascript 中的做法:

var elem = document.querySelector(".container");
elem.onscroll = function () {
sessionStorage.setItem('scroll', elem.scrollTop);
};

if (sessionStorage.getItem('scroll') != null) {
elem.scrollTop = parseInt(sessionStorage.getItem('scroll'));
}

Here is the JSFiddle demo

关于javascript - 如何保持列表框中滚动条的滚动位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31964907/

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