The Kahimyang Project Logo
Primo's Code Blog
Howtos tips and tricks
The Kahimyang Project

Adding Pinterest button to a lightbox

In this demonstration we will use slimbox 2, a lightbox 2 clone. Slimbox, like lightbox, and most its clones, uses the title attribute of a link to render a caption near the bottom of the frame, below the image. It is this title that we also use to add our Pinterest button. Using a link as title directly will not work, aside from the resulting title, which become the tool tip of the link, is ugly.

Slimbox is an image only lightbox.

Standard lightbox

This is how Slimbox is used.

<a href="http://kahimyang.info/resources/Barilla.jpg" rel="lightbox" 
	title="Barilla was the first coin struck in the country.">
	<img src="http://kahimyang.info/resources/s-Barilla.jpg"
		width="180" border="0" alt="How to add pinterest to a lightbox" />
</a>

This an image link that will display our lightbox. For this code to work, jQuery and the Slimbox plugin must be in the page. You add them to the head section of the page similar to the following:

<!-- jQuery -->
<script type="text/javascript"
scr="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js">
</script>

<!-- Slimbox -->
<script type="text/javascript" src="/kauswagan/slimbox-2.04/js/slimbox2.js">
	</script>
<link rel="stylesheet" 
	href="/kauswagan/slimbox-2.04/css/slimbox2.css" 
	type="text/css" media="screen" />

The slimbox lines above may be slightly different depending on the location of your slimbox plugin. Download your plugin here http://code.google.com/p/slimbox/downloads/detail?name=slimbox-2.04.zip if you don't have them already.

Pinterest

Adding Pinterest to the above lightbox requires the synchronous version of Pinterest script. Below is a snippet that employs the synchronous Pinterest script to draw the button.

<div>
	<a href="http://pinterest.com/pin/create/button/?
		url=http://kahimyang.info&
		media=http://kahimyang.info/resources/Barilla.jpg&  
		description=Barilla was the first coin struck in the country."
		class="pin-it-button"
		count-layout="horizontal"></a>
	<script type="text/javascript" src="http://assets.pinterest.com/js/pinit.js" />
</div>

Please visit the Pinterest website if you need more explanation of the fields above.

Adding the Pinterest button to a lightbox

To add a Pinterest button, do not code directly the button into the title attribute. As already mentioned, it will not work. Add the title, which is our Pinterest button (or any any other link/anchor), by handling the onmousedown of the link. In our example below, we also use onmouseover to prevent the link (the title) from displaying as tooltip while you hover over the link and while the lightbox is not yet displayed.

<a href="http://kahimyang.info/resources/Barilla.jpg"
	onmousedown="addPinterest(this);"
	onmouseover="this.title='';"
	rel="lightbox">
		<img src="http://kahimyang.info/resources/s-Barilla.jpg"
		width="180" border="0" alt="How to add pinterest to a lightbox" />

And below is the javascript onmousedown handler.

<script type="text/javascript">
	function addPinterest(lightbox) {
		lightbox.title='<a href="http://pinterest.com/pin/create/button/?';
		lightbox.title +='url=http://kahimyang.info&';
		lightbox.title +='media=http://kahimyang.info/resources/Barilla.jpg&';
		lightbox.title +='description=Barilla, first coin struck in the country." ';
		lightbox.title +='class="pin-it-button" ';
		lightbox.title +='count-layout="horizontal"></a> '
		lightbox.title += '<script type="text/javascript" '
		lightbox.title +='src=http://assets.pinterest.com/js/pinit.js">';
	}
</script>

Add the Javascript code above in the head section of your page and it is all set. Here is a demo. You may also want to view the source of this demo page.

http://demo.kahimyang.info/cgi-cpp/pinterest-1.html

Note that depending on the speed of your connection, there may be a very slight delay before Pinterest button renders itself (demo).

That's it Good Luck.

RSS Logo



Comment icon   Comments (Newest first)

Recent posts in HTML/JS/CSS category
Blue dot icon Using Rhinoslider with image and youtube content in JSF pages
Blue dot icon Using Google+ interactive post for web pages
Blue dot icon Tracking user interactions with Facebook and Twitter buttons with Google Analytics
Blue dot icon Using Google Map directions in HTML pages
Blue dot icon Building a page with infinite scroll in PrimeFaces using Waypoints jQuery plugin
Blue dot icon Implementing a collapsible ui:repeat rows in JSF
Blue dot icon Using jQuery UI in CGI C++ Web application





Most popular articles
Blue dot icon Using Expect script to automate SSH logins and do routine tasks accross multiple hosts   (15435)
Blue dot icon How to setup Tomcat 7 as your primary webserver on Debian Squeeze    (14090)
Blue dot icon How to setup FLV streaming with crtmpserver C++ RTMP server   (9891)
Blue dot icon A Java class for sending multipart Email messages through your Gmail account    (6991)
Blue dot icon How to use Google Translate's Text to Speech (TTS) services in your web page using Servlet   (5699)
Blue dot icon Speed up Primefaces page load with p:remoteCommand partial update   (5578)
Blue dot icon Building a mobile website with JSF 2 core and JQuery Mobile 1.0   (5345)
Blue dot icon Google Map-Adding markers, info window, circle, small control, and events in Javascript    (5253)