|
1 | 1 | /* The initial version is taken from |
2 | 2 | https://github.com/pytorch/pytorch_sphinx_theme |
3 | 3 | under MIT license |
| 4 | +rewrite to remove jQuery by ChatGPT |
4 | 5 | */ |
5 | 6 | window.mobileMenu = { |
6 | 7 | bind: function () { |
7 | | - $("[data-behavior='open-mobile-menu']").on("click", function (e) { |
8 | | - e.preventDefault(); |
9 | | - $(".mobile-main-menu").addClass("open"); |
10 | | - $("body").addClass("no-scroll"); |
11 | | - |
12 | | - mobileMenu.listenForResize(); |
13 | | - }); |
14 | | - |
15 | | - $("[data-behavior='close-mobile-menu']").on("click", function (e) { |
16 | | - e.preventDefault(); |
17 | | - mobileMenu.close(); |
18 | | - }); |
| 8 | + document |
| 9 | + .querySelectorAll("[data-behavior='open-mobile-menu']") |
| 10 | + .forEach(function (element) { |
| 11 | + element.addEventListener("click", function (e) { |
| 12 | + e.preventDefault(); |
| 13 | + document.querySelector(".mobile-main-menu").classList.add("open"); |
| 14 | + document.body.classList.add("no-scroll"); |
| 15 | + |
| 16 | + mobileMenu.listenForResize(); |
| 17 | + }); |
| 18 | + }); |
| 19 | + |
| 20 | + document |
| 21 | + .querySelectorAll("[data-behavior='close-mobile-menu']") |
| 22 | + .forEach(function (element) { |
| 23 | + element.addEventListener("click", function (e) { |
| 24 | + e.preventDefault(); |
| 25 | + mobileMenu.close(); |
| 26 | + }); |
| 27 | + }); |
19 | 28 | }, |
20 | 29 |
|
21 | 30 | listenForResize: function () { |
22 | | - $(window).on("resize.ForMobileMenu", function () { |
23 | | - if ($(this).width() > 768) { |
| 31 | + function resizeHandler() { |
| 32 | + if (window.innerWidth > 768) { |
24 | 33 | mobileMenu.close(); |
25 | 34 | } |
26 | | - }); |
| 35 | + } |
| 36 | + |
| 37 | + window.addEventListener("resize", resizeHandler); |
| 38 | + |
| 39 | + // Store the handler so it can be removed later |
| 40 | + this.resizeHandler = resizeHandler; |
27 | 41 | }, |
28 | 42 |
|
29 | 43 | close: function () { |
30 | | - $(".mobile-main-menu").removeClass("open"); |
31 | | - $("body").removeClass("no-scroll"); |
32 | | - $(window).off("resize.ForMobileMenu"); |
| 44 | + document.querySelector(".mobile-main-menu").classList.remove("open"); |
| 45 | + document.body.classList.remove("no-scroll"); |
| 46 | + |
| 47 | + // Remove the resize event listener |
| 48 | + if (this.resizeHandler) { |
| 49 | + window.removeEventListener("resize", this.resizeHandler); |
| 50 | + this.resizeHandler = null; |
| 51 | + } |
33 | 52 | }, |
34 | 53 | }; |
35 | | -$(document).ready(function () { |
| 54 | + |
| 55 | +document.addEventListener("DOMContentLoaded", function () { |
36 | 56 | mobileMenu.bind(); |
37 | 57 | }); |
0 commit comments