Кнопка раскрывающегося списка не работает при первом нажатииCSS

Разбираемся в CSS
Ответить
Anonymous
 Кнопка раскрывающегося списка не работает при первом нажатии

Сообщение Anonymous »

Я создаю веб-сайт для поездов и сделал раскрывающуюся кнопку для кнопки, при нажатии которой отображаются все поезда на веб-сайте, но по какой-то причине в моей версии для планшета кнопка просто не работает при первом нажатии, но после этого все ок. Это действительно странная проблема, и я просто не знаю, что делать. У меня совершенно нет опыта работы с JS, и у меня нет времени изучать его для этого проекта, но я очень хочу его использовать. На этом изображении вы можете видеть, что сначала обнаруживается кнопка меню, которая отображает панель навигации, а затем, когда я нажимаю кнопку раскрывающегося списка поездов, она обнаруживается, но все равно не запускает функцию

Код: Выделить всё

function TrainDropdown() {
const trainMenu = document.getElementById("trains");
trainMenu.classList.toggle("show");
console.log("TrainDropdown toggled:", trainMenu.classList.contains("show"));
}

function Menu() {
const navBar = document.getElementById("phone-nav-bar");
navBar.classList.toggle("show-nav-bar");
}
function MobileTrainDropdown() {
const trainElements = document.getElementById("tablet-trains");
if (trainElements.style.display === "none") {
trainElements.style.display = "flex";
} else {
trainElements.style.display = "none";
}
}

window.addEventListener("click", function (event) {
console.log("Window click detected on:", event.target);

// Close train dropdown if clicked outside the dropdown button
if (!event.target.closest('.dropdown-button')) {
const dropdowns = document.getElementsByClassName("train-elements");
for (let i = 0; i < dropdowns.length; i++) {
const openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
console.log("Closed train dropdown:", openDropdown);
}
}
}

// Close mobile nav menu if clicked outside menu button and nav bar
const navBar = document.getElementById("phone-nav-bar");
const menuButton = document.querySelector('.menu-button');
if (navBar && menuButton && !menuButton.contains(event.target) && !navBar.contains(event.target)) {
navBar.classList.remove("show-nav-bar");
console.log("Closed mobile nav menu.");
}
});

Код: Выделить всё

body {
margin: 0;
}

/*top-bar properties*/
.top-bar {
display: flex;
align-items: center;
height: 10vh;
background-color: #A7C4FF;
}

.branding {
display: flex;
align-items: center;
}

.branding img {
height: 12vh; /* Set a fixed height for the logo */
width: auto; /* Maintain aspect ratio */
}

/*menu toggle properties*/
.menu-button {
display: none; /* Hidden on larger screens */
}

.mobile-phone-branding {
display: none; /* Hidden on larger screens */
}
/*navigation bar properties*/
.nav-bar {
display: flex;
flex: 1;
align-items: center;
margin-left: 10vw;
}

/*buttons*/
.nav-button, .dropdown-button{
width: 100%;
height: 8vh;
line-height: 8vh;
margin-right: 5px;
font-family: Unbounded;
font-size: 1vw;
text-align: center;
text-decoration: none;
background-color: #A7C4FF;
border: none;
border-radius: 40px;
transition: background-color 0.4s ease-in-out;
}

.nav-button:hover, .dropdown-button:hover{
background-color: #8FA9FF;
}

/*train-dropdown and all its children properties*/
.train-dropdown{
position: relative;
margin-right: 5px;
width: 100%;
}

.train-elements{
display: none;
width: 100%;
position: absolute;
top: 10vh;
background-color: #7f9dfd;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
border-radius: 0.8vw;
z-index: 1;
}

.train-elements a{
padding: 1em;
color: black;
text-decoration: none;
}

.train-elements a:hover{
background-color: #567dff;
}

.train-elements #first-element:hover{
border-top-left-radius: 0.8vw;
border-top-right-radius: 0.8vw;
}

.train-elements #last-element:hover{
border-bottom-left-radius: 0.8vw;
border-bottom-right-radius: 0.8vw;
}

#mobile-phone-train-button{
display: none;
}

/*Class that JS uses to show train-elements*/
.show{
display: flex;
flex-direction: column;
}

@media only screen and (max-width: 1300px){
/*hiding nav-bar to save space and make it into a dropdown*/
.nav-bar{
display: none;
}

.menu-button{
display: flex;
height: 8.5vh;
width: 8.5vh;
justify-content: center;
align-items: center;
border-radius: 20px;
border: none;
background-color: #A7C4FF;
}

.menu-button img {
width: 3vw; /* Set a fixed width */
height: auto; /* Maintain aspect ratio */
max-width: none; /* Prevent scaling */
}

.menu-button:hover{
background-color: #8FA9FF;
}

/*button properties*/
.nav-button, .dropdown-button{
border-radius: 0;
background-color: #94b7ff;
}

.mobile-phone-nav-bar{
box-shadow: 8px 16px 16px 0px rgba(0,0,0,0.2);
background-color: #A7C4FF;
width: 300px;
position: absolute; /* Position below the top bar */
top: 10vh; /* Position below the top bar */
left: 0; /* Align to the left */
width: 300px; /* Full width */
display: none; /* Hidden by default */
}

/*train dropdown properties*/
.tablet-train-elements{
display: none;
width: 100%;
flex-direction: column;
position: absolute;
background-color: #7f9dfd;
}

.tablet-train-elements a{
padding: 12px 16px;
color: black;
text-decoration: none;
}

.tablet-train-elements a:hover{
background-color: #567dff;
}

/*hides the button i will be using for phone mode that
substitutes the dropdown button cause there is no space*/
#mobile-phone-train-button{
display: none;
}

/*a class JS uses to display the dropdown when pressing the button*/
.show-nav-bar{
display: flex;
flex-direction: column;
}
}

Код: Выделить всё









Железопътна страст


















[url=index.html]
[img]assets/images/logo.png[/img]
[/url]


[img]assets/images/menu.png[/img]




[url=index.html]Начало[/url]

[url=#trains]Влакове[/url]



Влакове



[url=#]Link 1[/url]

[url=#]Link 2[/url]

[url=#]Link 3[/url]



[url=#contacts]Контакти[/url]
[url=#aboutme]Повече за мен[/url]





[url=index.html]
[img]assets/images/logo.png[/img]
[/url]

[url=index.html]Начало[/url]



Влакове



[url=#]Link 1[/url]

[url=#]Link 2[/url]

[url=#]Link 3[/url]



[url=#contacts]Контакти[/url]
[url=#aboutme]Повече за мен[/url]









Подробнее здесь: https://stackoverflow.com/questions/791 ... irst-press
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «CSS»