I am rather new to all things JavaScript so please bear with me 🙂 I am trying to add ShareThis plugin to the title of FancyBox so that users can share a certain picture. It shows up nicely but nothing happens when I click on “share” buttons. Same plugin works on a regular page just fine, so I am guessing it’s conflicting scripts (plugin script inside the FancyBox script). Unfortunately, I don’t know JavaScript well enough to solve it on my own, but I really need this to work…
If anybody knows how to solve it, I will really appreciate it! Here is what I have so far code-wise:
Script for FancyBox (including showing elements in Fancybox title):
$(document).ready(function() {
$(".wrap").fancybox({
closeClick : false,
nextEffect: 'fade',
prevEffect: 'fade',
beforeLoad: function() {
var el, id = $(this.element).data('title-id');
if (id) {
el = $('#' + id);
if (el.length) {
this.title = el.html();
}
}
},
helpers : {
title: {
type: 'inside'
}}
});
});
Here is what I need to show in the title:
<div id="title1" class="hidden">
<div class='share'>
<span class='st_facebook_hcount' displayText='Facebook'></span>
<span class='st_twitter_hcount' displayText='Tweet'></span>
<span class='st_pinterest_hcount' displayText='Pinterest'></span>
<span class='st_email_hcount' displayText='Email'></span>
</div>
<p>Some description</p>
</div>
I included the ShareThis script from their website as well.
Any suggestions?
Digging into the issue, it seems that the ShareThis buttons don’t really work on Ajax calls that return HTML, meaning the ShareThis button is defined in the synchronous data. Because that moving/copying the content from
<div id="title1">into fancybox’stitlewon’t work regardless that the ShareThis buttons’ html is perfectly rendered.The workaround is to build the buttons up dynamically while opening fancybox and prompt the ShareThis script to place those buttons again (inside the
title) using their functionstButtons.locateElements();read mroe HERE. Of course, we have to use fancybox’s callbacks to sync the task.First, since what we want to share is specifically the content inside fancybox (I assume) and not the whole page, we need to create the function that builds the ShareThis buttons and pass the URL to share so
… the function above basically builds the same html structure as in the code you wanted to show in the
title. Notice that I have added some ShareThis attributes (st_url and st_img in the case of pinterest … check ShareThis documentation about share properties and information)Then the html can be something like this
Notice the
data-*attributes set for each anchor so we can pass additional information to fancybox.Then, the fancybox callbacks :
1) Use
beforeShowto build thetitlecallingbuildShareThis(url)and add the caption from thedata-captionattribute (if any) :2) call ShareThis’
stButtons.locateElements()usingafterShow:…. that should do the trick.
NOTE: You still need to bind the ShareThis scripts in your document like
(use your own
publisherID)This was tested using the latest fancybox version to date (v2.1.2), see WORKING DEMO HERE. Feel free to check the source code and modify it to fit your needs.