Is there a way to shorten the code?
Hello there,
I started learning jQuery about 2 days ago using the book "jQuery Novice to Ninja" by Sitepoint.
I'm currently working on a project where I need some jQuery-fancy to work so I startet programming my first little plugin, which works flawlessly.
This is the script-code:
- $(document).ready(function() {
- $('#aboutme').hide();
- $('#twitter').hide();
- $('#contact').hide();
-
- $('#aboutmebtn').toggle(function() {
- $('#aboutmeimg').attr("src", "bilder/aboutme-selected.gif");
- $('#twitterimg').attr("src", "bilder/twitter.gif");
- $('#contactimg').attr("src", "bilder/contact.gif");
- $('#aboutme').animate({ opacity: 'toggle', paddingTop: '+=16px' }, 'slow');
- $('#twitter').hide();
- $('#contact').hide();
- }, function() {
- $('#aboutmeimg').attr("src", "bilder/aboutme.gif");
- $('#aboutme').animate({ opacity: 'toggle', paddingTop: '-=16px' }, 'slow');
- });
- $('#twitterbtn').toggle(function() {
- $('#twitterimg').attr("src", "bilder/twitter-selected.gif");
- $('#aboutmeimg').attr("src", "bilder/aboutme.gif");
- $('#contactimg').attr("src", "bilder/contact.gif");
- $('#twitter').animate({ opacity: 'toggle', paddingTop: '+=16px' }, 'slow');
- $('#aboutme').hide();
- $('#contact').hide();
- }, function() {
- $('#twitterimg').attr("src", "bilder/twitter.gif");
- $('#twitter').animate({ opacity: 'toggle', paddingTop: '-=16px' }, 'slow');
- });
- $('#contactbtn').toggle(function() {
- $('#contactimg').attr("src", "bilder/contact-selected.gif");
- $('#aboutmeimg').attr("src", "bilder/aboutme.gif");
- $('#twitterimg').attr("src", "bilder/twitter.gif");
- $('#contact').animate({ opacity: 'toggle', paddingTop: '+=16px' }, 'slow');
- $('#twitter').hide();
- $('#aboutme').hide();
- }, function() {
- $('#contactimg').attr("src", "bilder/contact.gif");
- $('#contact').animate({ opacity: 'toggle', paddingTop: '-=16px' }, 'slow');
- });
- });
What this thing does: It displays three images side by side and when you click one, content comes into view. Only on section of the three can be displayed at the same time.
Now I was asking myself, since this is a pretty easy task if the code isn't way too long? I'm sure there are some protips which help me getting rid of a line or two.