Wednesday, 10 December 2014

Christmas Candle With jQuery

Christmas Candle With jQuery
Place the mouse pointer over the wick
to put on the fire and click to blow it out.

In my last post I designed a Christmas tree so I felt like making another thing related to Christmas.

This time it is a Christmas candle which you can lit and blow out. The fire and the candle parts are created by div elements. We want to lit the fire when we roll over the candle wick with the mouse. This is done by the mouseover event inside the HTML code.

Now, we want that the candle glimmers in the dark so we use jQuery because with it it's easy to write a code for animated effects. The glimmer effect is created by changing the opacity of the div fire element with the following syntax animate( {opacity:"+= or -= some number between 0 to 1"},{duration:milliseconds});. "+=" creates a fade in effect and "-=" a fade out effect.

By clicking the fire, the candle should stop glimmering. We put the onclick event inside the HTML code and connect it with the function called blow(). jQuery hide() hides the fire element.







HTML & CSS & JavaScript Code


Open The Door With CSS Hover

Open the Door!
Drag the mouse over the door and open it!

The door image is placed inside the door frame created with a div element. The door itself is a png image file.

The CSS transform-origin:right property lets the door shrink from the left to the right side which provides the impression as if the door is opening if you put the cursor on top of the door. To show the effect in all browsers the prefixes -ms- for Internet Explorer and -webkit- for all other browsers have to be put in front of transform-origin.

The duration of the animation is set to 5 seconds with the name "open". The @keyframes property describes details of the animation "open". We want to make a lateral rotation of the door therefore we use the rotateY(deg) property. @keyframes needs the prefix -webkit- as well.

Note:the hover effect is not working on a touch screen.


CSS & HTML Code

Tuesday, 9 December 2014

Blinking Xmas Tree With JavaScript Math.random()

Blinking Xmas Tree With JavaScript Math.random()

Click on the top and let it blink



It's Xmas time! When I was walking in downtown seeing all this beautiful Xmas decorations I was inspired to make my own Xmas tree with JavaScript.

A tree usually has a crown and trunk. The crown of the Xmas tree is made of 3 div elements. 1 green div element has white div elements on the right and left side creating a triangle. The decoration balls are also div elements with a border-radius of 50% defined in CSS.

Now the Xmas tree is ready to blink. So let's look at the JavaScript code. We want that the balls change colours randomly in a certain interval. The Math object with the random () method generates random numbers between 0 and 1; the syntax is Math.random().

Next, we define the colours for each ball with document.getElementById("id of the element").style.backgroundColor=#some color. Now, with Math.random() we generate random numbers and if the number is smaller than 0.5 the balls have colour A and if it is bigger than 0.5 they have colour B.

The method setTimeout evaluates the function for each half second generating random numbers and creating the blinking effect.

Save the CSS file with XMas_tree.css and the JavaScript file with XMas_tree.js otherwise it will not work. Our you can change the file name of css and js in the HTML code.

HTML Code

CSS Code

JavaScript Code

Monday, 8 December 2014

Futuristic Clock With Javascript And jQuery

Futuristic Clock With JavaScript
How about a futuristic looking clock for a web widget?

For fade out and fade in animation we use JavaScript and jQuery.

The clock is designed with CSS. Each panel has its own div element. The opacity of the green squares is set to 0 meaning that they are basically invisible. The panel showing the actual time is another div element. Never put the div elements associated with the green squares inside the panel element! Because if you set JavaScript both to a parent and child elements the script will not fire. Both codes have to be in different elements.

Now, we have designed the clock and the squares so we are ready to make them work together. First of all, the time should be shown in the center panel so we create a Date() object with JavaScript. Get the hours with getHours(), the minutes with getMinutes() and the seconds with getSeconds() and concatenate the methods with the date object.

Next, for each second passed by a square should fade out and the next should fade in. This is can be done with the animate() method of jQuery which is a part of JavaScrip library and is very useful.

The elements are choosen through their ids with ("$id of the element") and the animation is created with animate({}). The details of the animation are described in the curly brackets. We want that the squares will fade in gradually so we use opacity:"+=1". After 1 s has passed we want to make sure that the same square will fade out so we write opacity:"-=1". Note that if you use hide() once the square has vanished the same square will never appear again.

To link the div squares with the seconds we use the if/else conditional. The if/else conditional is inside the clock() function so we can use the variables declared previously.


HTML Code



JavaScript Code



CSS Code

Wednesday, 3 December 2014

Name Tag With Border Style

Name Tag With Border Style
Fat Ladies
Here is another text design looking like a name tag of your clothes. The stitched are created with border-style: dashed. For the top use border-top-style and for the bottom use border-bottom-style. Change the text inside the label and make your own one!

Monday, 1 December 2014

Blinking Neon Sign With jQuery

Blinking Neon Sign With jQuery
Welcome!
You can spice up your website with some JavaScript animation. The example shows a text which fades out and fades in 10 times in a row with the while loop.

jQuery ―which uses the $ sign in front of each element ―makes it easy to write codes for animations. The blinking starts with the value var i=0 and finished after blinking for 10 times which is set as while(i<10). In function() you include the fadeOut() and fadeIn() methods to create the blinking effect. For details, have a look at the code!

CSS, HTML & JavaScript Code