Showing posts with label JQuery. Show all posts

Responsive Videos In Posts on Blogger

YouTube. Dailymotion, Vimeo.. who doesn't like to spend time on these sites. Chances are you embed videos from these platforms on your blog, but the worst thing? It doesn't fit on mobile devices, that too even if you are using a responsive template.



Why is it important to make sure videos fits properly on mobile devices? If you would have asked this question about even just half a decade back we would said that it is not important, though times are changing a lot faster, majority of your readers maybe on some kind of mobile devices and that is why responsive web is such a big break-through in the internet World. 

Question arises: How do I make sure videos on my blog, from either YouTube, Vimeo, Dailymotion (.. or other sources) properly fit on my blog page when viewed from a mobile device.

Before we begin the tutorial, we would like you to understand that this tutorial is meant for those blogs which uses a responsive web design approach, mostly the default blogger templates aren't those, and following this tutorial would bring nothing good, it would not bite, but wouldn't bring you any good either.
Tutorial Begins
FitVidsJS is a lightweight jQuery plugin that works very well in making videos fluid i.e fit with the width of the container, from many popular platforms (..and also other platforms that you can specify).
Demo - Fluid Videos
Demo it didn't happen.
While you are on the demo page, try reducing the size of your browser window and see how the video responds to the re-size event and adapts itself accordingly.

Back it up: Before you begin with the steps, I recommend you to backup your blogger template as we will make changes to the template, and if anything goes wrong you can always revert back using the backed-up template file.

Adding jQuery

It is a JavaScript framework that is used for various tasks like DOM manipulation, AJAX and other technical stuffs that helps make it easier to make a website interactive with JavaScript. The FitVids plugin is a jQuery plugin which means it would not work if you do not load jQuery.

Check if you already have jQuery added Chances are that your template may already contain a version of jQuery (many do), it is not recommended to add jQuery twice, so check if you have jQuery already installed. How to do that :-

Go to the Blogger Dashboard -> Template -> Edit HTML -> Use CTRL+F or +F and search for the keyword 'jquery', go through the found instances and see if you have something similar to this:
<script type="text/javascript" src="https://....../jquery.min.js"></script>

If you do not have jQuery, add it - To add jQuery from Google's CDN, follow these steps:

  1. Go to your Blogger Dashboard -> Template -> Edit HTML
  2. Use CTRL+For+F search for </head>
  3. Copy the following line of code, which is the script tag to include jQuery library in your blog -
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>

    That's it. Now it must look something like this in the template code editor
    Adding jQuery's code snippet in Blogger template.
  4. Press the orange "Save Template" button to save it. 
Once it is done you may proceed to the next section to add the FitVids plgun. Adding it is almost similar to adding jQuery. Keep the template editor open, we need it for the next two steps too!

The main FitVidsJS plugin

It's time to add the JavaScript/jQuery plugin. We will add it from a CDN, that makes sure that the files load fast and doesn't impact on the performance of your blog.

Before we go on further, copy this script tag which we need in the steps mentioned next:
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/fitvids/1.1.0/jquery.fitvids.min.js"></script>
  1. Go to your Blogger Dashboard -> Template -> Edit HTML
  2. Use CTRL+For⌘+F search for </head>
  3. Paste the copied code immediately before line where </head> is.

    It is important that we put this script tag after the jQuery tag (wherever it is in the template), so if you already have jQuery somewhere else in the template consider putting this tag below it. These tags can be added anywhere, in the head, or even at the end of the body.
  4. Following is an example of how it must look like after you have added it to the template. Note that the plugin code i.e Fitvids script tag must always be placed after jQuery's for it to work.

Save the template. Yay! We now have jQuery & FitVids plugin added in our template. The final part is initializing the plugin so that it works upon the videos in your posts, and also other parts if you intend to use it for advance purpose.

Initialize the plugin - Fitvids

Once we have the script tags in place we have to tell it to initialize the function on the videos, the plugin can't guess where do you have your videos, or which videos do you intend to make responsive so we have to specify it.

We will provide different initialization code for different applications i.e if you want the plugin to work on all videos on your posts, or if you want it to work on some some that you can specify. It gives you more control over what you want to be handled by this plugin.

Choose the right initialize code and continue with the step to add it to your template (which is same as adding the script tags in the above steps, you just have to put the following codes below any script tags we added)

1. All videos in posts, or (recommended)

The code uses the class of the body of the post to identify which part of the page it must handle, mostly blogger templates include the class name "post-body" in the container of the post and assuming that your template does the same you can use this to initialize the plugin on all the videos in every posts.

<script type="text/javascript">
$(document).ready(function(){
$(".post-body").fitVids();
});
</script>

On the line 3 you can change .post-body to the class name you want to target. Most blogger templates this class name for posts, so this code may work right out of the box without any changes.

2. Only some videos, or

If you do not want every video in your post be handled, then you can specify the ones you want to. 

<script type="text/javascript">
$(document).ready(function(){
$(".fit").fitVids();
});
</script>

This version of the code works only on the videos wrapped inside a container with the class fit. It gives you a control over the videos you want to make responsive. As it has been said it needs to be wrapped inside a container, it means that your embed code must be put inside an element like this:

<div class="fit">
<iframe src="...[embed code]" width="xxx" height="xxx">
</div>

So while you are including a video somewhere in your post you always have to make sure you wrap it inside this code.

3. All, but not some

The third option is to have selective videos not being handled by the plugin, all videos will become responsive i the posts except for the ones you specify.

<script type="text/javascript">
$(document).ready(function(){
$(".post-body").fitVids({ ignore: '.not-fit'});
});
</script>

When you do not want a video to be handled by the plugin, you can just wrap its code inside a div element with the class not-fit (notice the .not-fit in the code above). This is how you can wrap the embed code for the video inside the element, and then paste it in your post:

<div class="not-fit">
<iframe src="...[embed code]" width="xxx" height="xxx">
</div>

And then put and code in place,

Choose an initialization script from any of the above sections and then include it in your Blogger template. To do that:

  1. Go to Blogger Dashboard -> Template
  2. Edit HTML
  3. Now search for the line where we added the FitVidsJS <script...></script> tag. Paste the initialization code just after it. Here's a screenshot of how it must look once you have it in place.


Make sure the scripts are in this order: jQuery -> FitVids -> Initialization Code

If it looks fine save the template and try it on a post.

Questions or comments?

It is quite possible that this may not work properly for every blog due to different reasons, even if one part of the guide is not followed properly the chances are high that errors may come up during the set-up, so if you have any questions regarding this you can post a comment on this post (include your blog address) and we will try to help you set it up.

If you have suggestions and comments on this tutorial please post it in the comment form, we would love to hear from you! This post may not cover every aspect of the jQuery library, or the FitVids plugin, as it would have made the post enormously large, to help others any bit of information that you think is vital would be appreciated.


Auto Hide Notification Box in Blogger

Notification boxes in blogs can be useful for notifying readers about something hot on your blog. But this notification box is a bit different from others, it hides automatically after a specified time.
You may find lots of tutorials about these types of notification boxes but this one is different. It hides itself after a specified time which is the best thing about this. It won't remain there on the page forever (which can possibly disturb the reader not interested in notifications).

Before we proceed to the tutorial check what you are going to add to your blog. 
Demo

Proceed to the tutorial to know how to add it to your blog.

The HTML


The first thing we need is the HTML code as always. The code is simple and you can edit its content easily to make it ready for your blog. This is the HTML snippet

<div id='float-bar' style='display:none;'>
<b class='notifyTextbld'>Notification:</b>
Hello! Welcome to our blog and blah blah blah. See this new post <a rel='nofollow' href='http://stramaxon.com'>Cookies!</a>
</div>
The box contains bold text "Notification" and below it is some message you want your readers to see. You may also include an anchor link to a post or website you want the readers to visit.

Edit the HTML

You will need to edit the text (on line number 3) to display your own message. You know how to do it, just replace the sample text with your own meaningful message. It also contains an anchor link :

<a rel='nofollow' href='http://stramaxon.com'>Cookies!</a>
If you want your readers to visit a page then you can add your own HTML anchor links or any element, you can even add images inside it.

If you are not familiar with HTML but want to add different HTML elements use the Blogger post editor to set the elements in a graphical user interface, switch to the HTML tab, copy the HTML and paste it in the third line of the HTML.

Add the HTML

To add this HTML you have to edit your template HTML so it's good to backup your current template.

We can add this HTML anywhere in the template (inside body element) but to make sure it doesn't interfere with any other element in the template it is best to add it below everything. Follow this steps to do that :
  1. Go to your Blogger Dashboard
  2. Click on the Template tab
  3. Press the Edit HTML button, ignore the warning and proceed. 
  4. Now open the in-page search function in your browser (default CTRL+F)
  5. And search for </body> 
  6. When you find it, paste the HTML code just above it and save the template. 
Once done, check your blog for the HTML (it might be at the bottom), it may look dull, to make it beautiful we will need the CSS. So proceed to the next step.

The CSS

Yeah, this is what gives life to dead HTML skeleton.  The following CSS will be used to style the notification box.
#float-bar {
font-family:"Lucida Grande", "Lucida Sans Unicode", Verdana, Helvetica, Arial, sans-serif;
background-color:rgba(248, 248, 248, 0.85);
color:#000;
padding:10px 30px 30px 30px;
font-size:16px;
position:fixed;
right:10px;
top:0px;
width:500px;
border-radius:0px 0px 10px 10px;
z-index:99999;
}

#float-bar a {color:#000;}
#float-bar a:hover {color:#000;text-decoration:none;}

#float-bar .notifyTextbld {display:block;margin-bottom:20px;}

Editing the CSS

You might want to modify the codes. Edit the values in the highlighted lines.
  • Line 3: This the background color of the box. Replace the value with a rgba color value (with alpha transparency) or HEX color code. 
  • Line 4: This is the color of the text inside the notification box. Use HTML Color Picker to choose a HEX color code  
  • Line 6: This is the size of font inside the notification box. Change the 16px to a desired value.
  • Line 8: You can edit both the property name and its value. Change right to left if you want the notification bar to appear on the left side. And also you can change the 10px to a some other value, this decides how far the box should be from the position. 
  • Line 9: The number of pixels you can the box to be far from the top. 
  • Line 10: This is the width of the box. Change the pixel value if you want.
If you know about CSS (it's easy) you may try adding for removing properties to design your own different box.

Adding the CSS

Check this tutorial to learn how to add CSS in Blogger
http://stramaxon.blogspot.com/2012/03/how-to-add-css-to-your-blogger-blog.html

The JavaScript jQuery 

The final step is to add the JavaScript jQuery script. The script controls how the box should enter and exit. We can do a lot of things with it but for now we will just do the basic in and out stuff.

Including jQuery Library

If you already have jQuery library installed then skip this step. If not then copy the HTML line below :
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
To add this in your template : Go to your Blogger Dashboard -> Template -> Edit HTML -> Search for </head> and paste the code just above </head> and save the template.

Now you may proceed to the next step.

The jQuery Script

This is the script you need.
<script type='text/javascript'>
$(document).ready(function(){
$('#float-bar').slideToggle(445,function(){
$(this).show(function(){
setTimeout(function (){
$('#float-bar').slideUp(445);
},5000);
});
});
});
</script>
You don't need to change anything in this script expect the number of seconds you want the notification bar to stay on the page.

The line number 7 contains a number value 5000 which is the number of milisecond after which the box will disappear. 1000ms = 1sec, so just change the value to set a different time for it to hide. If you want the box to stay on the page for 20 seconds you may change the value to 20000

Adding the Script

The procedure to add the script is same as adding the HTML in the body. To add it go to your Blogger Dashboard -> Template -> Edit HTML -> Use CTRL+F and find </body> in the template and then paste the jQuery code just above </body>.

Showing it just on Homepage

You might not want this notification bar everywhere so the question how do you set it to just display on homepage, post pages, or an index page and archive pages.

The best way to do this is to use Blogger conditional tag (which works on the backend) which will make the box render only on the page type you specify.

The code we will need is this
<b:if cond='data:blog.url == data:blog.homepageUrl'>
//The HTML/JS/CSS or any code you want.
</b:if>
Replace the 2nd line in this code with the HTML code of the notification box and then add it to your template as instructed in in the tutorial. All we did is added the <b:if> conditional tag around the HTML code provided in the tutorial.

Now check your blog for the new notification bar. How is it ? I hope you liked it but if you found any error in the code then do post it in the comments. Thanks.

Put videos, photos or any HTML in Tabs in Blogger with Jquery

There have been many question about adding content into easily switchable tabs without any page load, blogger users normally don't find using JQuery and most of them never headed to the Jquery plugin site, the instruction are complicated there and it's not easily understandable. But i will give easy and clear instruction to install this Tabs JQuery plugin on Blogger.

Update 25th March 2017 - Due to some issues in hosting the script files the plugin might not be working properly for everyone who implemented this. I would request you to come back here in a day or two and re-add the script codes with update URLS (you do not have to re-do the tabs you created in your post.)

Before we start this tutorial, thanks to sunsean.com for this JQuery plugin called idTabs, you are going to install that JQuery plugin on your blog.

Every blogger wants to add dynamic function but they are unable to do it, because there are not much people who writes about installing JQuery plugins on Blogger Blogs. If you want to display your post contents in a new way then this plugin is what you need, this plugin is useful for video bloggers having lots of videos in one post , if they use this plugin they can reduce space used for the videos.

If you aren't sure this is what you are looking for then please see the demonstration of the code.

Demo

tab contents in post blogger tabify blogger videos
Believe me this tutorial is very easy but yet some people with less experience with codes will find this tutorial difficult and to make this easy for you, i have divided the steps in parts, read further for the instructions.

Follow these steps

Before you proceed keep a backup of your template so if anything goes wrong you can revert it back to normal.

Include JS files

The first step is to include the JQuery library and the JS file for this plugin into your template. JQuery lib. This is the script tags you need.

To add it go to Blogger Dashboard -> Template -> Edit HTML -> Proceed -> Use CTRL+F to find </head> in the template. Paste the following code just above </head>
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js' type='text/javascript'></script>
<script src='http://cdn.stramaxon.com/stramaxon/ext/idTabs-stramaxon.js' type='text/javascript'/></script>
  • Line 1 : This is the script for latest version of minified JQuery library hosted on Google, if you have already included it in your template then don't add it again.
  • Line 2 : The second line is for the main plugin JS file which makes the tabs work. The file is being hosted my DropBox, you can host it on your own server, download the JS file here.

The HTML

The HTML is only where you will can confused but if you try to understand the concept of this plugin and how it works then it becomes easy for you to work with this HTML. Anyway, i am going to tell briefly about each element.

Here's the HTML we need
<ul class="idTabs">
<li><a href="#tab1">First</a></li>
<li><a href="#tab2">Second</a></li>
<li><a href="#tab3">Third</a></li>
<li><a href="#tab4">Fourth</a></li>
<li><a href="#tab5">Fifth</a></li>
</ul>

<div id="tab1">
<!-- Content Below this -->
</div>

<div id="tab2">
<!-- Content Below this -->
</div>

<div id="tab3">
<!-- Content Below this -->
</div>

<div id="tab4">
<!-- Content Below this -->
</div>

<div id="tab5">
<!-- Content Below this -->
</div>
  • Line 1 : It is the starting tag of the UnorderedList (ul)
  • Line 2 : This is the link tab for the content in div element with id='tab1' which is on line 9
  • Line 3 : Similar to the description of line 2, this tab link is for the content in div element with id='tab2'. 
  • Line 4 : This also do the same thing as the line 2 and line 3 but goes for the div element with id='tab3' 
  • Line 9 : From line 9 to line 11 is the first div element which will appear for a tab, you can put any valid HTML into it, just like i put the Video embed code in the Demo
  • The other div elements are for their corresponding links, if the <a> element has #tab1 in it's href value then it's the tab for the div element with id='tab1' and that's how it works.
Note : When you are giving an anchor link a href value with a hash like this #target, then it thinks links that to the current page (post editor) with the this #target. For example if i give the href value #mytab then the whole URL becomes
http://www.blogger.com/blogger.g?blogID=6219123568585811984#mytab

And that sometimes stops the plugin from working. To prevent this put the current Post URL before the target tag. Now we have got Custom permalink, so you can see what's the URL even before publishing the post. For example see this
<ul class="idTabs">
<li><a href="#tab1">First</a></li>
</ul>

<div id="tab1">
<!-- Content Below this -->
</div>
Should be turned to
<ul class="idTabs">
<li><a href="http://stramaxon.blogspot.com/2012/07/jquery-tabs-plugin-blogger.html#tab1">First</a></li>
</ul>

<div id="tab1">
<!-- Content Below this -->
</div>
If you turn the URL correctly then your plugin will work well and there will be no bugs.

The Final CSS

If you take a look at your post after adding the CSS, it will look very dull and to give it a professional look we need to apply the CSS on the tabs, we will be applying styles just on tabs. Following is the CSS

.idTabs {padding-left:0px !important;}
.idTabs li {display:inline;padding-left:0px;}
.idTabs li a {
padding:5px 15px;
background-color:#2EA537;
color:white;
text-decoration:none;
-webkit-transition:all 0.3s;
-moz-transition:all 0.3s;
-ms-transition:all 0.3s;
-o-transition:all 0.3s;
font-weight:bold;
border:1px solid transparent;
border-radius:10px 10px 0px 0px;
}
.idTabs li a:hover {
padding:5px 15px;
background-color:#805D5A;
color:white;
text-decoration:none;
}
.idTabs li a.selected {
background-color:#42BEF8;
}
.idTabs li a.selected:hover {
background-color:#42BEF8;
}
[Click here] To see how to add CSS to your blog.
There's no special instruction for CSS, you apply your own CSS design on the tabs to make it look good with your blog or if you don't have any idea about the editing the CSS then contact me for help at depy45631@gmail.com

Great ! You Did it

Hows the tab switcher looking, i hope you integrated it without any difficulties. If you have question, suggestion or just want to say thanks then your comments are welcomed. We will be publishing more JQuery  plugins for blogger so subscribe to our Email Newsletter.

Show/Hide effect for Gadgets in Blogger with JQuery

Don't you sometime feel like adding more dynamics to your blog and make it more user friendly, like giving them their own control like toggling show/hide of a gadget. You will learn how to do that in Blogger.
A little instruction to JQuery: It's a powerful and small JavaScript library to add dynamic features and sometimes also used for transferring data.

For a demo of this JQuery effect, click on the Title of About me gadget on right sidebar and see it toggling between hide and show. Ain't it great, you let your users decide whether they want to choose to display the gadget or not.

Adding the JQuery lib.

In classic blogger template, JQuery library isn't added in the document already. So to make the JQuery codes work you first have to implement the JQuery library.
To do that in blogger, Go to Blogger Dashboard -> Template -> Edit HTML -> Use CTRL+F and find </head> and paste this HTML script tag just above </head>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
This is just the first step, now read further for the real trick.

The JQuery to make the effect 

The main think we have to focus on this is the correct selector so it should work for your blog too. Although there i know the basic selector for classic blogger templates and can be applied on any template unmodified.


This is the magical code doing it all, study it well and read what ever line is about
<script type="text/javascript"> 
$(document).ready(function(){
$('.sidebar .title').one("click",function(){
$(".sidebar .widget-content").slideToggle("slow");
});
});
</script>
Note : The first and last line is just to wrap it into script tag to be placed in the document.
  • Line 2 : It tells the browser to load the Jquery after the document is fully loaded and read for performing JQuery effects
  • Line 3 : The whole line describes that when .title in the .sidebar gets clicked then perform the function (which is in the next line)
  • Line 4 : Continuing from the above line, it plays the function that was declared in the 3rd line, it makes the .widget-content inside .sidebar to hide when it's visible on a click event and when it's already hidden it's comes back in view. 
  • Line 5 and 6 : Ending for the above lines
Where to add this Script ? Go to Blogger Dashboard -> Template -> Edit HTML -> Use CTRL+F and find </head> and paste this HTML script tag just above </head>

You can use this on other areas of your blog just by making some changes in selectors, selectors can be identified easily with chrome developer tools.

Result : So when you are done placing the codes at right place, Save the template and refresh your blog using CTRL+F5 to ensure that all files are loaded freshly. Now try clicking on title of any of your gadget and see the amazing toggle effect. That will just make you blog to a new level.

Your feedback ?

This tutorial is designed to make implementation of JQuery in blogger easy and make you understand jquery, so if you find anything difficult and don't understand it then drop your comments and we may just look to it.

This JQuery will not work on some blogs having different templates, so if you don't have any idea what to do then post your blog links in the comment and we will guide you through the process.