|
|
I've been quite busy over the past few weeks with my clients and articles on SML. Today, I found sometime to work out a simple trick based on Jquery - Changing Backgrounds Dynamically. Well, I hope you read the article about "Adaptable View On Blogger" wherein, I mentioned we can tweak the design of our blog dynamically. We'll be using the same concept in this article as well...
I don't want to take too much of your time. So, I'll just explain the basics about the working of the script. The script will add or remove classes to the body element based on what list item the user clicks on. That's it. So, take a look at the demo of this simple effect by visting the link below and we'll get started...
CHANGING BG COLOR DYNAMICALLY - LIVE DEMO
1. Navigate to "Design>>Edit HTML" and make a backup of your template by clicking on the "Download full template" link at the top of the page. Once the back up is over, search for the following piece of code.
]]></b:skin>
2. Paste the following piece of code just above the line mentioned step 1.
.bg1 { background: url(BG IMAGE URL) repeat-x; background-color: #6c0000; }
.bg2 { background: url(BG IMAGE URL) repeat-x; background-color: #5A2A00; }
.bg3 { background: url(BG IMAGE URL) repeat-x; background-color: #00345B; }
.bg2 { background: url(BG IMAGE URL) repeat-x; background-color: #5A2A00; }
.bg3 { background: url(BG IMAGE URL) repeat-x; background-color: #00345B; }
3. Next, search for the following piece of code.
</head>
4. Paste the following piece of code just above the line mentioned in step 3. Save your template.
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js' type='text/javascript'/>
<script type='text/javascript'>
$(document).ready(function(){
$("li.one").click( function(){ $
("body").removeClass('bg2 , bg3').addClass("bg1");
});
$("li.two").click( function(){ $
("body").removeClass("bg1 , bg3").addClass("bg2");
});
$("li.three").click( function(){ $
("body").removeClass("bg1 , bg2").addClass("bg3");
});
});
</script>
<script type='text/javascript'>
$(document).ready(function(){
$("li.one").click( function(){ $
("body").removeClass('bg2 , bg3').addClass("bg1");
});
$("li.two").click( function(){ $
("body").removeClass("bg1 , bg3").addClass("bg2");
});
$("li.three").click( function(){ $
("body").removeClass("bg1 , bg2").addClass("bg3");
});
});
</script>
Note: You need not paste the first line (highlighted in orange) in the above coding if you already have the jquery file running on your blog.
5. Now, insert the following tags anywhere in your template - may be within the post editor or on the sidebar or below the content wrapper.
<ul>
<li class="one"><a href="#">Red</a></li>
<li class="two"><a href="#">Brown</a></li>
<li class="three"><a href="#">Blue</a></li>
</ul>
6. That's it! Save your template or publish the post. <li class="one"><a href="#">Red</a></li>
<li class="two"><a href="#">Brown</a></li>
<li class="three"><a href="#">Blue</a></li>
</ul>
Just hit the text of your choice - Red or Brown or Blue and enjoy the changing environment on your blog. You may change from the default colors (Red, Brown, Blue) to the ones you like, by just altering the color codes (highlighted in blue) mentioned in step 2. In fact, you can alter between your background images. To do so, just replace "BG IMAGE URL" with the URL of your background image.
I hope, I kept the logic as well as the post simple. The above code was tested to work fine on all major browsers including, but not limited to, IE, FF, Opera, Chrome and Safari. If you find any difficulties in installing the above effect, then leave a comment or ping me on gtalk. I'll help you out! Do share your opinions about the article via comments:)
Click here to subscribe to FREE email updates from "Everything About Blogging", so that you do not miss out anything that can be valuable to you and your blog!
thanks for sharing :D was searching for this since long
REPLY@sumera: I'm glad that your long term search has come to an end today! Lol:) Thanks for the comment buddy!
REPLYi will try this.. its is have a text resizer hacks for blogspot? to add every blog post at a tittle post..?
REPLY@Haz Issac: I'm not able to understand your English! Just tell me clearly what you want and I'll let you know if it is possible on blogger:)
REPLYI must say that it is a great tweak which has been explained in an easy to understand language. You are doing great job.
REPLYYou got good points there, so I always love your site, it looks like you are an expert in this field. keep up the fantastic work, My friend recommends your blog.
REPLYMy blog:
rachat credit pret www.rachatdecredit.net
@Analyst: Ah! Yes! Thanks for the comment buddy! Hope you enjoy the upcoming articles as well!
REPLY@Anonymous: Yup! Thanks for the comment and your friend as well who recommends my blog:)
REPLYi really appriciate.god bless
REPLY@chris: Well, thanks for the comment buddy! I hope you enjoy the other articles on my blog as well!
REPLYNICE! Thanks alot
REPLY@benooi: Yup! Thanks for the comment buddy!
REPLYchanging backgrounds is exactly what i needed, but what is the code for changing 4 to 5 backgrounds
REPLY@kk: You just have to extend the codes! If you don't know, how to do that, just hit me via gmail and I'll help you out!
REPLYSeriously, I'm addicted in your tutorials xD.
REPLYA while ago I asked you how to add a button to my blog, but I managed to install it after some failed atempts, so I never reached you via e-mail about that. thanks anyway.
I've got a doubt... in fact two about this effect. First off all, Does it only accept 3 choices of Bg change, or can I manually add more and name them "4", "5"... etc.
Second one, let's imagine I want to expand the possibility of changing BG to the possibility of changing between 2/3 or more color schemes, Is it possible?
I will either way use it on my blog, but this questions popped my mind. thanks, and congrats for you amazing tutorials ;)
@Ricardo: Well, sorry for the delayed response! Anyway, it is possible to add more than 3 colors! I'm not clear with the second question! Nevertheless, install the above effect and let me know if you like it!
REPLYIs it possible to use several photos, instead of colors/
REPLYUse the tab below to comment. No spam please!!!
POST YOUR COMMENT