gpt4 book ai didi

javascript - 单击 anchor 标记时自动转到页面顶部

转载 作者:行者123 更新时间:2023-12-03 16:37:13 25 4
gpt4 key购买 nike

我有一个问题,

我将页面滚动到中间。当我点击右侧的a标签显示div时,页面会自动滚动到页面顶部。我想阻止这种情况,

请帮忙!谢谢

我的源代码:https://codepen.io/lhthuong181/pen/OJPxpxB

-html

<div id="container">
<a href="#">
<img
onclick="showCustom()"id="setting"src="https://images.unsplash.com/photo157530259766162aae7fe1447?
ixlib=rb-
1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ">
</a>
<div id="custom" class="custom-1">

</div>

</div>

-CSS

  #custom {
position: fixed;
width: 100px;
height: 200px;
right: 20px;
top: 100px;
background-color: white;
transition: 0.8s;
border-radius: 20px;

}
.custom-1 {

right: -150px !important;


}
#setting {
position: fixed;
width: 30px;
z-index: 99;
right: 30px;
top: 50px;
background-color: rgb(255, 255, 255);
padding: 5px;
border-radius: 15px;
}

#container {
background-color:black;
width:100%;
height:2000px;
}

-Js

var btnsetting = document.getElementById("setting");
function showCustom() {
var x = document.getElementById("custom");
if (x.className == "" ) {

x.className = "custom-1";

}

else{


x.className = "";


}}

https://codepen.io/lhthuong181/pen/OJPxpxB

最佳答案

欢迎来到SO

href 中使用 JavaScript:Void(0); 而不是 #

替换

 <a href="JavaScript:Void(0);">
<img
onclick="showCustom()"id="setting"src="https://images.unsplash.com/photo157530259766162aae7fe1447?
ixlib=rb-
1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ">
</a>

引用 https://www.quackit.com/javascript/tutorial/javascript_void_0.cfm

关于javascript - 单击 anchor 标记时自动转到页面顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59541085/

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