Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Javascript for moving DIV (WARNING: Newbie Question!)

Featured Replies

Okay, this is a real newbie question and, believe me, I have delayed in posting since I am not, in most cases, such a novice but...

 

I am finally trying to get my head around Javascript. The basic concept is easy and follows all programming languages I am already aware of and yet I cannot get a basic div to work. In fact no matter what I do I cannot get anything to work. This is really bugging me but I believe once the obvious has been stated I can then move on to more complex things.

 

The HTML is basic:

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="css/styling.css" />
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
        <title>Test Site</title>
<script type="text/javascript">
function movediv()
    {
    x =  document.getElementById('banner').style.top + 200;
    document.getElementById('banner').style.top=x+"px";
    }
</script>
    </head>
    <body onload="movediv">
        <div id="container"></div>
        <div id="banner">
            <div id="banner_lside"></div>
            <div id="banner_centre"></div>
            <div id="banner_rside"></div>
        </div>
    </body>
</html>

And the CSS is easy:

@charset "UTF-8";

html		{height: 100%}

body
		{
		height: 100%;
		width: 100%;
		background-color: #036;
		margin: 0;
		padding: 0;
		}
		
#container
		{
		margin: auto;
		width: 80%;
		height: 100%;
		background-color: #fff;
		box-shadow: 0px 0px 10px 10px #333;
		border-left: 1px solid #ccc;
		border-right: 1px solid #ccc;
		}
		
#banner
		{
		z-index: 1;
		position: absolute;
		top: 0px;
		height: 20px;
		width: 100%;
		background-color: #ccc;
		opacity:0.8;
		border-top: 1px solid #666;
		border-bottom: 1px solid #666;
		box-shadow: 0px 5px 10px 1px #444;
		}
		
	#banner_centre
		{
		position: relative;
		float: left;
		width: 80%;
		height: 100%;
		}
		
	#banner_lside
		{
		position: relative;
		float: left;
		width: 10%;
		height: 100%;
		box-shadow: -10px 5px 5px 1px #444;
		}
		
	#banner_rside
		{
		position: relative;
		float: left;
		width: 10%;
		height: 100%;
		box-shadow: 14px 5px 5px 1px #444;
		}

I have written a very simple piece of code to demonstrate what I perceive as the concept required but I am obviously making a newbie error. Would someone be kind enough to point it out and put me out of my misery?

  • Author

I told you it was a newbie question!

 

Actually quite pleased because from that one answer I now have a much greater concept of where the calls belong.

 

Thank you!

Did you figure it out? If not here's a solution

1. remove the onload attribute from the opening body tag

2 attach the function call to a window.onload event insted by placing this code just before your closing body tag

<script type="text/javascript">
window.onload=movediv();
</script>

The problem with your original code is that you reference the div before it has been instantiated

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.