
jQuery tutorial
Usiamo alcune semplici funzioni di jquery per creare alcuni effetti di sicuro successo.
Realizziamo un semplice javascript che ci permetterà di far comparire e scomparire alcune slide a seconda del pulsante premuto, in seguito ricliccando sul pulsante torneremo alla fase iniziale.
Let’s start designing our html, what we want is a simple structure made of a div containing our buttons, we will use some image as buttons, and the section we want to hide/unhide.
<div id=”image_catalogo”>
<img src=”images/marketing.jpg” class=”imgcatalogo” align=”middle” alt=”Marketing” title=”Marketing” name=”Marketing” id=”marketing_catalogo” />
<img src=”images/siti-web.jpg” class=”imgcatalogo” align=”middle” alt=”Web Site” title=”Web Site” name=”Web Site” id=”website_catalogo” />
<img src=”images/software.jpg” class=”imgcatalogo” align=”middle” alt=”Software” title=”Software” name=”Software” id=”software_catalogo” />
</div>
<div id=”marketing-text” class=”catalogo-text” style=”display:none;”>Section 1 Marketing</div>
<div id=”website-text” class=”catalogo-text” style=”display:none; “>Section 2 Web Site</div>
<div id=”software-text” class=”catalogo-text” style=”display:none;”>Section 3 Software</div>
</div>
As you can see we have created a container whose id is catalogo_description and 4 child:
- id: image_catalogo => this div contain our menu, in this example it consist in 3 images
- id: marketing-text => this div contain our first section
- id: website-text => this div contain our second section
- id: software-text => this div contain our third section
We have included some default style in our section “display:none;” this cause javascript have problem in changing the display properties inside an externalCSS
Let’s add some CSS style to this you can add it inside an external CSS as in a style tag in your html.
position: absolute;
}
#website_catalogo{
left: 2%;
z-index: 0;
}
#software_catalogo {
left:50%;
margin-left: -100px;
z-index: 1;
}
#marketing_catalogo{
left:98%;
margin-left: -200px;
z-index: 2;
}
#image_catalogo {
width: 240px;
height: 500px;
float:left;
text-align:center;
}
.catalogo-text {
position:absolute;
z-index: -1;
padding-left: 250px;
}
As you can see it’s a simple formatting css, the only important properties are the absolute positioning of the element.
Now we can add our effect.
var detailed = false;
var interrupt;
$(‘.imgcatalogo’).click(function() {
if (detailed == false) {
detailed = true;
clearTimeout ( interrupt );
$(“.imgcatalogo:not(#” + $(this)[0].id + “)”).animate({
left: ’2%’,
marginLeft: ’0px’
}, 1500 );
$(“.imgcatalogo:not(#” + $(this)[0].id + “)”).slideUp();
$(this).animate({
left: ’2%’,
marginLeft: ’0px’
}, 3000 );
interrupt = setTimeout(‘ $(“#’ + $(this)[0].id.replace(“_catalogo”, “-text”) +’”).slideDown(“fast”)’, 3000);
}
else {
detailed = false;
clearTimeout ( interrupt );
$(‘.catalogo-text’).slideUp();
$(“.imgcatalogo:not(#” + $(this)[0].id + “)”).slideDown(“fast”);
$(“img#marketing_catalogo”).animate({
marginLeft: ‘-200px’,
left: ’98%’
}, 1500 );
$(“img#website_catalogo”).animate({
left: ’2%’
}, 1500 );
$(“img#software_catalogo”).animate({
left: ’50%’,
marginLeft: ‘-100px’
}, 1500 );
$(“.imgcatalogo”).slideDown(“fast”);
}
});
</script>
Let’s explain what we do.
First we create a var “detailed” this is usefull to store the state of the page, it say if we have just opened a section or if the sections are all closed.
Next we associate an onclick event to the “imgcatalogo” class.
At first we want to make all the buttons exept the one clicked to disappear, thi is done easily with the SlideUp function of jquery, as selector for this effect we want all the elements that has class “imgcatalogo” except the one clicked, well we have the id of the clicked one so it’s easy :
Why not add a special effect before the slide? lets move all the image not clicked to the left side and then made ‘em disappear:
left: ’2%’,
marginLeft: ’0px’
}, 1500 );
This line remove the css property margin-left and set the position to left 2%, it takes 1,5 sec to do it so we will be able to see the moving elements.
Let’s move the one clicked too but slowly so we can see what’s appening:
left: ’2%’,
marginLeft: ’0px’
}, 3000 );
Now we want to make our section appear, well of course we have to wait till the effect for the images are over, let’s use a timer so:
The id of the section is the same as the id of the image clicked except the last word, we made it SlideDown fast.
Ok now the first half of our javascript is over, let’s see how to make the section disappear and the button come back at their position.
Now it comes to hand the “detailed” var so we can know in which of the two cases we are, show the section or hide it.
First of all we want the section to disappear, we can use the slideUp effect as before:
Next we want the image to comeback so we made them slideDown:
And finally we restore the CSS as it was at start:
marginLeft: ‘-200px’,
left: ’98%’
}, 1500 );
$(“img#website_catalogo”).animate({
left: ’2%’
}, 1500 );
$(“img#software_catalogo”).animate({
left: ’50%’,
marginLeft: ‘-100px’
}, 1500 );
Demo here
Update 11/04/2209 – We have modified some line of code to make it more solid since there was problem if you click on image while they are moving, the line modified are in bold character.










$(“.imgcatalogo:not(#” + $(this)[0].id + “)”).animate({
left: ’2%’,
marginLeft: ’0px’
}, 1500 );
in this code make some erore what can i do?
Heya nice website, I was wondering what anti-spam program you use for feedback since i recieve a ton of spam on my web-site.
[...] Crespo Antonio Creus Larry Crockett Tony Crook Art Cross Geoff Crossley Chuck Daigh Yannick Dalmas Derek Daly Christian [...]
[...] Brooks Alan Brown Walt Brown Warwick Brown Adolf Brudes Martin Brundle Gianmaria Bruni Jimmy Bryan Clemar Bucci [...]
Good information. Be great if you might possibly be able to write much more about this area of interest, because it looks there is not a lot of first-rate posts like these around.
Is China Ever going To Be Free…
[...]Writing about a topic like this is pretty tough. It’s good to understand some individuals nevertheless care about this[...]…
Hmm it looks like your site ate my first comment (it was super long) so I guess I’ll just sum it up what I wrote and say, I’m thoroughly enjoying your blog. I as well am an aspiring blog writer but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d genuinely appreciate it.
I really glad to locate this web site on bing, just what I was seeking for : D as properly saved to favorites . 139482
We clean up on completion. This might sound obvious but not numerous a plumber in Sydney does. We wear uniforms and always treat your home or office with respect. 466830
you use a great weblog here! do you require to make some invite posts on my blog? 357151
Sources…
[...]check below, are some totally unrelated websites to ours, however, they are most trustworthy sources that we use[...]……
Useful information. Fortunate me I discovered your website by accident, and I am stunned why this accident didn’t took place in advance! I bookmarked it.
Techniques for dilution antimicrobial susceptibility beadlets for beagles that grow aerobically-fifth edition. 740058
Hello there, just became alert to your blog via Google, and discovered that it
Thank you for your extremely great info and respond to you. san jose car dealers 693224
Really good style and great content material material , absolutely nothing else we want : D. 401622
Could you provide some examples of jquery fade hide/show effect ? I need it more. Thanks
thank you
good script
Nicely picked details, a lot of thanks towards the author. It is incomprehensive in my experience at present, even so in common, the convenience and importance is mind-boggling. Regards and all of the very best .. 428224
Soccer Shoes Adidas…
Awesome story ! You have raised a little very astute statements also I appreciate the work you have put into your writing. Its open in order that you know what you are writing in relation to. I am excited in order to interpret more of your website list…
I gotta bookmark this internet web site it seems incredibly beneficial invaluable 734321
Should you tow a definite caravan nor van movie trailer your entire family pretty soon get exposed towards the down sides towards preventing finest securely region. awnings 45734
We will have a link exchange agreement among us! 445644
This article is very appealing to thinking people like me. It
Hey man, .This was an excellent page for such a hard subject to talk about. I look forward to reading many more great posts like these. Thanks 22194
Someone essentially lend a hand to make critically articles I
Really superb details can be located on weblog . 572519
Likely to commence a business venture around the refers to disclosing your products and so programs not just to individuals near you, remember, though , to several potential prospects much more via the www often. earn cash 717493
so considerably very good data on here, : D. 276747
Music began playing any time I opened this web site, so frustrating! 633794
Exceptional post even so , I was wanting to know in case you could write a litte far more on this subject? I
Outstanding post, I feel weblog owners need to larn a whole lot from this weblog its actually user genial . 14949
I provide Chinese translation service…
[magnificent post, very informative. I wonder why the other experts of this sector do not notice this. You should continue your writing. I am sure, you’ve a huge readers’ base already!I’d like to add more information to your post so as to have visit…
Just to say that the demo link is dead
Public Auction…
Hi there, i read your blog from time to time and i own a similar one and i was just wondering if you get a lot of spam comments? If so how do you reduce it, any plugin or anything you can recommend? I get so much lately it’s driving me mad so any assi…
Couldn’t have arrived at a better moment. Awesome post
I have to admit its not easy to grasp esipecially if you are new to javascript like me. i am working on a my first php project an ebook script but i have come through alot of challenges when it comes to using javascript. let me just say sites like this one makes and play a greater part in our day to day programming practices.