How to hide nav bar on scroll and show a sidebar? - html

I am using bootstrap to code the frontend of a website. What I hope to achieve is that when I scroll the navbar vanishes and the sidebar pops up. I have been stuck on this for ages so If anyone has any idea to let me know.

This should answer the first piece of the question, in terms of hiding the navbar on scroll. I would get away from using bootstrap in this instance. It is easier to build your navbar from scratch, style it with css, and then use javaScript to manipulate it dynamically, such as hiding it. Once this is working. I can help you to get the sidebar to present on scroll. The link should give you an idea where to do with this.
https://www.w3schools.com/howto/howto_js_navbar_hide_scroll.asp
It is my opinion that the following block of javaScript will help you the most. you did not leave a code block, so it is only an assumption, based on what the common layout is. Again, I would move away from bootstrapping the menu bar, since you are wanting to customize features within it. You will see an explanation of the site that I listed below:
The first block would be your navbar div:
<div id="navbar">
Home
News
Contact
</div>
You can set this up and represent it however you would choose too. It should be in a standalone HTML file, lets say index.html for these purposes.
The next block of code is the CSS, as it would pertain to the above codeblock. Again, this can be shaped however you would like it to be, but for these purposes it is simply giving a blueprint. This should also be in a standalone css file.
#navbar {
background-color: #333; /* Black background color */
position: fixed; /* Make it stick/fixed */
top: 0; /* Stay on top */
width: 100%; /* Full width */
transition: top 0.3s; /* Transition effect when sliding down (and up) */
}
/* Style the navbar links */
#navbar a {
float: left;
display: block;
color: white;
text-align: center;
padding: 15px;
text-decoration: none;
}
#navbar a:hover {
background-color: #ddd;
color: black;
}
The final code block is your script. The JS that will dynamically change the navbar is contained within. As you can see below, they are setting the variable globally, though not always the best way, and then creating a function expression, this has to do with hoisting, and then simply hide the navigation bar. Please let me know if you need any further assistance to help you understand this.
js.file
var prevScrollpos = window.pageYOffset;
window.onscroll = function() {
var currentScrollPos = window.pageYOffset;
if (prevScrollpos > currentScrollPos) {
document.getElementById("navbar").style.top = "0";
} else {
document.getElementById("navbar").style.top = "-50px";
}
prevScrollpos = currentScrollPos;
}

Related

How to fix fixed link taking up full width of page?

I have a fixed anchor tag linked to a heading on my website. This anchor tag has an arrow icon that is meant to be a quick way to get to the top of the page. I wanted to position it at the bottom right, but the link is taking up full width at the bottom of the page unless I use "width: fit-content;'. I tried all the display options and float only works on the icon and not the link. It makes the arrow icon float to the position I want but the link width is still taking up all the pages width. Does anyone know how I can fix the width of the link and position it to the bottom right side of the page? Thank you in advance.
NOTE-
I am using bootstrap 5
#quick-anchor-top {
font-size: 25px;
padding: 15px 25px 15px 25px;
border-radius: 50px;
color: rgb(0, 0, 0);
background-color: rgba(182, 20, 20, 0.800);
transition: all 0.4s ease;
margin: 20px;
}
#quick-anchor-top:hover {
transition-duration: 0.4s;
color: white;
background-color: rgba(0, 0, 0, 0.800);
}
<a id="quick-anchor-top" href="#header-title-1" class="fixed-bottom float-end"> <i class="fa-solid fa-arrow-up"></i></a>
You can use predefined bootstrap 5 classes to position elements on the site. Use position-fixed and bottom-0, end-0 to set your element to the bottom right corner.
Just change your classes in the anchor element like this:
<a id="quick-anchor-top" href="#header-title-1" class="position-fixed bottom-0 end-0"><i class="fa-solid fa-arrow-up"></i></a>
Here is an example: https://jsfiddle.net/5j7do14t/
More: https://getbootstrap.com/docs/5.0/utilities/position/
Always try to share your full code so others can understand better and help you in a faster way. As I can see your code is missing an important point which is the position.
Since you didn't share the full code I will try to explain to you how to do a Scroll back to top button in a very easy way.
First of all, you need the button itself, and secondly, do your style, and finally, the JavaScript code that will handle the scroll.
So let's start.
1. Create the button that will take you to the top of the page when clicked on.
<button id="quickAnchorTop" title="Go to top"><i class="fa-solid fa-arrow-up"></i></button>
For the naming, it's better to use the camelCase since you will use its ID in the CSS and in the JS code, so we can call your button (quickAnchorTop instead of quick-anchor-top).
2. Now the most important part, is the CSS to style your button.
#quickAnchorTop {
display: none; /* It should be hidden by default */
position: fixed; /* Fixed/sticky position */
bottom: 20px; /* Place the button at the bottom of the page */
right: 20px; /* Place the button 20px from the right */
z-index: 99; /* Make sure it does not overlap */
border: none; /* Remove borders */
outline: none; /* Remove outline */
background-color: blue; /* Set a background color */
color: white; /* Text color */
cursor: pointer; /* Add a mouse pointer on hover */
padding: 16px; /* Some padding */
border-radius: 10px; /* Rounded corners */
font-size: 18px; /* Font size */
}
#quickAnchorTop:hover {
background-color: #555; /* Add a dark-grey background on hover */
}
3. Finally, use JavaScript to handle the scroll and show your button.
// Get the button from the current page:
let myBackToTopButton = document.getElementById("quickAnchorTop");
// When you scrolls down 20px from the top of the document, show the button
window.onscroll = function() {scrollFunction()};
function scrollFunction() {
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
myBackToTopButton .style.display = "block"; // To show your button
} else {
myBackToTopButton .style.display = "none"; // To hidd your button
}
}
// When you click on the button, scroll to the top of the document
function topPageFunction() {
document.body.scrollTop = 0; // For Safari
document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera
}
In the above JS code, first, you will get your button by the ID you used which is (quickAnchorTop), then you will have a function to handle the scroll and to show or hide your button.
Now make sure to update your button code and use this final form as you added the onclick event to your button:
<button onclick="topPageFunction()" id="quickAnchorTop" title="Go to top"><i class="fa-solid fa-arrow-up"></i></button>
Hopefully this will help you.

Can I allow inline-block elements to split across lines?

I'd like to have links zoom in when the mouse hovers on them, I've tried with transform unsuccessfully, but then I found this answer, which looked promising.
However, making an inline element an inline-block also seems to prevent it from being split across several lines, as shown in the snippet below, which can create very unpleasant results for short width of the enclosing box.
div {
border: 1px solid black;
text-align: justify;
width: 20em;
}
a {
text-decoration: none;
display: inline-block;
}
a:hover {
transform: scale(1.01);
}
<div>
<p>Today, <a href="https://github.com/Aster89/WinZoZ">my
Vim plugin for easy window navigation</a>, which I named
WinZoZ,
has got its first star! Given <a
href="https://stackoverflow.com/questions/69007954/vim-remap-ctrl-w-in-insert-mode-to-behave-as-it-does-in-normal-mode#comment121984179_69007954">this
comment on StackOverflow</a>, the star is from the user #yolenoyer.
</p>
</div>
On the other hand, in this specific example above I see that the first link is so long that it does split across lines, so it looks like inline-block elements can indeed do that. How can allow it when they are shorter than the text width?
The animation missing is due to the original link (a tag) element not having the transition: property defined. Per the positioning documentation here it seems only inline-block is suitable for flowing text and that fails to show wrapped text, even with wrap: break-word; present. The inline-flex, inline-grid don't work either since they are both block display types.
One solution would be to break the text lines at certain points and setting different <br> elements to show at different #media widths for certain page widths/devices. However the inline-block elements cannot wrap like normal text, so the breaks just end up making it a 2-line block in the middle of the text.
div {
border: 1px solid black;
/* text-align: justify; */
width: 20em;
}
a {
text-decoration: none;
/* new */
transition: transform .15s; /* Animations: "transform" on a-tag must be present */
display: inline-block;
}
a:hover {
transform: scale(1.01); /* then we transform the transition here */
-ms-transform: scale(1.01); /* IE 9 */
-webkit-transform: scale(1.01); /* Safari 3-8 */
}
<div>
<p>Today, <a href="https://github.com/Aster89/WinZoZ">my
Vim plugin for easy window navigation</a>, which I named
WinZoZ,
has got its first star! Given <a
href="https://stackoverflow.com/questions/69007954/vim-remap-ctrl-w-in-insert-mode-to-behave-as-it-does-in-normal-mode#comment121984179_69007954">this
comment on StackOverflow</a>, the star is from the user #yolenoyer.
</p>
</div>
Some JS scripting
A breaking up of the line into blocks in a ul list with the li items being inline-block themselves could be a solution. A function to run at DOM load on each desired div's contents could do this. A loop for all a elements in those divs that transform each of the links into an array of words and puts the array items in a ul -> li. Perhaps there is a jQuery plugin for this already.
Light JS example
(not complete code, but using querySelectorAll, which could be used to gather the links from a <div> with a class you put as the function input):
<script type="text/javascript">
// function to look for a-tags in a DIV with a specific class
function linkToList(inputDivClass) {
// get the links inside the div with the input div class
const allLinks = document.querySelectorAll("div." + inputDivClass + " > a");
for (var i = allLinks.length; i < 0; i++) {
// here we go through the links returned from the div...
}
// then go through the data and see what to put where...
}
// when dom is loaded we run the function that looks for the divs with the a-tags...
document.addEventListener("DOMContentLoaded", linkToList(inputDivClass) );
</script>

Hide header and footer using Custom CSS

I am trying to hide the header and footer from a specific page on my website. I am using a theme I downloaded online. The specific page I am trying to hide is http://ai-home.com/dsme/
I installed a custom CSS plugin so that I can customize the CSS on this page. I inspected the page element and can see that I am most likely trying to hide the
div id="header-space" and div id="footer-outer"
After reading online I think the code should be
.page-id-5321 .site-header, .page-id-5321 .site-footer {
display: none;
}
or
.page-id-5321 .site-header-space, .page-id-5321 .footer-outer {
display: none;
}
When I publish, I do not see any changes to the page. I am not a developer so I want to make this edit as easily as possible without it affecting the rest of my website.
EDIT:
tried some suggestions and was able to fix most of the problem, but now I am stuck with a big grey bar on the bottom but I can't find it via inspect element.
EDIT#2: So the CSS looks like this right now, but still stuck with a grey bar on the bottom
#header-outer { display: none;}
#header-space { display: none;}
#footer-outer { display: none;}
Use the visibility property as hidden.
Like [ visibility:hidden ]
In your header class/id.
Please try below code for removing header-outer, header-space and footer-outer
.page-id-5321 #header-outer, .page-id-5321 #header-space, .page-id-5321 #footer-outer {
display: none;
}
I think if you use wordpress, in your specific page can use other header or/and other footer.
Change
get_header();
to
get_header('<other header file>');
same with footer
Try it
<div id="header-space" class="hide">xyz</div> and <div class="show" id="footer-outer">abc</div>
csscode:
.hide{display:none;}
.show{display:block;}
If you need to hide a block/section then just add class:hide to HTML OR for showing use class name show like mention above.
Hope it will work. Revert if it is not.
For grey bar solution
#footer-outer #copyright, body {
border: none!important;
background-color: #f8f8f8!important;
}
By changing the color of footer you can use the same background.

Make nav bar change color on scroll?

It's complicated to put it in words, but here I go.
Alright so at the moment I'm working on a project that requires me too do one thing. That thing being when the nav bar gets too a certain point I want the color to change. So let's say I have a nav bar white, and underneath that it's gray. How do I go about that?
I don't think you can do it with HTML/CSS but this work
Javascript/jQuery
$( window ).scroll(function() {
if ($( window ).scrollTop() > 500) {
$('.nav').addClass('updated-color')
} else {
$('.nav').removeClass('updated-color')
}
})
CSS
.nav {
background-color: white;
}
.nav.updated-color {
background-color: gray;
}

Use Only CSS to Alter Various Elements

I have a page with a left sidebar that I want to be able to toggle on or off based on whether or not the user clicks it. Unfortunately entering JavaScript code on this website has been disabled and I only have access to CSS.
The left sidebar has
its main div (parentBlock)
a div for the show/hide, (toggleBlock)
a div for the logo, (div1)
a div for the navbar, and (div2)
a div for social icons (div2)
When the user clicks on "Show / Hide" I want to:
Hide (display:none) the logo, navbar, and social div's, and
Set the height of the main div to something smaller (say 30px).
Is there any way to do this in CSS?
<div class="parentBlock">
<div class="toggleBlock">Show / Hide</div>
<div class="divBlah">div1</div>
<div class="divBlah">div2</div>
<div class="divBlah">div3</div>
</div>
Then if the user clicks "Show / Hide" again, it will unhide the div's and set the height back to filling the screen.
Is this possible?
I found some code that would work if the "Show / Hide" button was in "parentBlock" but it didn't work if it was within "toggleBlock" (and I have to have the Show/Hide button in toggleBlock)
(http://tympanus.net/codrops/2012/12/17/css-click-events/)
I realize onClick events require JavaScript. Those are not possible since I can't use JavaScript :( Some people try to get around it by using either :active or creating checkboxes and having the checkbox:clicked value load the action ... but it only works with certain relations that I can't seem to nail down.
Unfortunately I cannot alter the ultimate structure of "toggleBlock", div1, div2, and div3 ... only what's in them and their CSS. Also making it even more difficult is that the website randomly generates ID="" each time the page loads so the TARGET method isn't possible. Also, the 3 div's (div1 thru div3) have the same class name. I'm beginning to think it's impossible :(
(For reference, I'm trying to use the tools on the New SmugMug and they're rather restrictive)
Here is a CSS only solution using target
DEMO http://jsfiddle.net/kevinPHPkevin/r4AQd/
.button {
display: block;
width:60px;
background: red;
z-index:1;
}
#element {
display: none;
background:#fff;
margin-top:-20px;
z-index:2;
}
#element:target {
display: block;
}
#show:target {
display: block;
}
#hide {
background: #000;
color: #fff;
}
As Joum has pointed out this is not possible to do via click events but using hover on siblings you might be able to achieve a similar effect. for example try adding this css:
div.toggleBlock { display: block; }
div.toggleBlock ~ div { display: none; }
div.toggleBlock:hover ~ div { display: block; }
for more information see this: http://css-tricks.com/child-and-sibling-selectors/