Showing posts with label HTML. Show all posts
Showing posts with label HTML. Show all posts

Sunday, 11 May 2014

Using JavaScript To Create Buttons

I wanted to make a quick blog post on how I've used JavaScript to create the rollover effects for the buttons in my navigation. The very first button I tried to create for my website was the home button. This is because logically, it comes first (it's in the top left). If you remember from my previous post about my home button I've decided to go with the following style for it.
This required me to have a rollover effect applied, so that when a mouse hit the 'up' image it swapped to the over image. I was having a lot of trouble with it - an unexpected, discouraging amount of trouble considering it was the very first thing I attempted. That was until I saw the following video, shared with me by a friend.
I followed this video to a tee, and it worked.

Following this video has given me a better understanding of how I can use JavaScript as before I'd never used it and never really understood what it was for. I've learnt how I can put code into the <head> of the document as a <script> which can then be referenced in the <body>. This is the code that I put into the <head> of my websites HTML.

<head>
<script type="text/javascript">
<!-- Home Button -->

function imgOver(id) {
document.getElementById(id).src="./graphics/home_grey_over.png";
}
function imgOut(id) {
document.getElementById(id).src="./graphics/home_grey_up.png";
}

</script>
</head>

The only "issue" with this technique is that I have to think of a unique way of saying 'over' and 'out; each time I use it for a different button. You can see in the example above I have 'imgOver' and 'imgOut'. If I wanted to use this again for another button I'd have to copy and paste the code over, change the src images and use alternative words - like 'imgOn' and 'imgOff'.

The above code is what goes in the <head> section of my html. There is another part that must go in the <body> section:

<div class="one-third column">
<a href="index.html" onmouseover="imgOver('homeButton');" onmouseout="imgOut('homeButton');">
<img src="graphics/home_grey_up.png" width="85px" height="auto" alt="home" id="homeButton" /> 
</a>
</div>

There are certain parts of this code that I can explain to show how it links with the script in the <head> and creates a image swap link.

a href: the link that the button leads to. In this case it is a home button, so it's destination is 'index.html' - the name of the home page.
onmouseover="imgOver": the words 'onmouseover' are recognised as a command, with 'imgOver' being in the script in the <head>. When a mouse is over the image, the browser should refer to the 'imgOver' code in the <head> which tells it which image to display/swap to.
onmouseout="imgOut": when the mouse is not over/on the image, the browser should display the image that is referred to under the 'imgOut' command in the script.

All of this code has resulted in creating a home button that has a rollover/image swap effect that also works as a link. I'll be using this for every image button that I create.

Friday, 9 May 2014

Building a Website: What do I already know?

As I'm going to be 'hand-coding' an entire website out of HTML/CSS/other various web languages - well, with help from the Skeleton Framework - I figured I'd write out everything that I already know about building websites. This gives me a good idea of where I'm already at and I can identify areas which are going to be 'easy' (I use that term loosely...) for me to build and areas where I'm going to have to delve into some research.

These are also things that I've picked up in our classes at University.

3 basic categories to web pages:
- Structure (HTML etc)
- Style (CSS, CSS3 etc. CSS effects what your content "looks like")
- Interactivity(CSS, jQuery)

Elements
<p) opening tag
</p> closing tag

Anything that has a 'tag' is an element, of which there are only two types.

Type 1: Block Level Element
Aside from 'anchor', 'image' and 'span', every element is a block level element, that is transparent and invisible on it's own.

  • 100% width
    • Full width of the page or the full width of what it's inside
    • Always 100% wide, even if you define the pixels to be shown (e.g. 300px). This means it always owns the entire width of the page and the height that is explicitly defined by the content. As a result, Block Level elements stack and cannot generally be placed side by side.
  • 0 height
    • Cannot be seen, is invisible
    • It's height is defined by its content

You can add margins, padding, borders etc*

Type 2: Inline Elements
Inline images sit next to each other until you run out of space, and then will move to the next line. You cannot give inline elements padding, margin or a border.

*Margins, Padding & Borders

Padding: If you put text inside a box and you don't want it to hit the edges, you put padding to stop it doing so.
Border: The space around the padding
Margin: Empty space to give breathing room.
When adding padding etc you are increasing the defined width of the element. If you have coded the element to be 200px but then add padding etc, the element side increases. A tool such as BorderBox can help with this - includes the padding and the border in the overall size, leaves the Margin as additional.  

A HTML Page
Everything in a HTML page is wrapped in opening and closing <html> </html> tags. Within that there is;
<head>
</head>
<body>
</body>

Everything in a HTML page is wrapped in opening and closing <html> </html> tags.
Within that there is;
<head>
<link-to-css> (this then links the browser to the appropriate CSS stylesheets)
</head> (the browser leaves the 'head' with all the information required to read the rest of the document)
<body>
<insert-content-here>
</body>
Elements naturally want to get to point 0, 0 - the top left hand corner, as long as nothing is in the way. But as they stack, they will appear one by one in the order that they read in the code. This can come in handy as if you want your elements to appear in a certain order, you just code them in that order.