0

If you look at my code pen my code pen and scroll down to the bottom of the page, and click a drop-down link the view jumps?

var nav = $('#menu > ul > li');
nav.find('li').hide();
nav.click(function () {
    nav.not(this).find('li').hide();
    $(this).find('li').slideToggle();
});

function openNav() {
    document.getElementById("open").style.display = "none";
    document.getElementById("close").style.display = "block";
    $('#menu ul').slideToggle();
}

function closeNav() {
    document.getElementById("open").style.display = "block";
    document.getElementById("close").style.display = "none";
    $('#menu ul').slideToggle();
}
Yehor Androsov
  • 4,885
  • 2
  • 23
  • 40
hydrix85
  • 49
  • 1
  • 5
  • Does this answer your question? [How do I stop a web page from scrolling to the top when a link is clicked that triggers JavaScript?](https://stackoverflow.com/questions/1601933/how-do-i-stop-a-web-page-from-scrolling-to-the-top-when-a-link-is-clicked-that-t) – Yehor Androsov Nov 25 '20 at 17:10
  • can you please give me the solution I don't know jQuery – hydrix85 Nov 25 '20 at 17:32
  • there are many solutions on the link that don't require jquery. please put some effort into reading small chunk of text. In fact, your issue is not related to jquery at all – Yehor Androsov Nov 25 '20 at 17:49
  • sorry I Found it thank you – hydrix85 Nov 25 '20 at 18:06

1 Answers1

0

Here is the update to your code:

//...
nav.click(function (e) {
    e.preventDefault();
    nav.not(this).find('li').hide();
    $(this).find('li').slideToggle();
});
//...

Explanation: you have to pass the self reference and use the preventDetault() method to override the default behavior.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Si8
  • 9,141
  • 22
  • 109
  • 221