this is the Code for my Navigation Bar (ignore the .hide() spam)
function test(){
var tabsNewAnim = $('#navbarSupportedContent');
var selectorNewAnim = $('#navbarSupportedContent').find('li').length;
var activeItemNewAnim = tabsNewAnim.find('.active');
var activeWidthNewAnimHeight = activeItemNewAnim.innerHeight();
var activeWidthNewAnimWidth = activeItemNewAnim.innerWidth();
var itemPosNewAnimTop = activeItemNewAnim.position();
var itemPosNewAnimLeft = activeItemNewAnim.position();
$(".hori-selector").css({
"top":itemPosNewAnimTop.top + "px",
"left":itemPosNewAnimLeft.left + "px",
"height": activeWidthNewAnimHeight + "px",
"width": activeWidthNewAnimWidth + "px"
});
$("#navbarSupportedContent").on("click","li",function(e){
$('#navbarSupportedContent ul li').removeClass("active");
$(this).addClass('active');
var activeWidthNewAnimHeight = $(this).innerHeight();
var activeWidthNewAnimWidth = $(this).innerWidth();
var itemPosNewAnimTop = $(this).position();
var itemPosNewAnimLeft = $(this).position();
$(".hori-selector").css({
"top":itemPosNewAnimTop.top + "px",
"left":itemPosNewAnimLeft.left + "px",
"height": activeWidthNewAnimHeight + "px",
"width": activeWidthNewAnimWidth + "px"
});
});
}
$(document).ready(function(){
setTimeout(function(){ test(); });
$("#toggle1").click(function() {
$("#panel1").show("slow");
$("#panel2").hide();
$("#panel3").hide();
$("#panel4").hide();
$("#panel5").hide();
});
$("#toggle2").click(function() {
$("#panel1").hide()
$("#panel2").show("slow");
$("#panel3").hide();
$("#panel4").hide();
$("#panel5").hide();
});
$("#toggle3").click(function() {
$("#panel1").hide()
$("#panel2").hide()
$("#panel3").show("slow");
$("#panel4").hide();
$("#panel5").hide();
});
$("#toggle4").click(function() {
$("#panel1").hide()
$("#panel2").hide()
$("#panel3").hide()
$("#panel4").show("slow");
$("#panel5").hide();
});
$("#toggle5").click(function() {
$("#panel1").hide()
$("#panel2").hide()
$("#panel3").hide()
$("#panel4").hide()
$("#panel5").show("slow");
});
});
$(window).on('resize', function(){
setTimeout(function(){ test(); }, 500);
});
$(".navbar-toggler").click(function(){
$(".navbar-collapse").slideToggle(300);
setTimeout(function(){ test(); });
});
When the page loads I want to hide divs with the id #panel2, #panel3, #panel4…
How can I do this?
Or is it also possible that if the links lead to a specific id, for example href="#panel4" and I refresh the page, that all divs are hidden except #panel4?
EDIT
The HTML Nav:
<li class="nav-item active">
<a id="toggle1" class="nav-link" href="javascript:void(0);"><i class="fas fa-tachometer-alt"></i>My Link</a>
</li>
<li class="nav-item ">
<a id="toggle2" class="nav-link" href="javascript:void(0);"><i class="far fa-address-book"></i>Buttons</a>
</li>
<li class="nav-item">
<a id="toggle3" class="nav-link" href="javascript:void(0);"><i class="far fa-clone"></i>Background</a>
</li>
<li class="nav-item">
<a id="toggle4" class="nav-link" href="javascript:void(0);"><i class="far fa-calendar-alt"></i>Tracking</a>
</li>
<li class="nav-item">
<a id="toggle5" class="nav-link" href="javascript:void(0);"><i class="far fa-chart-bar"></i>Other</a>
</li>
>Solution :
If you want to hide them as soon as the DOM finished loading you can use the
DOMContentLoaded event.
You also need to check if the DOMContentLoaded event hasn’t fired before your code execution. You can do that with the document.readyState
With jQuery you have the .hide() method to hide elements.
You can also show keep the anchor used in your url visible. You can get it with window.location.href and a regex.
const regex = /#.*/;
const url = window.location.href;
const anchor = url.match(regex)[0]
if(document.readyState === "complete") {
$("#panel2, #panel3, #panel4").hide();
$(anchor).show();
}else{
window.addEventListener('DOMContentLoaded', (event) => {
$("#panel2, #panel3, #panel4").hide();
$(anchor).show();
});
}
However, you might have a small delay where you see the elements.