Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically go to the top of the page when clicking the anchor tag

I have a problem,

I scroll the page to the middle. When I click on the a tag on the right to show the div, the page will automatically scroll to the top of the page. I want to prevent this,

Please help ! THanks

My src code: 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

like image 908
Hoài Avatar asked Dec 08 '25 00:12

Hoài


2 Answers

Welcome to SO

Use JavaScript:Void(0); rather then # in href

Replace

 <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>

Ref https://www.quackit.com/javascript/tutorial/javascript_void_0.cfm

like image 94
Awais Avatar answered Dec 10 '25 13:12

Awais


You should remove the "a" element and use cursor: pointer for the same effect.

HTML:

    <div id="container">

                <img onclick="showCustom()" id="setting" src="https://images.unsplash.com/photo-1575302597661-62aae7fe1447?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ">

            <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;
    cursor: pointer;
  }

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

Showing here https://codepen.io/jeroldlin/pen/rNaGyoW

like image 30
Jerry Lin Avatar answered Dec 10 '25 15:12

Jerry Lin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!