#javascript #html #css
Вопрос:
Я создаю этот веб-сайт для компании для моего студенческого кооператива, и я сделал этот заголовок для своего веб-сайта, и когда я его создал, он работал совершенно нормально. Теперь, когда я проверяю свой телефон, я нажимаю кнопку «Службы» для своего выпадающего меню, и оно не открывается, но когда я пытаюсь использовать инструмент Google chrome dev responsive, я могу нажать на выпадающее меню, но я также не могу его открыть. Я понимаю, что у меня много кода, написанного в моем файле CSS, у меня есть вещи, помеченные как заголовок/домашняя страница.
Моя Проблема:
Когда я нажимаю на свою отзывчивую навигационную панель, когда она изменяется в меньшем соотношении. Я нажимаю на панель, и мое навигационное меню открывается, но когда я нажимаю на выпадающее меню «Мои услуги», оно не открывается, хотя оно работает с адаптивными инструментами разработки Google Chrome. Но когда я нажимаю кнопку «Услуги», и она работает, выпадающий список не исчезнет, когда я нажму его снова, как я могу это исправить? Большая часть моей навигационной панели помечена как «Заголовок» в моем css-файле. Я решил поместить весь свой файл, так как, возможно, что-то не так с его написанием, но я понятия не имею, так ли это?
HTML
<!DOCTYPE html>
<html>
<head>
<!-- Meta Data *IGNORE* !-->
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link href="https://unpkg.com/ionicons@4.5.10-0/dist/css/ionicons.min.css" rel="stylesheet">
<link rel="stylesheet" href="css1/headerNewCSS.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.15.3/css/all.css" integrity="sha384-SZXxX4whJ79/gErwcOYf zWLeJdY/qpuqC4cAa9rOGUstPomtqpuNWT9wdPEn2fk" crossorigin="anonymous">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Montserratamp;display=swap" rel="stylesheet">
<!-- Header !-->
<header>
<div style="background-color: #1E90FF;" class="container-fluid">
<nav>
<div class="menu-icons">
<i class="fas fa-bars"></i>
</div>
<a href="index.php" class="logo">
<img class="logo" src="images/logo_trans.png" alt="logo" style="height: 75px;">
</a>
<ul class="nav-list">
<!-- Normal listing buttons !-->
<li>
<a href="index.php">Home</a>
</li>
<li>
<a class="dropdown">Services
<i class="fas fa-sort-down"></i>
</a>
<!-- Drop Down List !-->
<ul id="drop-sub" class="sub-menu">
<li>
<a href="businessPrintingPage1.php">Business Printing</a>
</li>
<li>
<a href="canvasPrintingPage1.php">Canvas Printing</a>
</li>
<li>
<a href="embroideryPage1.php">Embroidery</a>
</li>
<li>
<a href="engravingPage1.php">Engraving</a>
</li>
<li>
<a href="signsPage1.php">Signs</a>
</li>
<li>
<a href="apperalPage1.php">Custom Apperal</a>
</li>
</ul>
</li>
<!-- Normal listing buttons !-->
<li>
<a href="Promotions.php">Promotions</a>
</li>
<li>
<a href="partners.php">Partners</a>
</li>
<li>
<a href="aboutUs.php">About Us</a>
</li>
<li>
<a href="contactUs.php">Contact Us</a>
</li>
<!-- Cart !-->
<li class="move-right">
<img class="cart" style="float: right;" src="images/new_cart.png" alt="Cart">
</li>
</ul>
</nav>
</div>
</header>
<!-- Javascript for making a new navbar image for responsiveness !-->
<script src="javascript/script.js"></script>
CSS
/* DONT TOUCH ANY OF THESE INFORMATION */
*,
*::before,
*::after {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
overflow-x: hidden;
}
html {
font-family: "Montserrat", sans-serif;
font-size: 12px;
scroll-behavior: smooth;
}
a {
text-decoration: none;
}
ul {
list-style: none;
}
/* END OF UNTOUCHABLE INFORMATION */
/*-------------------------------------------- STYLING FOR HEADER ----------------------------------------------*/
header {
width: 100%;
position: sticky;
top: 0;
left: 0;
z-index: 999;
}
.container {
width: 100%;
max-width: 117rem;
margin: 0 auto;
padding: 0 1.5rem;
}
.menu-icons {
color: #eee;
font-size: 4rem;
position: absolute;
top: 50%;
right: 2rem;
transform: translateY(-50%);
cursor: pointer;
z-index: 1500;
display: none;
}
nav {
display: flex;
align-items: center;
width: 100%;
height: 8rem;
border-bottom: 1px solid rgba(225, 225, 225, 0.1);
}
.logo {
display: flex;
margin-right: 5rem;
}
.nav-list {
display: flex;
width: 100%;
align-items: center;
}
.nav-list li {
line-height: 8rem;
position: relative;
}
.sub-menu li {
line-height: 4rem;
}
.nav-list a {
display: block;
color: white;
background-color: #1e90ff;
padding: 0 1.5rem;
font-size: 1.4rem;
text-transform: uppercase;
transition: color 650ms;
transition: background-color 650ms;
}
.nav-list a:hover {
color: black;
background-color: yellow;
}
.cart {
height: 30px;
width: 40px;
cursor: pointer;
margin-right: 15px;
margin-bottom: 5px;
}
.btn {
padding: 1.3rem;
display: inline-block;
background-color: #1e90ff;
border: 2px solid #1e90ff;
border-radius: 5rem;
transition: background-color 650ms;
}
.btn:hover {
color: black;
background-color: yellow;
}
.sub-menu {
width: 20rem;
display: block;
position: absolute;
border-top: 3px solid #00bfff;
z-index: 100;
top: 16rem;
transition: all 650ms ease;
opacity: 0;
visibility: hidden;
}
.sub-menu::before {
content: "";
position: absolute;
top: -2.5rem;
left: 3rem;
border: 1.2rem solid transparent;
border-bottom-color: #00bfff;
}
.sub-menu .sub-menu::before {
top: 0.9rem;
left: -2.5rem;
border: 1.2rem solid transparent;
border-right-color: #00bfff;
}
.sub-menu .sub-menu {
border-top: none;
border-left: 3px solid #00bfff;
top: 0;
left: 160%;
}
.nav-list li:hover > .sub-menu {
top: 8rem;
opacity: 1;
visibility: visible;
}
.sub-menu li:hover > .sub-menu {
top: 0;
left: 100%;
}
li.move-right {
margin: auto 0 auto auto;
line-height: initial;
transition: color 650ms;
transition: background-color 650ms;
}
/*-------------------------------------------- STYLING FOR HEADER END ----------------------------------------------*/
/*-------------------------------------------- RESPONSIVENESS FOR HEADER ----------------------------------------------*/
@media screen and (max-width: 1200px) {
.nav-list {
position: fixed;
top: 0;
left: 0;
height: 100vh;
display: none;
align-items: initial;
flex-direction: column;
background-color: #1e90ff;
z-index: 1000;
overflow-y: scroll;
}
.nav-list li {
line-height: 6rem;
}
.sub-menu {
position: initial;
border: 3px solid transparent;
border-left-color: #00bfff;
margin-left: 1rem;
max-height: 0;
display: none;
}
.sub-menu::before {
display: none;
}
.nav-list li:hover > .sub-menu {
visibility: hidden;
}
.sub-menu-vis {
opacity: 1;
display: block;
visibility: visible !important;
max-height: initial;
}
li.move-right {
margin: 0 auto 0 0;
line-height: initial;
}
.menu-icons {
display: block;
}
.fas fa-times {
display: none;
}
.dropdown{
cursor: pointer;
}
nav.active .fas fa-times {
display: block;
}
nav.active .fas fa-bars {
display: none;
}
nav.active .nav-list {
display: flex;
}
}
/*-------------------------------------------- RESPONSIVENESS FOR HEADER END ----------------------------------------------*/
/* ----------------------------- STYLING FOR TABLE -------------------------------------- */
/* Table container */
.tableContainer {
display: flex;
justify-content: center;
flex-direction: row;;
}
/* Contents of the table that are being style */
.tableContents {
display: flex;
justify-content: center;
text-align: left;
margin-top: 10px;
margin-bottom: 20px;
flex-direction: row;
}
.tableContents table {
border-collapse: collapse;
}
.tableContents th {
background-color: #1e90ff;
color: white;
border: 1px solid black;
padding: 8px;
padding-top: 12px;
padding-bottom: 12px;
font-size: 25px;
}
.tableContents td {
padding: 8px;
border: 1px solid black;
}
.tableContents tr {
background-color: #F0F8FF;
color: black;
border: 1px solid black;
font-size: 15px;
}
.tableContents tr:hover {
background-color: white;
}
table tr:nth-child(odd) {
background-color: #DCDCDC;
}
/* Fixing margins for phone */
.tablePhoneFix {
margin-left: 10px;
display: flex;
justify-content: center;
}
/* ----------------------------- STYLING FOR TABLE END -------------------------------------- */
/* ----------------------------- STYLING FOR HERO IMAGE BANNER -------------------------------------- */
.back-test {
height: 100vh;
background-image: linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5)), url(../images/heroImage.jpg);
background-attachment: fixed;
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}
/* ----------------------------- STYLING FOR HERO IMAGE BANNER END -------------------------------------- */
/*------------------------------------------- TABLE RESPONSIVENESS ----------------------------------------------*/
@media (max-width: 730px) {
.tableContainer {
display: flex;
justify-content: center;
flex-direction: column;
width: 90%;
}
.tableContents {
display: flex;
justify-content: center;
text-align: left;
margin-top: 10px;
margin-bottom: 20px;
flex-direction: column;
margin-left: 10px;
}
.tableContents th {
background-color: #1e90ff;
color: white;
border: 1px solid black;
padding: 8px;
padding-top: 12px;
padding-bottom: 12px;
font-size: 20px;
}
.tableContents tr {
background-color: #F0F8FF;
color: black;
border: 1px solid black;
font-size: 15px;
}
.tablePhoneFix {
margin-top: 10px;
margin-left: 0px;
display: flex;
justify-content: center;
}
.back-test {
height: 200vh;
background-image: linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5)), url(../images/heroImage.jpg);
background-attachment: fixed;
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}
}
/*------------------------------------------- TABLE RESPONSIVENESS END ----------------------------------------------*/
язык JavaScript
const selectElement = (element) => document.querySelector(element);
selectElement('.menu-icons').addEventListener('click', () => {
selectElement('nav').classList.toggle('active');
});
var drop = document.querySelector(".dropdown");
drop.addEventListener("click", function() {
var sub = document.querySelector("#drop-sub");
if(sub.classList.contains("sub-menu-vis")){
document.getElementById("drop-sub").classList.remove("sub-menu-vis");
}else{
document.getElementById("drop-sub").classList.add("sub-menu-vis");
}
});
Комментарии:
1. Ваш атрибут наведения не работает на телефонах, потому что вам нужно щелкнуть по сенсорному экрану.
2. Дело в том, что он работал, вам просто нужно было щелкнуть по нему, и он начал бы зависать, но теперь он не работает
3. Но на мобильных устройствах нет возможности отцепиться. Посмотрите, что я опубликовал ниже.
Ответ №1:
Вот что я сделал: это ссылка, потому что в фрагменте было много кода, который нужно было вставить. https://jsfiddle.net/Allan_StackoverFlow/stygwvx7/3/
Язык JavaScript:
(function() {
const selectElement = (element) => document.querySelector(element);
selectElement('.menu-icons').addEventListener('click', () => {
selectElement('nav').classList.toggle('active');
});
var drop = document.querySelector(".dropdown");
drop.addEventListener("click", function() {
var sub = document.querySelector("#drop-sub");
if(sub.classList.contains("sub-menu-vis")){
document.getElementById("drop-sub").classList.remove("sub-menu-vis");
}else{
document.getElementById("drop-sub").classList.add("sub-menu-vis");
}
});
})();
Есть место только для javascript. HTML и css были отредактированы.
Вы можете получить код оттуда.
Попробуй Это!
ОБНОВЛЕНИЕ: CSS, который я обновил
/*-------------------------------------------- RESPONSIVENESS FOR HEADER ----------------------------------------------*/
@media screen and (max-width: 1200px) {
.nav-list {
position: fixed;
top: 0;
left: 0;
height: 100vh;
display: none;
align-items: initial;
flex-direction: column;
background-color: #1e90ff;
z-index: 1000;
overflow-y: scroll;
}
.nav-list li {
line-height: 6rem;
}
.sub-menu {
position: initial;
border: 3px solid transparent;
border-left-color: #00bfff;
margin-left: 1rem;
max-height: 0;
display: none;
}
.sub-menu::before {
display: none;
}
.nav-list li:hover > .sub-menu {
visibility: hidden;
}
.sub-menu-vis {
opacity: 1;
display: block;
visibility: visible !important;
max-height: initial;
}
li.move-right {
margin: 0 auto 0 0;
line-height: initial;
}
.menu-icons {
display: block;
}
.fas fa-times {
display: none;
}
.dropdown{
cursor: pointer;
}
nav.active .fas fa-times {
display: block;
}
nav.active .fas fa-bars {
display: none;
}
nav.active .nav-list {
display: flex;
}
}
/*-------------------------------------------- RESPONSIVENESS FOR HEADER END ----------------------------------------------*/
const selectElement = (element) => document.querySelector(element);
selectElement('.menu-icons').addEventListener('click', () => {
selectElement('nav').classList.toggle('active');
});
var drop = document.querySelector(".dropdown");
drop.addEventListener("click", function() {
var sub = document.querySelector("#drop-sub");
if(sub.classList.contains("sub-menu-vis")){
document.getElementById("drop-sub").classList.remove("sub-menu-vis");
}else{
document.getElementById("drop-sub").classList.add("sub-menu-vis");
}
});
/* DONT TOUCH ANY OF THESE INFORMATION */
*,
*::before,
*::after {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
overflow-x: hidden;
}
html {
font-family: "Montserrat", sans-serif;
font-size: 12px;
scroll-behavior: smooth;
}
a {
text-decoration: none;
}
ul {
list-style: none;
}
/* END OF UNTOUCHABLE INFORMATION */
/*-------------------------------------------- STYLING FOR HEADER ----------------------------------------------*/
header {
width: 100%;
position: sticky;
top: 0;
left: 0;
z-index: 999;
}
.container {
width: 100%;
max-width: 117rem;
margin: 0 auto;
padding: 0 1.5rem;
}
.menu-icons {
color: #eee;
font-size: 4rem;
position: absolute;
top: 50%;
right: 2rem;
transform: translateY(-50%);
cursor: pointer;
z-index: 1500;
display: none;
}
nav {
display: flex;
align-items: center;
width: 100%;
height: 8rem;
border-bottom: 1px solid rgba(225, 225, 225, 0.1);
}
.logo {
display: flex;
margin-right: 5rem;
}
.nav-list {
display: flex;
width: 100%;
align-items: center;
}
.nav-list li {
line-height: 8rem;
position: relative;
}
.sub-menu li {
line-height: 4rem;
}
.nav-list a {
display: block;
color: white;
background-color: #1e90ff;
padding: 0 1.5rem;
font-size: 1.4rem;
text-transform: uppercase;
transition: color 650ms;
transition: background-color 650ms;
}
.nav-list a:hover {
color: black;
background-color: yellow;
}
.cart {
height: 30px;
width: 40px;
cursor: pointer;
margin-right: 15px;
margin-bottom: 5px;
}
.btn {
padding: 1.3rem;
display: inline-block;
background-color: #1e90ff;
border: 2px solid #1e90ff;
border-radius: 5rem;
transition: background-color 650ms;
}
.btn:hover {
color: black;
background-color: yellow;
}
.sub-menu {
width: 20rem;
display: block;
position: absolute;
border-top: 3px solid #00bfff;
z-index: 100;
top: 16rem;
transition: all 650ms ease;
opacity: 0;
visibility: hidden;
}
.sub-menu::before {
content: "";
position: absolute;
top: -2.5rem;
left: 3rem;
border: 1.2rem solid transparent;
border-bottom-color: #00bfff;
}
.sub-menu .sub-menu::before {
top: 0.9rem;
left: -2.5rem;
border: 1.2rem solid transparent;
border-right-color: #00bfff;
}
.sub-menu .sub-menu {
border-top: none;
border-left: 3px solid #00bfff;
top: 0;
left: 160%;
}
.nav-list li:hover > .sub-menu {
top: 8rem;
opacity: 1;
visibility: visible;
}
.sub-menu li:hover > .sub-menu {
top: 0;
left: 100%;
}
li.move-right {
margin: auto 0 auto auto;
line-height: initial;
transition: color 650ms;
transition: background-color 650ms;
}
/*-------------------------------------------- STYLING FOR HEADER END ----------------------------------------------*/
/*-------------------------------------------- RESPONSIVENESS FOR HEADER ----------------------------------------------*/
@media screen and (max-width: 1200px) {
.nav-list {
position: fixed;
top: 0;
left: 0;
height: 100vh;
display: none;
align-items: initial;
flex-direction: column;
background-color: #1e90ff;
z-index: 1000;
overflow-y: scroll;
}
.nav-list li {
line-height: 6rem;
}
.sub-menu {
position: initial;
border: 3px solid transparent;
border-left-color: #00bfff;
margin-left: 1rem;
max-height: 0;
display: none;
}
.sub-menu::before {
display: none;
}
.nav-list li:hover > .sub-menu {
visibility: hidden;
}
.sub-menu-vis {
opacity: 1;
display: block;
visibility: visible !important;
max-height: initial;
}
li.move-right {
margin: 0 auto 0 0;
line-height: initial;
}
.menu-icons {
display: block;
}
.fas fa-times {
display: none;
}
.dropdown{
cursor: pointer;
}
nav.active .fas fa-times {
display: block;
}
nav.active .fas fa-bars {
display: none;
}
nav.active .nav-list {
display: flex;
}
}
/*-------------------------------------------- RESPONSIVENESS FOR HEADER END ----------------------------------------------*/
/* ----------------------------- STYLING FOR TABLE -------------------------------------- */
/* Table container */
.tableContainer {
display: flex;
justify-content: center;
flex-direction: row;;
}
/* Contents of the table that are being style */
.tableContents {
display: flex;
justify-content: center;
text-align: left;
margin-top: 10px;
margin-bottom: 20px;
flex-direction: row;
}
.tableContents table {
border-collapse: collapse;
}
.tableContents th {
background-color: #1e90ff;
color: white;
border: 1px solid black;
padding: 8px;
padding-top: 12px;
padding-bottom: 12px;
font-size: 25px;
}
.tableContents td {
padding: 8px;
border: 1px solid black;
}
.tableContents tr {
background-color: #F0F8FF;
color: black;
border: 1px solid black;
font-size: 15px;
}
.tableContents tr:hover {
background-color: white;
}
table tr:nth-child(odd) {
background-color: #DCDCDC;
}
/* Fixing margins for phone */
.tablePhoneFix {
margin-left: 10px;
display: flex;
justify-content: center;
}
/* ----------------------------- STYLING FOR TABLE END -------------------------------------- */
/* ----------------------------- STYLING FOR HERO IMAGE BANNER -------------------------------------- */
.back-test {
height: 100vh;
background-image: linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5)), url(../images/heroImage.jpg);
background-attachment: fixed;
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}
/* ----------------------------- STYLING FOR HERO IMAGE BANNER END -------------------------------------- */
/*------------------------------------------- TABLE RESPONSIVENESS ----------------------------------------------*/
@media (max-width: 730px) {
.tableContainer {
display: flex;
justify-content: center;
flex-direction: column;
width: 90%;
}
.tableContents {
display: flex;
justify-content: center;
text-align: left;
margin-top: 10px;
margin-bottom: 20px;
flex-direction: column;
margin-left: 10px;
}
.tableContents th {
background-color: #1e90ff;
color: white;
border: 1px solid black;
padding: 8px;
padding-top: 12px;
padding-bottom: 12px;
font-size: 20px;
}
.tableContents tr {
background-color: #F0F8FF;
color: black;
border: 1px solid black;
font-size: 15px;
}
.tablePhoneFix {
margin-top: 10px;
margin-left: 0px;
display: flex;
justify-content: center;
}
.back-test {
height: 200vh;
background-image: linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5)), url(../images/heroImage.jpg);
background-attachment: fixed;
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}
}
/*------------------------------------------- TABLE RESPONSIVENESS END ----------------------------------------------*/
<!DOCTYPE html>
<html>
<head>
<!-- Meta Data *IGNORE* !-->
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link href="https://unpkg.com/ionicons@4.5.10-0/dist/css/ionicons.min.css" rel="stylesheet">
<link rel="stylesheet" href="css1/styler.css">
<script src="javascript/script.js" defer></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.15.3/css/all.css" integrity="sha384-SZXxX4whJ79/gErwcOYf zWLeJdY/qpuqC4cAa9rOGUstPomtqpuNWT9wdPEn2fk" crossorigin="anonymous">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Montserratamp;display=swap" rel="stylesheet">
<!-- Header !-->
<header>
<div style="background-color: #1E90FF;" class="container-fluid">
<nav>
<div class="menu-icons">
<i class="fas fa-bars"></i>
</div>
<a href="index.php" class="logo">
<img class="logo" src="images/logo_trans.png" alt="logo" style="height: 75px;">
</a>
<ul class="nav-list">
<!-- Normal listing buttons !-->
<li>
<a href="index.php">Home</a>
</li>
<li>
<a class="dropdown">Services
<i class="fas fa-sort-down"></i>
</a>
<!-- Drop Down List !-->
<ul id="drop-sub" class="sub-menu">
<li>
<a href="businessPrintingPage1.php">Business Printing</a>
</li>
<li>
<a href="canvasPrintingPage1.php">Canvas Printing</a>
</li>
<li>
<a href="embroideryPage1.php">Embroidery</a>
</li>
<li>
<a href="engravingPage1.php">Engraving</a>
</li>
<li>
<a href="signsPage1.php">Signs</a>
</li>
<li>
<a href="apperalPage1.php">Custom Apperal</a>
</li>
</ul>
</li>
<!-- Normal listing buttons !-->
<li>
<a href="Promotions.php">Promotions</a>
</li>
<li>
<a href="partners.php">Partners</a>
</li>
<li>
<a href="aboutUs.php">About Us</a>
</li>
<li>
<a href="contactUs.php">Contact Us</a>
</li>
<!-- Cart !-->
<li class="move-right">
<img class="cart" style="float: right;" src="images/new_cart.png" alt="Cart">
</li>
</ul>
</nav>
</div>
</header>
<!-- Javascript for making a new navbar image for responsiveness !-->
<script src="javascript/script.js"></script>
</head>
<body>
Комментарии:
1. Вы изменили только javascript здесь?
2. Нет, я изменил CSS И HTML, потому что не было места, чтобы поместить это в сообщение. Посмотрите здесь полный код: jsfiddle.net/Allan_StackoverFlow/stygwvx7/3
3. Теперь он должен работать на мобильных устройствах. Это будет нажатие на нажатие.
4. Какие части CSS и HTML вы изменили? Я взял HTML-код заголовка и код заголовка CSS, но мне не повезло с этим, подменю даже не появляется сейчас
5. Вы должны нажать на него прямо сейчас