I have a sticky menu on my wordpress site which is just a header with css position fixed but it's overlaying over the top of each of my sections. You can see what I mean if you view the test site here
if you click on the menu icon and click on a section it will navigate to each section but I need the sticky menu to rest above each section instead of it overlaying at the top.
In my header.php I have
<div id="header-wrap">
<div class="poweredby">POWERED BY bluesource<p class="mobile-phone">0845 319 2100</p></div>
<div class="headerphone">0845 319 2100</div>
<button class="toggle-menu menu-right push-body"><i class="fa fa-bars"></i></button>
<!-- Right menu element-->
<?php if ( has_nav_menu( 'primary' ) || has_nav_menu( 'social' ) ) : ?>
<?php if ( has_nav_menu( 'primary' ) ) : ?>
<nav id="site-navigation" class="cbp-spmenu cbp-spmenu-vertical cbp-spmenu-right" role="navigation" aria-label="<?php esc_attr_e( 'Primary Menu', 'twentysixteen' ); ?>">
<?php
wp_nav_menu( array(
'theme_location' => 'primary',
'menu_class' => 'primary-menu',
) );
?>
</nav>
<?php endif; ?>
<?php endif; ?>
</div><!-- end header wrap -->
In my stylesheet I have
.site-header {
background: #333 none repeat scroll 0 0;
height: 98px;
padding: 27px 0;
text-align: right;
}
.site-header-main {
text-align: right;
right: 0;
display: block;
padding-right: 27px;
}
#header-wrap {
background: #333;
position: fixed;
top: 0;
z-index: 100;
height: 98px;
padding-top: 27px;
padding-right: 27px;
opacity: 0.9;
}
This code will do it
$('#header-wrap ul li a').click(function(){
href=$(this).attr('href');
$('html, body').animate({
scrollTop: $(href).offset().top-98 // since the height of your nav is 98px
}, 500);
});
For external links
$(document).ready(function(){
var urlHash = window.location.href.split("#")[1];
$('html,body').animate({
scrollTop: $('#' + urlHash).offset().top-98
}, 500);
});
This should do the job. I increased the position right so you get the white bar along the side, I applied an important tag otherwise it gets overwritten.
nav#site-navigation {
top: 98px;
}
.cbp-spmenu-right.menu-open {
right: 23px !important;
}
Related
How can i make when I click on the card it opens a "modal window" with information about the product.
<div class="card">
<div class="imgBox">
<img src="./img/bau.png" alt="Produto" class="mouse">
</div>
<div class="contentBox">
<h3>Plugin</h3>
<h2 class="price">25.<small>00</small> BRL</h2>
Comprar Agora!
</div>
</div>
There are several approaches. And there is no real right or wrong here either.
The approach has to fit your application. If you always try to keep the approach somewhat abstract, there is nothing fundamentally wrong with it.
In the example below, I have taken the linked modal example from the comment
below your question and adapted the following.
added a data object in which I manage the corresponding contents of the modal.Here you can also use an API call against an interface.
I have assigned an EventListener to all buttons.
The parts that are variable in the modal are exchanged with the corresponding content when clicked.
Done!
const modalData = [
{id: 1, title: "Title One", content: "bla"},
{id: 2, title: "Title Two", content: "bla blu"},
];
// Get the modal
var modal = document.getElementById("myModal");
// Get the button that opens the modal
var btns = document.querySelectorAll(".myBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks the button, open the modal
btns.forEach(b => {
b.addEventListener('click', (e) => {
modal.style.display = "block";
const dataId = e.target.getAttribute("data-id")
const data = modalData.filter(m => m.id == dataId);
const modalTitle = document.querySelector("#myModal .title");
const modalContent = document.querySelector("#myModal .content");
modalTitle.innerHTML = data[0].title;
modalContent.innerHTML = data[0].content;
})
});
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
body {font-family: Arial, Helvetica, sans-serif;}
/* The Modal (background) */
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding-top: 100px; /* Location of the box */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}
/* Modal Content */
.modal-content {
position: relative;
background-color: #fefefe;
margin: auto;
padding: 0;
border: 1px solid #888;
width: 80%;
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
-webkit-animation-name: animatetop;
-webkit-animation-duration: 0.4s;
animation-name: animatetop;
animation-duration: 0.4s
}
/* Add Animation */
#-webkit-keyframes animatetop {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}
#keyframes animatetop {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}
/* The Close Button */
.close {
color: white;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
.modal-header {
padding: 2px 16px;
background-color: #5cb85c;
color: white;
}
.modal-body {padding: 2px 16px;}
.modal-footer {
padding: 2px 16px;
background-color: #5cb85c;
color: white;
}
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h2>Animated Modal with Header and Footer</h2>
<!-- Trigger/Open The Modal -->
<button class="myBtn" data-id="1">Open Modal 1</button>
<button class="myBtn" data-id="2">Open Modal 2</button>
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<div class="modal-header">
<span class="close">×</span>
<h2 class="title">Modal Header</h2>
</div>
<div class="modal-body content">
</div>
<div class="modal-footer">
<h3>Modal Footer</h3>
</div>
</div>
</div>
</body>
I want to make a portfolio site and am struggling with my page creation:
The visual elements are a Header, a Sidebar, a Frame for the content and the Content itself. I already have a Grid designed with respective areas for all the elements and dynamic resizing based on the calculated dimensions of a logo in the Header.
Now I have the issue, that the Content overflows and therefore should be scrollable, however the Header and the Frame (literally a frame as from an old painting) shall not move when scrolling and always be on top.
The size of the container with the Grid is defined by the viewport and overflow is hidden. The size of the container with the Content is defined by its content and overflow is scrollable. With z-index or order of containers the Content was put on a layer below the Header and Frame. This does not work, since as soon as the layer of the scrollable Content was not on top of everything, it became not scrollable anymore. Modyfing the above layers with pointer-events: none is not possible since they contain the site navigation. Stacking a separate Grid for Content and Header/Sidebar/Frame on top of each other seemed to suffer from the same layer problem.
How shall one design a site with scrollable content beneath a fixed header in the Grid system?
I would greatly appreciate the help, I promise I tried to look it up online but did not really see it discussed. This approach by Michael X for example did not work for me due to layer issues: https://medium.com/#beyondborders/beginner-css-grid-sticky-navigation-scrolling-content-7c4de0a8d1dc
I added the relevant code:
/* ////////////////////////////////////////////////////////////ROOT */
:root {
--margin-left: min(10vw, var(--max-margin-left));
--max-margin-left: 6rem;
--margin-right: min(10vw, var(--max-margin-right));
--max-margin-right: 8rem;
--margin-top: 2rem;
--margin-bottom: 2rem;
--dimensions-logo: min(20vw, var(--max-dimensions-logo));
--max-dimensions-logo: 8rem;
--padding-logo: calc(var(--dimensions-logo) / 7);
--margin-left-content: calc(var(--margin-left) + var(--dimensions-logo) + 3 * var(--padding-logo));
--margin-right-content: calc(var(--margin-right) + 2 * var(--padding-logo));
--margin-top-content: calc(var(--margin-top)) + var(--dimensions-logo) + 3 * var(--padding-logo));
--margin-bottom-content: calc(var(--margin-bottom) + 2 * var(--padding-logo));
}
*,
*::before,
*::after {
box-sizing: border-box;
}
/* ////////////////////////////////////////////////////////////HTML-ROOT */
html {
line-height: 1.15;
}
body {
position: relative;
width: 100vw;
height: 100vh;
overflow: hidden;
}
/* ////////////////////////////////////////////////////////////LAYOUT */
/* ///////////////////////////////GRID */
.layout-main {
position: relative;
width: 100vw;
height: 100vh;
display: grid;
grid-template-columns: var(--margin-left) var(--dimensions-logo) var(--padding-logo) var(--padding-logo) var(--padding-logo) 1fr var(--padding-logo) var(--padding-logo) var(--margin-right);
grid-template-rows: var(--margin-top) var(--dimensions-logo) var(--padding-logo) var(--padding-logo) var(--padding-logo) 1fr var(--padding-logo) var(--padding-logo) var(--margin-bottom);
}
.layout-content {
position: absolute;
top: var(--margin-top-content);
bottom: var(--margin-bottom-content);
overflow-y: scroll;
grid-column: 6 / 7;
grid-row: 6 / 7;
}
/* ///////////////////////////////LOGO */
.cont-logo {
grid-column: 2 / 3;
grid-row: 2 / 3;
}
.logo {
width:100%;
height: auto;
}
/* ///////////////////////////////NAVIGATION */
.cont-topheader {
grid-column: 4 / 9;
grid-row: 2 / 3
}
.cont-leftaside {
grid-column: 2 / 3;
grid-row: 4 / 7;
}
/* ///////////////////////////////FRAME */
.cont-frame {
position: relative;
grid-column: 4 / 9;
grid-row: 4 / 9;
}
.c1 {
width: var(--dimensions-logo);
height: auto;
}
.c2 {
position: absolute;
right: 0;
bottom: 0;
width: var(--dimensions-logo);
height: auto;
}
/* ///////////////////////////////BACKGROUND*/
.cont-uppergradient {
height: 100%;
background: linear-gradient(0deg, rgba(2,0,36,1) 0%, rgba(0,0,0,0) 0%, rgba(255,255,255,0.8939776594231442) 6%, rgba(255,255,255,1) 10%);
}
.cont-lowergradient {
height: 100%;
background-color: #767575;
background: linear-gradient(180deg, rgba(2,0,36,1) 0%, rgba(0,0,0,0) 0%, rgba(255,255,255,0.8939776594231442) 20%, rgba(255,255,255,1) 10%);
}
////////////////////////////////////////////////////////////NAVIGATION */
.primary-nav {
width: var(--dimensions-logo);
height: var(--dimensions-logo);
}
#primary-nav {
display: flex;
flex-direction: column;
justify-content: space-between;
width: auto;
height: 100%;
}
.primary-inf {
margin-top: var(--padding-logo);
}
#secondary-nav {
display: flex;
flex-direction: column;
justify-content: space-between;
width: auto;
height: var(--dimensions-logo);
}
.secondary-nav {
width: var(--dimensions-logo);
height: var(--dimensions-logo);
}
#secondary-nav {
width: auto;
height: 100%;
}
ul {
list-style: none;
padding-left: 0em;
}
a {
font: var(--font-family-sans-serif);
color: var(--font-navigation);
}
a:hover {
font-size: calc(var(--dimensions-logo) / 6);
}
.menu-item {
font-size: calc(var(--dimensions-logo) / 7);
text-align: center;
border-style: solid;
border-width: calc(var(--dimensions-logo) / 44);
background-color: #fff;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PAGE</title>
</head>
<body>
<div class = "layout-main">
<div class = "cont-gradient cont-uppergradient"></div>
<div class = "cont-gradient cont-lowergradient"></div>
<section class = "cont-logo">
<img class = "logo" src="link-to-logo">
</section>
<header class = "cont-topheader">
<nav class = "secondary-nav">
<?php
wp_nav_menu(
array(
'menu' => 'secondary-nav',
'container' => '',
'theme_location' => 'secondary-nav',
'items_wrap' => '<ul id="secondary-nav">%3$s</ul>',
)
);
?>
</nav>
</header>
<aside class = "cont-leftaside">
<nav class = "primary-nav">
<?php
wp_nav_menu(
array(
'menu' => 'primary-nav',
'container' => '',
'theme_location' => 'primary-nav',
'items_wrap' => '<ul id="primary-nav">%3$s</ul>',
)
);
?>
</nav>
<nav class = "primary-inf">
<?php
wp_nav_menu(
array(
'menu' => 'primary-inf',
'container' => '',
'theme_location' => 'primary-inf',
'items_wrap' => '<ul id="primary-inf">%3$s</ul>',
)
);
?>
</nav>
</aside>
<div class = "cont-heading">
<div class = "page-heading-box"><p><?php the_title(); ?></p></div>
<div class = "post-heading-box"><p><?php the_post(); ?></p></div>
</div>
<div class = "cont-frame">
<img class = "c1" src="link-to-frame-image-1"></img>
<img class = "c2" src="link-to-frame-image-2"></img>
</div>
<div class = "layout-content">
<div class = "cont-content">
<article class = "a-scrollable-article">
<p>article-testBEGINNING</p>
</div>
</div>
</div>
</body>
</html>
The problem with a layout of overlapping content is that elements may obstruct pointer-events so that the elements below are not interactive anymore.
The fast solution is to modify the obstructing elements with pointer-events:
.body {
position: relative;
}
.element-top {
position: absolute;
pointer-events: none
}
.element-bottom {
position: absolute;
background-color: red;
}
.element-bottom:hover {
background-color: green;
}
<body>
<div class = "element-bottom">_</div>
<div class = "element-top">_</div>
</body>
However this may not always be an option - for example when there are interactive elements on the layer of the top element. In this case, a more organized layout and layering can help eliminate these conflicts:
Use one main grid to assign areas for the different overlapping page-parts but make sure that only the bottom layer covers the entire viewport. Make sure that the interactive elements on the page obstruct only the place they need by confining them in subgrids.
I just have a quick question as I've run into a problem I am unable to solve. I just need a logo to be on the left of the container and navigation links (top-bar) on the left. Where is the problem in my code?
HTML:
<header>
<div class="container">
<?php
$custom_logo_id = get_theme_mod('custom_logo');
$logo = wp_get_attachment_image_src($custom_logo_id, 'full');
if (has_custom_logo()) {
echo ' <img class="site-logo" src="' . esc_url($logo[0]) . '" alt="' . get_bloginfo('name') . '"> ';
} else {
echo '<h1 class="site-logo">' . get_bloginfo('name') . '</h1>';
}
?>
<?php
wp_nav_menu(
array(
'theme_location' => 'top-menu',
'menu_class' => 'top-bar'
)
);
?>
</div>
</header>
CSS:
header .container {
height: 20%;
z-index: 99;
display: flex;
align-items: center;
}
header .container .site-logo {
align-items: flex-start;
}
header .container .top-bar {
list-style-type: none;
display: flex;
align-self: flex-end;
}
When there is a logo image, it is wrapped in an <a> tag. So the margin needs to be applied to the <a>. When there is no logo image, the margin should be applied to the <h1>.
Try:
.site-link, .site-logo {
margin-right: auto;
}
I just want to share my code here where I got the Idea on W3schools. I already search of the same topic, but it didn't solve the problem of my Code.
/* The Modal (background) MY CSS CODE*/
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding-top: 100px; /* Location of the box */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}
/* The Modal1 (background) */
.modal1 {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding-top: 100px; /* Location of the box */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}
And heres the Javascript Code
<!-- Javascript -->
<script>
// Get the modal
var modal = document.getElementByclass("myModal");
// Get the button that opens the modal
var btn = document.getElementById("myBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks the button, open the modal
btn.onclick = function() {
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>
<script>
// Get the modal1
var modal = document.getElementByIclass("myModal1");
// Get the button that opens the modal
var btn = document.getElementById("myBtn1");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close1")[0];
// When the user clicks the button, open the modal
btn.onclick = function() {
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>
And Lastly, the HTML Code
<!-- Trigger/Open The Modal -->
<button id="myBtn">Apply Now</button>
<!-- The Modal -->
<div class="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<span class="close">×</span>
<p>Some text in the Modal..</p>
</div>
</div>
<!-- Trigger/Open The Modal1 -->
<button id="myBtn1">Apply Now</button>
<!-- The Modal -->
<div class="myModal1" class="modal1">
<!-- Modal content -->
<div class="modal-content">
<span class="close1">×</span>
<p>Some text in the Modalsssssss..</p>
</div>
</div>
I've been kinda stuck with this for a long time. Any opinion or suggestions will be greatly appreciated.
**** UPDATE**
Read your comment and I agree, this is pretty "hacky" and I personally would almost never do this. So I want to show you a solution which is closer to what I would do on a production site. A quick overview:
The code for the actual modal is good, so we can use that.
What I think is "hacky" here is the way we get the data to feed to the modal. Storing data in HTML elements is not terrible, but there are way better ways to do this.
A good way to store this kind of data (strings, numbers, etc.) is with JSON.
We can include the JSON data in the document itself, or we can use AJAX to store it in a different location. Could be a file on the server, or maybe a database somewhere.
More info on $.getJSON() method.
/* Same code from previous example. New code marked with comments */
$("body").on("click", ".toggle-modal", function() {
if ($(".modal").is(":visible")) {
$(".modal").fadeOut("fast", function() {
$(this).remove()
});
} else {
/* Get the data ID from the html element */
const data_id = $(this).data("modal-text");
/* Get JSON data from file on server (simplified)
Replace the url with the path to your JSON file on the server. */
$.getJSON("https://httpbin.org/json", function(response) { // <-- Callback function
/* This is the "callback" function from the "getJSON" method
This fires when the AJAX request has been completed
This function has access to the AJAX response variable which contains the returned data */
/* Light validation here just checks if the response is json */
if (typeof response == 'object') {
/* For kicks let's check out the response */
console.log(response);
/* The response is obviously not our data. Next variable emulates correct response */
response = {
primary_modal: "The message for the primary modal",
secondary_modal: "The message for the secondary modal"
}
/* Get the correct message from the response.
Use the value from the data attribute as the key to find our data in the json structure */
const msg = response[data_id];
/* Launch the modal */
const modal = $("<div />", {
"class": "modal"
}).append(
$("<div />", {
"class": "modal-overlay toggle-modal"
}),
$("<div />", {
"class": "modal-container"
}).append(
$("<div />", {
"class": "modal-close toggle-modal"
}),
$("<div />", {
"class": "modal-content"
}).text(msg) // <-- The message
)
).appendTo("body")
.fadeIn("fast")
.css("display", "flex");
} else {
return "Couldn't get the data";
}
});
}
})
body,
html {
margin: 0;
padding: 0;
height: 100%;
width: 100%;
background: cyan;
display: flex;
justify-content: center;
align-items: center;
}
.modal {
display: none;
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 9000;
padding: 20px;
}
.modal-overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1;
background: rgba(0, 0, 0, .8);
cursor: pointer;
}
.modal-container {
background: white;
margin: auto;
position: relative;
font-size: 16px;
line-height: normal;
text-align: center;
font-weight: bold;
z-index: 20;
}
.modal-close {
position: absolute;
top: 0;
right: 0;
width: 12px;
height: 12px;
font-family: sans-serif;
font-size: 14px;
line-height: 1;
overflow: hidden;
cursor: pointer;
padding: 8px 20px;
background: coral;
color: white;
display: flex;
justify-content: center;
align-items: center;
}
.modal-close:before {
content: "X"
}
.modal-content {
padding: 35px 20px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- We removed the modal data and added a reference to the data in the json -->
<div>
<button class="toggle-modal" data-modal-text="primary_modal">Click Here</button>
<button class="toggle-modal" data-modal-text="secondary_modal">Click Here</button>
</div>
Here is a really easy way to re-purpose a modal template for different content using jQuery. Basically you store the message right in the button's data attribute, then just append that content to the dynamically generated modal.
/* Listen for the click event on the button */
$("body").on("click", ".toggle-modal", function() {
/* Check if modal is open */
if ($(".modal").is(":visible")) {
/* if modal is already visible close it out */
$(".modal").fadeOut("fast", function() {
$(this).remove()
});
} else {
/* Get message from button */
const msg = $(this).data("modal-text");
/* dynamically create modal elements */
const modal = $("<div />", {
"class": "modal"
}).append(
$("<div />", {
"class": "modal-overlay toggle-modal"
}),
$("<div />", {
"class": "modal-container"
}).append(
$("<div />", {
"class": "modal-close toggle-modal"
}),
$("<div />", {
"class": "modal-content"
}).text(msg) /* Append text to modal */
)
).appendTo("body") /* Append modal to body */
.fadeIn("fast") /* Fade in modal */
.css("display", "flex") /* Flexbox to center content */
}
})
body,
html {
margin: 0;
padding: 0;
height: 100%;
width: 100%;
background: cyan;
display: flex;
justify-content: center;
align-items: center;
}
.modal {
display: none;
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 9000;
padding: 20px;
}
.modal-overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1;
background: rgba(0, 0, 0, .8);
cursor: pointer;
}
.modal-container {
background: white;
margin: auto;
position: relative;
font-size: 16px;
line-height: normal;
text-align: center;
font-weight: bold;
z-index: 20;
}
.modal-close {
position: absolute;
top: 0;
right: 0;
width: 12px;
height: 12px;
font-family:sans-serif;
font-size:14px;
line-height:1;
overflow: hidden;
cursor: pointer;
padding: 8px 20px;
background: coral;
color: white;
display: flex;
justify-content: center;
align-items: center;
}
.modal-close:before {
content: "X"
}
.modal-content {
padding: 35px 20px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Set the modal text right in the data attribute of the button. This approach works for short messages but may not be well suited for more complex content -->
<div>
<button class="toggle-modal" data-modal-text="Short message to be displayed in the first modal">Click Here</button>
<button class="toggle-modal" data-modal-text="A different message for the secondary modal">Click Here</button>
</div>
I need help with this website I'm making for a project at school. I need help with the Nav bar at the bottom of the window when you first load the page but if you scroll down it scrolls with the page. I need it to basically align to the bottom of the window when its first loaded. I can't for the life of me figure out how to do it. Here is a reference.
Example Website
Notice how the Nav bar is always at the bottom of the window when scrolled to the top even if you resize the window. I would like to do this with only CSS and HTML, but I understand a little bit of Javascript so if It can't be done with only CSS and HTML it's alright. Thanks for the help.
The magic is in using both JQuery and CSS.
JQuery
$(document).ready(function () {
var navBarY = $(".bottom-bar").offset().top;
$(document).scroll(function () {
if ($(window).scrollTop() >= navBarY) {
$(".bottom-bar").addClass("fixed-top");
} else {
$(".bottom-bar").removeClass("fixed-top");
}
});
});
Whenever the .bottom-bar reaches the top of the window a class fixed-top is added.
CSS
.fixed-top {
position: fixed;
margin-top: 0 !important;
}
Example
$(document).ready(function () {
var navBarY = $(".bottom-bar").offset().top;
$(document).scroll(function () {
if ($(window).scrollTop() >= navBarY) {
$(".bottom-bar").addClass("fixed-top");
} else {
$(".bottom-bar").removeClass("fixed-top");
}
});
});
body {
margin: 0px;
font-family: Helvetica, Arial;
height: 2000px;
}
.bottom-bar {
width: 100%;
height: 40px;
background: #2A2A2A;
color: white;
text-align: center;
line-height: 40px;
margin-top: calc(100vh - 40px);
}
.fixed-top {
position: fixed;
margin-top: 0 !important;
}
<nav class="bottom-bar">Navigation bar</nav>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>