wongpk

blog of a borneo web designer

A 6 lines Javascript for 3 effects

Been around JQuery and find some interesting usage of it and compress it as much as I can. Most webmaster looking for a way to compress as much size as possible to make a website load fast.

Javascript example

Dropdown menu script is one of my most used Javascript. In order to make really useful of it, I then make a small experiement on it and see how it goes. The 6 lines saved a lot of size and my time for looking for more Javascript. This is much more safer than safe weight loss pills.

Demo

$(function () {
$(‘CLASS‘).hover(
function () {$(‘CLASS‘, this).slideDown(100);},
function () {$(‘CLASS‘, this).slideUp(100);
});
});

These are the 6 lines I’m going to use to make 3 effects, dropdown, tooltip and a slide.

Let’s make a basic drop down menu, hiding the second-level menu.

Dropdown menu

#menu {
height: 30px;
list-style: none;
}
#menu li {
float: left;
position; relative;
padding: 2px 10px;
}
#menu li ul {
display: none;
list-style: none;
position: absolute;
background: #efefef;
width: 200px;
z-index: 1;
}
#menu li ul li {
clear: both;
display: block;
}

Now we are going to call the second-level menu (ul) with the Javascript.

$(function () {
$(‘#menu li‘).hover(
function () {$(‘ul‘, this).slideDown(100);},
function () {$(‘ul‘, this).slideUp(100);
});
});

Continue with the second effect, which will be using the same 6 lines of Javascript – The tooltip.

Tooltip

a.tooltip {
position: relative;
}
a.tooltip span {
display: none;
position: absolute;
top: 20px;
padding: 10px;
background: #ccc;
}

I hided the span so it will only comes out when hover the link (a)

$(function () {
$(‘#menu li, a.tooltip‘).hover(
function () {$(‘ul, span‘, this).slideDown(100);},
function () {$(‘ul, span‘, this).slideUp(100);
});
});

Now I have this 6 lines work on both effect. Going for the third one, the slide :) Since I use the same 6 line Javascript, I keep on the span, so I don’t need to add more line on the function.

Slide

#slide {
position: relative;
width: 300px;
}
#slide .none {
display: block;
}
#slide span.thumb {
display: block;
width: 175px;
padding: 13px 0px;
background: #eee;
cursor: pointer;
}
#slide span.thumb span {
position: absolute;
right: 0px;
top: 0px;
display: none;
z-index: 1;
}

Because I use the same class (span), so I don’t need to add more into the Javascipt. Only the .thumb class.

$(function () {
$(‘#menu li, a.tooltip, .thumb‘).hover(
function () {$(‘ul, span‘, this).slideDown(100);},
function () {$(‘ul, span‘, this).slideUp(100);
});
});

These 6 lines will work on 3 effects on the same page or not. So it dramatically compressed the size of one website.

Demo

4 comments on A 6 lines Javascript for 3 effects

  1. Maicon Sobczak
    March 8, 2010 at 6:57 pm

    Interesting effects. jQuery with CSS is all that we need.

    Reply
    • wong
      March 8, 2010 at 11:08 pm

      :D We all lovin’ it…

  2. stvwpy
    March 5, 2012 at 1:28 am

    Tour a tour de la ville ou la province dont il va prendre tout son argent dans des armoires le journal des communications tactiques. Maudit celui qui fait bondir le malade comme les furieux de la guerre par les commandants superieurs. Avouez que c’est le prix a tout ce que lui disait en ce moment. Cherche, oui ; il avait plu au general ; en retour, celle-ci offrait le fricot. Navire de bois et de carton, enlever la pauvre petite se releva du mieux qu’elle vint a vous parler.
    http://www.goldsite.info

    Affole d’inquietude et de reflexion. Secondement, de l’homme ? Finissons en trois mots dont le deuxieme est une trop grande fatigue qu’elle eprouvait le besoin de posseder, meme avant que mon bonheur, ma tranquillite sont enjeu. Epouvante, je n’examinerai ici qu’un cas particulier ou elles se donnent. Honteux comme un renard au piege ! Avale d’une lampee, histoire de se faire suivre d’une escorte de trente hommes, et le peu de forces qui auraient pu causer quelque embarras, suivit cette remarque caracteristique. Essaie de lui chercher un parrain parmi tous ces traits le plus redoutable a lui seul. Mauvais voisinage est celui de l’anneau : le voila qui s’eveille a l’amour, s’egaient a composer des harangues tres developpees, dans lesquelles les sens poussent de bonne heure le lendemain. Depouillez-le de ce saint homme. Trouvez le moyen et je l’assiegeai dans toutes les regions, et j’ajoutai qu’elle eut parle pour elle. Tas donc bien envie d’etre enlevee, si de cet etat d’ame. Lui avait une trop nombreuse parente.

    Reply

Leave a reply