Javascript - Loading Multiple functions onLoad - javascript

I started learning javascript this week and I'm getting my head round the basics. I've started building what will be a "FAQ" section for a page that uses a toggle switch to open and close target divs.
However, by default the div's display is set to visible. I've put a function in that will set this to hidden, so that the dropdown's are collapsed on page load. I've tried stacking them (the toggle function, and display functions), separating them with a semi-colon within "onLoad" in the body tag.
Now, before I applied these functions to run "onLoad" both worked fine. However now only the 2nd function works that toggles the divs, but the function stating to have the divs collapsed onLoad is not.
Where am I going wrong here?
Also, seeing as I'm new to this, if there's a better way, or more shorthand version of this feel free to let me know :)
function toggleOnLoad() {
document.getElementById('dropdown01').style.display = 'none';
}
function toggleFunction() {
var dropDown = document.getElementById('dropdown01');
var dropDownCollapse = document.getElementById('toggle-image').src = "Images/banner_toggle_collapse.png";
var dropDownExpand = document.getElementById('toggle-image').src = "Images/banner_toggle_expand.png";
if (dropDown.style.display != 'none') {
dropDown.style.display = 'none';
document.getElementById('toggle-image').src = dropDownExpand;
} else {
dropDown.style.display = '';
document.getElementById('toggle-image').src = dropDownCollapse;
}
}
css: body {
background-color: #cccccc;
padding: 0;
margin: 0;
}
.container {
margin-left: 20%;
margin-right: 20%;
background-color: white;
padding: 1em 3em 1em 3em;
}
.toggle-header {
padding: 0.5em;
background-color: #0067b1;
overflow: auto;
}
#toggle {
border: none;
width: 300;
height: 3em;
background-color: #0067b1;
outline: 0;
}
.button-container {
float: right;
margin-right: 0.5em;
display: inline-block;
}
.dropdowns {
padding: 2em;
background-color: #eeeeee;
}
HTML & Javascript:
<body onLoad="toggleOnLoad(); toggleFunction()">
<div class="container">
<div class="toggle-header">
<div class="button-container" title="">
<button id="toggle" onClick="toggleFunction()">
<img id="toggle-image" src="" alt="toggle" style="max-height: 100%; max-width: 100%">
</button>
</div>
</div>
<div id="dropdown01" class="dropdowns">
<span>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</span>
</div>
</div>
</body>

Do create an init function manually.
window.addEventListener("load", myInit, true); function myInit(){ // call your functions here.... };
By doing this you can call that set of functions anytime.

The better way to do it i believe is to call your functions inside window.load instead of the body as follows:
window.onload=function(){
toggleOnLoad();
toggleFunction();
}

Your idea is correct. Probably we can simplify the implementation.
First, let's define a new class hidden to remove elements.
.hidden {
display: none;
}
Now we just can toggle this class to show and hide the content.
function toggleFunction() {
...
content.classList.toggle('hidden')
...
}
Also remove toggleOnLoad function from the body and add hidden to the content div
<body onLoad="toggleFunction()">
...
<div id="dropdown01" class="dropdowns hidden">
...
</body>
Finally, the toggleFunction must add the right class based on the hidden class of the content.
function toggleFunction() {
dropDown.classList.remove(expand, collapse)
...
const state = content.classList.contains('hidden') ? collapse : expand
dropDown.classList.add(state)
}
This is functional snipped:
const content = document.getElementById('dropdown01')
const dropDown = document.querySelector('#toggle-image')
const collapse = 'fa-plus-square'
const expand = 'fa-minus-square'
function toggleFunction() {
dropDown.classList.remove(expand, collapse)
content.classList.toggle('hidden')
const state = content.classList.contains('hidden') ? collapse : expand
dropDown.classList.add(state)
}
body {
background-color: #cccccc;
padding: 0;
margin: 0;
}
.container {
margin-left: 20%;
margin-right: 20%;
background-color: white;
padding: 1em 2em 1em 2em;
}
.toggle-header {
padding: 0.5em;
background-color: #0067b1;
overflow: auto;
}
#toggle {
border: none;
width: 300px;
height: 2em;
background-color: #0067b1;
outline: 0;
}
.button-container {
float: right;
margin-right: 0.5em;
display: inline-block;
}
.dropdowns {
padding: 1em;
background-color: #eeeeee;
}
.hidden {
display: none;
}
<script src="https://kit.fontawesome.com/0c7c27ff53.js" crossorigin="anonymous"></script>
<body onLoad="toggleFunction()">
<div class="container">
<div class="toggle-header">
<div class="button-container" title="">
<i id="toggle-image" class="fas fa-plus-square" onClick="toggleFunction()"></i>
</div>
</div>
<div id="dropdown01" class="dropdowns hidden">
<span>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</span>
</div>
</div>
</body>

Related

Show Navbar when increase scroll, hide Navbar when decrease scroll

Currently i want to show my navbar when i am scrolling on my webpage and hide my navbar when i am scrolling back. I search all over on Google, and i could not find something that could help me. I am surprised that this wasn't on the web yet. Maybe i didn't search good enough, however, after several hours of frustrating myself, i decided to get some help from the pro's here. ;-)
Would appreciate any help!
<body>
<nav>
<ul>
<li>Help</li>
</ul>
</nav>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"</script>
</body>
nav{
position: fixed;
padding: 30px 70px;
width: 100vw;
height: 10vh;
background: black;
transition: .5s ease;
z-index: 5;
}
nav ul{
float: right;
margin-right: 100px;
padding: 0px 40px;
}
$(window).scroll(function () {
var scroll = $(window).scrollTop();
var p = $win.scrollTop() / max;
var x = window.matchMedia("(max-width: 800px)")
console.log(scroll);
if (scroll > 0) {
$('nav').css({
"top": "0%",
});
} else {
$('nav').css({
"top": "-25%",
});
};
});
I think you mean something like that https://www.w3schools.com/howto/howto_js_navbar_slide.asp
Or something like this:
let lastScrollTop = 0;
function scrollFunction() {
scroll = document.body.scrollTop || document.documentElement.scrollTop;
if (scroll > lastScrollTop) // Scrolling Down
document.getElementById("navbar").style.top = "0";
else // Scrolling Up
document.getElementById("navbar").style.top = "-50px";
lastScrollTop = scroll;
}
window.onscroll = function() {scrollFunction()};
let lastScrollTop = 0;
function scrollFunction() {
scroll = document.body.scrollTop || document.documentElement.scrollTop;
if (scroll > lastScrollTop) // Scrolling Down
document.getElementById("navbar").style.top = "0";
else // Scrolling Up
document.getElementById("navbar").style.top = "-50px";
lastScrollTop = scroll;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
margin: 0;
background-color: #f1f1f1;
font-family: Arial, Helvetica, sans-serif;
}
#navbar {
background-color: #333;
position: fixed;
top: -50px;
width: 100%;
display: block;
transition: top 0.3s;
}
#navbar a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 15px;
text-decoration: none;
font-size: 17px;
}
#navbar a:hover {
background-color: #ddd;
color: black;
}
</style>
</head>
<body>
<div id="navbar">
Home
News
Contact
</div>
<div style="padding:15px 15px 2500px;font-size:30px">
<p><b>This example demonstrates how to slide down a navbar when the user starts to scroll the page.</b></p>
<p>Scroll down this frame to see the effect!</p>
<p>Scroll to the top to hide the navbar.</p>
<p>Lorem ipsum dolor dummy text sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
</body>
</html>
And someone already did it in jQuery
How can I determine the direction of a jQuery scroll event?

Dialog with a fixed footer

I have a dialog with some items ("tags") and a footer with a "done" button. When the items get populated enough, scroll is added and the footer is shown BENEATH all the items.
But I want to show the footer at all times, to be fixed, and make the scroll only work for the populated items.
<Dialog
hidden={this.state.hideDialog}
onDismiss={this._closeWithoutSavingDialog}
containerClassName={'ms-dialogMainOverride ' + styles.textDialog}
modalProps={{
isBlocking: false,
}}>
<div className={styles.tags}>
<div className={styles.tagsDialogCloud}>
{this.state.dialogTags.map((tag, index) => this.renderDialogTags(tag, index))}
</div>
</div>
<DialogFooter>
{this.state.showDialogButton == true ?
<DefaultButton
style={{ backgroundColor: '#ff0033', color: '#ffffff' }}
onClick={this._closeDialog}
text="Done"
/>:null
};
</DialogFooter>
</Dialog>
</div>;
enter image description here
Picture shows an example of what I want to achieve. The "tags" part is scrollable, but the "Done" button is always shown.
This sounds more like a CSS problem than a React one.
The problem with your code is that the DialogFooter component should be absolute so it won't extend the parent's height and you should render the button without a condition.
Here's an example of how you can achieve it:
JSX:
<div className="popup-wrapper">
<div className="popup">
<div className="popup-content">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim
ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.
</div>
<div className="popup-footer" />
</div>
</div>
CSS:
.App {
font-family: sans-serif;
text-align: center;
}
.popup-wrapper {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.popup {
height: 300px;
width: 200px;
background: grey;
position: relative;
}
.popup-footer {
height: 40px;
width: 100%;
position: absolute;
bottom: 0;
background: silver;
}
.popup-content {
max-height: 260px;
overflow: auto;
}
And a sandbox can be found here
Try this HTML as your dialog code/you can modify the code as per your dialog this is a code to give you a hint.
<div id="header" style="position:absolute; top:0px; left:0px; height:200px; right:0px;overflow:hidden;">
</div>
<div id="content" style="position:absolute; top:200px; bottom:200px; left:0px; right:0px; overflow:auto;">
</div>
<div id="footer" style="position:absolute; bottom:0px; height:200px; left:0px; right:0px; overflow:hidden;">
</div>
Hope this helps...
Happy coding...

How to use external js file?

Trying to move a js script from the html file to an external js file and I can't get it to work.The js script works fine when in the html file but when I try to use it from an external js file it does not work.I've looked up how to import external js file and I think I'm doing it right but my script is not working.What am I doing wrong?
The HTML file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>NAVBAR</title>
<meta name="description" content="NAVBAR - A responsive nav manu." />
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="style.css" />
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<!--
<script type="text/javascript">
$(document).ready( function(){
$('.m_btn').click( function(){
$('.menu').stop().slideToggle();
});
});
</script>
-->
</head>
<body>
<header>
<div class="container">
<div class="logo">NAVBAR</div>
<a href="javascript:void(0);" class="m_btn">
<span></span>
<span></span>
<span></span>
</a>
<nav class="menu">
<ul>
<li>home</li>
<li>about</li>
<li>contact</li>
</ul>
</nav>
</div>
</header>
<main>
<h1>Lorem ipsum</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</main>
<footer>
<p>NAVBAR 2018</p>
</footer>
<script type="text/javascript" src="navbar.js"></script>
</body>
</html>
The js:
$(document).ready( function(){
$('m_btn').click( function() {
$('.menu').stop().slideToggle();
});
});
The CSS:
* {
margin: 0px;
padding: 0px;
}
.container {
max-width: 1170px;
margin: 0 auto;
position: relative;
padding: 0 20px;
}
header {
background: #ffffff;
border-bottom: 1px solid #2d2d2d;
padding: 30px 0;
}
.logo {
float: left;
font-size: 20px;
font-weight: bold;
}
.menu {
float: right;
}
.menu ul {
}
.menu ul li {
float: left;
margin-left: 40px;
list-style-type: none;
}
.menu ul li a {
color: #2d2d2d;
font-size: 14px;
text-decoration: none;
}
.menu ul li a:hover {
color: #d2d2d2;
text-decoration: none ;
}
.m_btn {
float: right;
display: block;
width: 35px;
}
.m_btn span {
background: #2d2d2d;
height: 2px;
display: block;
margin: 5px 0;
}
/* Mobile */
#media only screen and ( max-width: 599px ) {
.m_btn {
display: block;
}
.menu {
display: none;
width: 100%;
margin-top: 1px;
}
.menu ul li {
margin-top: 3px;
margin-left: 0;
padding: 10px 0;
border-top: 1px solid #2d2d2d;
float: none;
}
}
/* Desktop */
#media only screen and ( min-width: 600px ) {
.m_btn {
display: none;
}
.menu {
display: block !important;
}
}
Thank you!
1) Check typo $('m.btn')...
2) Put your scripts before closing body tag.
3) If you run your site locally - delete / before navbar.js in the path.
Navbar.js must be in the same directory with your index.html file.
This must help.
It appears to me that the browser is not able to load the script at all, probably because it cannot find it. I would suggest ensuring the filename is spelled exactly the same as the file. If it is in the same directory, try removing the first slash. Also, open up the web terminal (CTRL + SHIFT + I on most browsers) and see what the console says in terms of errors.

How to insert a Pop up into a Div

I have been working on a Pop up but i'm unable to properly place it inside the container...
$(document).ready(function() {
$('.show-popup').on('click', function() {
$('.popup').fadeIn();
});
$('.close_pop').on('click', function() {
$('.popup').fadeOut();
});
});
var max = true;
function expand_collapse(elem) {
var top_div = elem.parentNode.parentNode.parentNode;
if (max === false) {
elem.innerHTML = "▼";
top_div.classList.toggle("minimized", false);
top_div.classList.toggle("maximized", true);
max = true;
} else if (top_div.classList.contains("maximized")) {
elem.innerHTML = "▲";
top_div.classList.toggle("minimized", true);
top_div.classList.toggle("maximized", false);
max = false
}
}
function close_pop(elem) {
var top_div = elem.parentNode.parentNode.parentNode;
top_div.style.display = 'none';
if (top_div.classList.contains("maximized")) {
max = false;
}
};
.container {
height: 30px;
width: 100%;
position: fixed;
bottom: 0;
right: 0;
background-color: red;
}
.popup {
display: none;
}
.pop_out {
background: #333;
border-radius: 5px 5px 0 0;
box-shadow: 0px 0px 10px #000;
}
.minimized {
display: inline-block;
margin-right: 10px;
bottom: 0;
width: 250px;
height: 60px;
overflow: hidden;
}
.maximized {
top: 0;
position: fixed;
display: block;
width: auto;
height: auto;
}
.close_pop {
cursor: pointer;
color: #fff;
}
.close_pop:hover {
color: red;
}
.expand_collapse {
margin-right: 10px;
cursor: pointer;
color: #fff;
height: 3px;
}
.expand_collapse:hover {
color: #ccc;
}
a {
position: fixed;
top: 150;
}
<html>
<link href="./index.css" rel="stylesheet" type="text/css">
<a class="show-popup" href="#">CLICK HERE</a>
<!--Right Here -->
<div class="popup" style="position:fixed;bottom:0px;">
<div class="pop_out maximized">
<div style="padding:2px;position:relative;"> <span style="margin-left:10px;">Tab 1</span>
<span style="position:absolute;right:15px;">
<span class="expand_collapse" onclick="expand_collapse(this);">▼</span>
<span class="close_pop">&times</span></span>
</div>
<div style="background:white;font-size:15px;padding:2px;">The standard Lorem Ipsum passage, used since the 1500s "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est
laborum.
</div>
</div>
</div>
<div class="container">
text
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<script src="./index.js"></script>
</html>
I'm trying to insert the pop up into the container whenever the minimize button is clicked. Any help will be appreciated.
Change .popup container from position:fixed to position:absolute
Add below css in .minimized class
.minimized {
position: relative;
z-index: 1;
vertical-align: bottom;
}
bottom property works when position is specified to the container.
position:relative will come handy in this scenario.
Try to reduce the height (approzimately more then the red div).
$(document).ready(function() {
$('.show-popup').on('click', function() {
$('.popup').fadeIn();
});
$('.close_pop').on('click', function() {
$('.popup').fadeOut();
});
});
var max = true;
function expand_collapse(elem) {
var top_div = elem.parentNode.parentNode.parentNode;
var fatherDiv=elem.parentNode.parentNode.parentNode.parentNode;
if (max === false) {
elem.innerHTML = "▼";
top_div.classList.toggle("minimized", false);
top_div.classList.toggle("maximized", true);
max = true;
fatherDiv.style.zIndex="2";
} else if (top_div.classList.contains("maximized")) {
elem.innerHTML = "▲";
top_div.classList.toggle("minimized", true);
top_div.classList.toggle("maximized", false);
max = false;
fatherDiv.style.zIndex="0";
}
}
function close_pop(elem) {
var top_div = elem.parentNode.parentNode.parentNode;
top_div.style.display = 'none';
if (top_div.classList.contains("maximized")) {
max = false;
}
};
.container {
height: 89px;
width: 100%;
position: fixed;
bottom: 0;
right: 0;
background-color: red;
}
.popup {
display: none;
}
.pop_out {
background: #333;
border-radius: 5px 5px 0 0;
box-shadow: 0px 0px 10px #000;
}
.minimized {
display: inline-block;
margin-right: 10px;
width: 100%;
height: auto;
overflow: auto;
z-index: 0;
top: 89px;
position: fixed;
}
.maximized {
top: 89px;
position: fixed;
display: block;
width: auto;
height: auto;
z-index: 2;
}
.close_pop {
cursor: pointer;
color: #fff;
}
.close_pop:hover {
color: red;
}
.expand_collapse {
margin-right: 10px;
cursor: pointer;
color: #fff;
height: 3px;
}
.expand_collapse:hover {
color: #ccc;
}
a {
position: fixed;
top: 150;
}
<html>
<link href="./index.css" rel="stylesheet" type="text/css">
<a class="show-popup" href="#">CLICK HERE</a>
<!--Right Here -->
<div class="popup" style="position:fixed;bottom:0px;z-index:2;">
<div class="pop_out maximized">
<div style="padding:2px;position:relative;"> <span style="margin-left:10px;">Tab 1</span>
<span style="position:absolute;right:15px;">
<span class="expand_collapse" onclick="expand_collapse(this);">▼</span>
<span class="close_pop">&times</span></span>
</div>
<div style="background:white;font-size:15px;padding:2px;">The standard Lorem Ipsum passage, used since the 1500s "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est
laborum.
</div>
</div>
</div>
<div class="container">
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<script src="./index.js"></script>
</html>
Log:
new var fatherDiv that is the container div "Popup";
add "fatherDiv.style.zIndex='2'" to set the Div in front of the red Div (z-index work like a layer,is like 2 layer of paper).zIndex work only on the main container of a thing (in this case the Popup Div,may doesn't work on the children in some case);
add the "top" and "position" rule in .minimized because you can't use top without the position rule (i balance the position only to show you this result,you may balance this value in your file)
Say me what you think about this.

Make a div have a top-most position?

I'm not quite sure how to ask this question. What I'm trying to do is have an element on the bottom of my page that will always stick to the bottom, but if the page is too short it will be pushed down by the other elements on the page.
Example:
The left side middle graphic and the right titles are all contained within #mainBodyContent. If the browser window is small enough that this div runs into the "Latest in Geek" flash box, I want the flash box to be pushed down. However if #mainBodyContent isn't running into the flash box, I want the flash box to stick to the bottom in the place where it's at currently. I'm rather confused as how to pull it off.
Any suggestions/help out there? Am I making sense?
You can use absolute positioning combined with padding. Example can be seen here:
Static Footer Example
<html>
<head>
<style type="text/css">
html, body { height:100%; }
/* layout */
#container {
width:800px; margin: 0px auto;
position: relative; min-height: 100%;
}
#top { height: 12px; } /* margin on header breaks liquid layout */
#header { position: relative; }
#content { padding-bottom: 72px; } /* 60px footer height plus 12px padding */
#footer {
clear: both;
position:absolute;
bottom: 0px;
width:100%;
height:60px;
text-transform: uppercase;
background-color: red;
}
.section {
margin-top: 12px;
padding-top: 12px;
border-top: 1px solid black;
border-bottom: 1px solid black;
}
</style>
</head>
<body>
<div id="container">
<div id="top"></div>
<div id="header">
<h1>Title</h1>
</div><!-- end header div -->
<div id="content">
<div class="section">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
</div><!-- end content div -->
<div id="footer">Footer content here</div>
</div><!-- end container div -->
</body>
Try using the CSS selector position:fixed
A quick stab gives you most of what you're looking for. The concern (for me) is that while the resulting footer is at the bottom of the page, the elements above will run underneath it. Some scripting could change that (and is likely how it's done in your example), but this is a start.
<html>
<head>
<style>
#footer {
position: fixed;
bottom: 0px;
background-color: Red;
height: 200px;
}
*html #footer {
position: absolute;
bottom: 0px;
background-color: Red;
height: 200px;
}
</style>
</head>
<body>
<div>A test</div>
<div id="footer">Here's my footer</div>
</body>
</html>
Compare the height of your content to the height of your window. If the window is larger than the height of your content, then set the footer to position: fixed; bottom: 0;.
css:
#footer
{
bottom: 0; /* this line does nothing until you set position: fixed; */
}
javascript:
function setFooterPosition()
{
var foot = document.getElementById("footer");
var docEl = document.documentElement;
foot.style.position = docEl.clientHeight < docEl.scrollHeight ? "" : "fixed";
}
window.onload = setFooterPosition;
window.onresize = setFooterPosition;

Categories