When I run my code locally, my Javascript works perfectly. But after I deploy to Heroku, none of it works anymore. Why is that?
Javascript: (file called basic.js)
$(window).scroll(function() {
if ($(document).scrollTop() > 700) {
$('nav').addClass('shrink');
}
else {
$('nav').removeClass('shrink');
}
});
HTML:
<nav id="mainNav" class="navbar navbar-fixed-top">
<div class="navbar-contents">
<div class="collapse navbar-collapse">
<ul class="nav">
<li>
<a class="page-scroll" href="gallery.html">GALLERY</a>
</li>
<li>
<a class="page-scroll" href="#contact-us">CONTACT</a>
</li>
<li>
<a class="page-scroll" href="#customer-quote">WHAT WE DO</a>
</li>
<li>
<a class="page-scroll" href="#showcase-section">SHOWCASE</a>
</li>
<li>
<a class="page-scroll" href="#about-section">ABOUT US</a>
</li>
</ul>
</div>
</div>
</nav>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src="basic.js"></script>
CSS:
#mainNav.shrink {
background-color: darkgray;
transition: 0.8s;
-webkit-transition: 0.8s;
}
Thanks in advance for the help.
Related
window.addEventListener("resize", function() {
"use strict"; window.location.reload();
});
document.addEventListener("DOMContentLoaded", function(){
// make it as accordion for smaller screens
if (window.innerWidth < 992) {
document.querySelectorAll('.sidebar .nav-link').forEach(function(element){
element.addEventListener('click', function (e) {
let nextEl = element.nextElementSibling;
let parentEl = element.parentElement;
let allSubmenus_array = parentEl.querySelectorAll('.submenu');
if(nextEl && nextEl.classList.contains('submenu')) {
e.preventDefault();
if(nextEl.style.display == 'block'){
nextEl.style.display = 'none';
} else {
nextEl.style.display = 'block';
}
}
});
})
}
// end if innerWidth
});
// DOMContentLoaded end
/* ============ desktop view ============ */
#media all and (min-width: 992px) {
.sidebar li{ position: relative; }
.sidebar li .submenu{
display: none;
position: absolute;
left:100%; top:-7px;
max-width: 140px;
}
[dir=rtl] .sidebar li .submenu{
right:100%; left:auto;
}
.sidebar li:hover{ background: var(--bs-light); }
.sidebar li:hover > .submenu{ display: block; }
}
/* ============ desktop view .end// ============ */
/* ============ small devices ============ */
#media (max-width: 991px) {
.sidebar .submenu, .sidebar .dropdown-menu{
position: static!important;
margin-left:0rem; margin-right:0.7rem; margin-bottom: .5rem;
}
}
/* ============ small devices .end// ============ */
.sidebar .nav-link {
font-weight: 500;
color: var(--bs-dark);
}
.sidebar .nav-link:hover {
background: var(--bs-light);
color: var(--bs-primary);
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta2/dist/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta2/dist/js/bootstrap.bundle.min.js"crossorigin="anonymous"></script><body class="bg-light">
<div class="container-fluid">
<section class="section-content">
<div class="row">
<aside class="col-lg-3">
<!-- ============= COMPONENT ============== -->
<nav class="sidebar card py-2 mb-4">
<ul class="nav flex-column">
<li class="nav-item">
<a class="nav-link" href="#"> Link name </a>
</li>
<li class="nav-item">
<a class="nav-link" href="#"> Submenu links <b class="float-end">»</b> </a>
<ul class="submenu dropdown-menu">
<li><a class="nav-link" href="#">Submenu item 1 </a></li>
<li><a class="nav-link" href="#">Submenu item 2 </a></li>
<li><a class="nav-link" href="#">Submenu item 3 </a>
</li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link" href="#"> Treeview category <b class="float-end">»</b> </a>
<ul class="submenu dropdown-menu">
<li><a class="nav-link" href="#">Submenu item 1 </a></li>
<li><a class="nav-link" href="#">Submenu item 2 </a></li>
<li><a class="nav-link" href="#">Submenu item 3 <b class="float-end">»</b> </a>
<ul class="submenu dropdown-menu">
<li><a class="nav-link" href="#">Multi level 1</a></li>
<li><a class="nav-link" href="#">Multi level 2</a></li>
<li><a class="nav-link" href="#">Multi level 3</a></li>
</ul>
</li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link" href="#"> Another page </a>
</li>
<li class="nav-item">
<a class="nav-link" href="#"> Demo link </a>
</li>
<li class="nav-item">
<a class="nav-link" href="#"> Menu item </a>
</li>
<li class="nav-item">
<a class="nav-link" href="#"> Something </a>
</li>
<li class="nav-item">
<a class="nav-link" href="#"> Other link </a>
</li>
</ul>
</nav>
<!-- ============= COMPONENT END// ============== -->
</aside>
<main class="col-lg-9">
<h6>Demo for sidebar nav menu links. <br> Based on Bootstrap 5 CSS framework. </h6>
</main>
</div>
</section>
</div><!-- container // -->
</body>
</html>
Above I have code for Bootstrap 5 left sidebar menu. (Please check code on full page screen).
Currently menu working on mouse hover.
can anyone help me with:
Set sidebar always 100% height page
and display submenu only after click on mouse as second sidebar with also height 100%.
This is example screen with except result:
In this code there is some error, which I could not get it. Js code not working properly, any advice to change it so that it works properly.
Web link.
<script src="js/jquery-3.2.1.min.js"></script>
<!-- Main Navigation -->
<!-- Brand and toggle get grouped for better mobile display -->
<!-- Collect the nav links, forms, and other content for toggling -->
<nav class="main_nav">
<ul class="d-flex flex-row align-items-start justify-content-start">
<li class="active">
<a class="page-scroll" href="#home">Home</a>
</li>
<li>
<a class="page-scroll" href="#about">About</a>
</li>
<li>
<a class="page-scroll" href="#services">Services</a>
</li>
<li>
<a class="page-scroll" href="#team">Team</a>
</li>
<li>
<a class="page-scroll" href="#gallery">Gallery</a>
</li>
<li>
<a class="page-scroll" href="#contact">Contact</a>
</li>
</ul>
<!-- navbar-collapse -->
</nav>
JS code
$(document).ready(function()
{
"use strict";
var main_menu = $(".main-menu ul.d-flex li ");
main_menu.on('click', function() {
main_menu.removeClass("active");
$(this).addClass("active");
});
});
Class name is wrong in your js code.
use var main_menu = $(".main_nav ul.d-flex li"); in place of var main_menu = $(".main-menu ul.d-flex li");
$(document).ready(function()
{
"use strict";
var main_menu = $(".main_nav ul.d-flex li");
main_menu.on('click', function() {
main_menu.removeClass("active");
$(this).addClass("active");
});
});
.active,.active a {
color:red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Main Navigation -->
<!-- Brand and toggle get grouped for better mobile display -->
<!-- Collect the nav links, forms, and other content for toggling -->
<nav class="main_nav">
<ul class="d-flex flex-row align-items-start justify-content-start">
<li class="active">
<a class="page-scroll" href="#home">Home</a>
</li>
<li>
<a class="page-scroll" href="#about">About</a>
</li>
<li>
<a class="page-scroll" href="#services">Services</a>
</li>
<li>
<a class="page-scroll" href="#team">Team</a>
</li>
<li>
<a class="page-scroll" href="#gallery">Gallery</a>
</li>
<li>
<a class="page-scroll" href="#contact">Contact</a>
</li>
</ul>
<!-- navbar-collapse -->
</nav>
I got an error
Not Found: /menu.js.
I wanna embed jQuery into HTML,app(child app) is in testapp(parent app),and app has index.html&menu.js. index.html&menu.js is in templates folder which is in app.
index.html
<html>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="./menu.js" ></script>
<body>
<div class="list-company_demand">
<ul class="menu demand-parent">
<li>
<a class="menu-item">A(a)</a>
<ul class="menu-child">
<li>
<a class="menu-item">1</a>
<a class="menu-item">2</a>
<a class="menu-item">3</a>
<a class="menu-item">4</a>
</li>
</ul>
</li>
<li>
<a class="menu-item">A(b)</a>
<ul class="menu-child">
<li>
<a class="menu-item">1</a>
<a class="menu-item">2</a>
<a class="menu-item">3</a>
<a class="menu-item">4</a>
<a class="menu-item">5</a>
</li>
</ul>
</li>
<li>
<a class="menu-item">A(c)</a>
<ul class="menu-child">
<li>
<a class="menu-item">1</a>
<a class="menu-item">2</a>
<a class="menu-item">3</a>
<a class="menu-item">4</a>
</li>
</ul>
</li>
<li>
<a class="menu-item">A(d)</a>
<ul class="menu-child">
<li>
<a class="menu-item">1</a>
<a class="menu-item">2</a>
<a class="menu-item">3</a>
</li>
</ul>
</li>
</ul>
</div>
</body>
</html>
menu.js
$(function() {
$('.menu-item').on('click', function() {
var child = $(this).next('.menu-child');
$('.list-menu').height(child.height());
child.addClass('is-active');
});
$('.menu-child').prepend('<li class="menu-back"><a>1つ戻る<i class="fa fa-chevron-left"></i></a></li>');
$('.menu-back').on('click', function() {
var parents = $(this).parent();
parents.removeClass('is-active');
$('.list-menu').height(parents.parent().parent().height());
});
});
I rewrote in this part into into but same error happens.What is wrong in my code?Am I wrong to use jQuery?How can I fix this?
By the way, by using Google validation, Failed to load resource: the server responded with a status of 404 (Not Found) menu.js error happens in Console.
Folder structure is
testapp (app name)
-app (child app)
-templates
-menu.js &index.html
menu.js &index.html is in same folder templates.
Your jquery file makes the error, replace it width https://code.jquery.com/jquery-2.2.4.min.js, have a look:
$(function() {
$('.menu-item').on('click', function() {
var child = $(this).next('.menu-child');
$('.list-menu').height(child.height());
child.addClass('is-active');
});
$('.menu-child').prepend('<li class="menu-back"><a>1つ戻る<i class="fa fa-chevron-left"></i></a></li>');
$('.menu-back').on('click', function() {
var parents = $(this).parent();
parents.removeClass('is-active');
$('.list-menu').height(parents.parent().parent().height());
});
});
<html>
<head>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="./menu.js" ></script>
</head>
<body>
<div class="list-company_demand">
<ul class="menu demand-parent">
<li>
<a class="menu-item">A(a)</a>
<ul class="menu-child">
<li>
<a class="menu-item">1</a>
<a class="menu-item">2</a>
<a class="menu-item">3</a>
<a class="menu-item">4</a>
</li>
</ul>
</li>
<li>
<a class="menu-item">A(b)</a>
<ul class="menu-child">
<li>
<a class="menu-item">1</a>
<a class="menu-item">2</a>
<a class="menu-item">3</a>
<a class="menu-item">4</a>
<a class="menu-item">5</a>
</li>
</ul>
</li>
<li>
<a class="menu-item">A(c)</a>
<ul class="menu-child">
<li>
<a class="menu-item">1</a>
<a class="menu-item">2</a>
<a class="menu-item">3</a>
<a class="menu-item">4</a>
</li>
</ul>
</li>
<li>
<a class="menu-item">A(d)</a>
<ul class="menu-child">
<li>
<a class="menu-item">1</a>
<a class="menu-item">2</a>
<a class="menu-item">3</a>
</li>
</ul>
</li>
</ul>
</div>
</body>
</html>
I retrieved a code on Bootsnipp, which I try to adapt ...
I would like the menu present in the drop down to appear in the sidebar
How to do it?
The JsFiddle : https://jsfiddle.net/2hpxxpez/
HTML :
<header role="banner" class="navbar navbar-default">
<div class="container">
<button data-toggle="collapse-side" data-target=".side-collapse" data-target-2=".side-collapse-container" type="button" class="navbar-toggle pull-left">
<i class="glyphicon glyphicon-menu-hamburger"></i>
</button>
<div class="side-collapse in">
<nav role="navigation" class="navbar-collapse">
<!-- <ul class="nav navbar-nav" >
<li>Home</li>
<li>Users</li>
<li>Places</li>
</ul> -->
<ul class="nav navbar-nav" >
<li class="dropdown">
<a><span class="caret"></span></a>
<ul class="dropdown-menu">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
</ul>
</li>
</ul>
</nav>
</div>
</div>
</header>
<div class="container side-collapse-container">
<h1>Hello</h1>
<p>This is a side opening nav demonstration</p>
<p>Make your browser smaller and the top menu wil become into a lateral slider menu</p>
</div>
Javascript :
$(document).ready(function() {
var sideslider = $('[data-toggle=collapse-side]');
var sel = sideslider.attr('data-target');
var sel2 = sideslider.attr('data-target-2');
sideslider.click(function(event){
$(sel).toggleClass('in');
$(sel2).toggleClass('out');
});
});
I want this result :
i had created a fiddle for you hope it will help you click here you just need to toggle the class.
$('li.dropdown').click(function() {
$('li.dropdown').not(this).find('ul').hide();
$(this).find('ul').toggle();
});
$(document).ready(function() {
var sideslider = $('[data-toggle=collapse-side]');
var sel = sideslider.attr('data-target');
var sel2 = sideslider.attr('data-target-2');
sideslider.click(function(event){
$(sel).toggleClass('in');
$(sel2).toggleClass('out');
});
});
#media screen and (max-width: 768px) {
.side-collapse-container{
width:100%;
position:relative;
left:0;
transition:left .4s;
}
.side-collapse-container.out{
left:200px;
}
.dropdown-nave-block .dropdown-menu{ display: block !important;}
.side-collapse {
top:51px;
bottom:0;
left:0;
width:200px;
position:fixed;
overflow:hidden;
transition:width .4s;
background-color: #f8f8f8;
}
.side-collapse a {
border-bottom: 1px solid #e7e7e7;
}
.side-collapse.in {
width:0;
}
}
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<body>
<header role="banner" class="navbar navbar-default">
<div class="container">
<button data-toggle="collapse-side" data-target=".side-collapse" data-target-2=".side-collapse-container" type="button" class="navbar-toggle pull-left">
<i class="glyphicon glyphicon-menu-hamburger"></i>
</button>
<div class="side-collapse in">
<nav role="navigation" class="navbar-collapse">
<ul class="nav navbar-nav" >
<li>Home</li>
<li>Users</li>
<li>Places</li>
</ul>
<ul class="nav navbar-nav" >
<li class="dropdown dropdown-nave-block open" >
<a class="dropdown-toggle" data-toggle="dropdown" href="#" aria-expanded="true" >Page 1
<span class="caret"></span></a>
<ul class="dropdown-menu">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
</ul>
</li>
</ul>
</nav>
</div>
</div>
</header>
<div class="container side-collapse-container">
<h1>Hello</h1>
<p>This is a side opening nav demonstration</p>
<p>Make your browser smaller and the top menu wil become into a lateral slider menu</p>
</div>
i have change same html add dropdown class and
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Page 1
<span class="caret"></span></a>
How do i make my navigation bar scroll down together with the page? I have found some answers here but I'm having a hard time cause the code for my navigation bar is in another file and I just used <?php include ("includes/navbar.php"); ?> to incorporate it to my other pages. Here are my codes so far: What I'm planning to do is if I scroll my page down the navigation bar will be on top of the page
Navigation
<div class="navbar navbar-inverse">
<div class="container">
<div class="navbar-header">
<!-- Button for smallest screens -->
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button>
<!--<a class="navbar-brand" href="index.html">
<img src="assets/images/logo.png" alt="Techro HTML5 template"></a>-->
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav pull-right mainNav">
<li class="c1 active">Home</li>
<li class="c1">Profile</li>
<li class="c1">Games</li>
<li class="c1">Top Players</li>
<li class="c1">About</li>
<li class="c1 dropdown">
Settings <b class="caret"></b>
<ul class="dropdown-menu">
<li><a data-toggle="modal" data-target='#change'>Change Password</a></li>
<li>Logout</li>
</ul>
</li>
</ul>
</div>
<!--/.nav-collapse -->
</div>
<!--MODAL-->
<?php include("includes/changepass.php");?>
<!--END MODAL-->
</div>
CSS
.navbar-inverse .navbar-nav > li.c1 a{
color: #00A8FF;
}
.navbar-inverse .navbar-nav > li.c1 a:hover, .navbar-inverse .navbar-nav > li.c1.active a{
color:#fff;
background: #00A8FF;
}
JS
<script type="text/javascript">
$(document).ready(function() {
$(window).scroll(function () {
console.log($(window).scrollTop())
if ($(window).scrollTop() > 280) {
$('.navbar').addClass('navbar-fixed');
}
if ($(window).scrollTop() < 281) {
$('.navbar').removeClass('navbar-fixed');
}
});
});
</script>
Here's how my page looks
May be you are looking for this. View in fullscreen
You should add $('.navbar').addClass('navbar-fixed-top');
$(document).ready(function() {
$(window).scroll(function() {
console.log($(window).scrollTop())
if ($(window).scrollTop() > 280) {
$('.navbar').addClass('navbar-fixed-top');
}
if ($(window).scrollTop() < 281) {
$('.navbar').removeClass('navbar-fixed-top');
}
});
});
.navbar-inverse .navbar-nav > li.c1 a {
color: #00A8FF;
}
.navbar-inverse .navbar-nav > li.c1 a:hover,
.navbar-inverse .navbar-nav > li.c1.active a {
color: #fff;
background: #00A8FF;
}
.dummy-div {
background: black;
height: 200px;
}
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="dummy-div">
</div>
<div class="navbar navbar-inverse">
<div class="container">
<div class="navbar-header">
<!-- Button for smallest screens -->
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span>
</button>
<!--<a class="navbar-brand" href="index.html">
<img src="assets/images/logo.png" alt="Techro HTML5 template"></a>-->
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav pull-right mainNav">
<li class="c1 active">Home
</li>
<li class="c1">Profile
</li>
<li class="c1">Games
</li>
<li class="c1">Top Players
</li>
<li class="c1">About
</li>
<li class="c1 dropdown">
Settings <b class="caret"></b>
<ul class="dropdown-menu">
<li><a data-toggle="modal" data-target='#change'>Change Password</a>
</li>
<li>Logout
</li>
</ul>
</li>
</ul>
</div>
<!--/.nav-collapse -->
</div>
<!--MODAL-->
<?php include( "includes/changepass.php");?>
<!--END MODAL-->
</div>
<div style="height: 2000px;">
</div>
</div>
Since you're using BootStrap, please add the following class to your <nav> element:
navbar-fixed-top
Eg:
<nav class="navbar navbar-default navbar-fixed-top">
Have you tried adding position:fixed to the CSS for .navbar.navbar-inverse?