简体   繁体   English

ShareThis插件在FancyBox标题中不起作用

[英]ShareThis plugin not working in FancyBox title

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. 我对JavaScript的所有事物都比较陌生,所以请忍受:)我正在尝试将ShareThis插件添加到FancyBox的标题中,以便用户可以共享特定图片。 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). 相同的插件可以在常规页面上正常工作,所以我猜它是有冲突的脚本(FancyBox脚本中的插件脚本)。 Unfortunately, I don't know JavaScript well enough to solve it on my own, but I really need this to work... 不幸的是,我对JavaScript的了解不足以独自解决它,但是我确实需要它来工作...

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 ): FancyBox的脚本(包括在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. 我也从他们的网站中添加了ShareThis脚本

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. 深入探讨该问题,似乎ShareThis按钮在返回HTML的Ajax调用上实际上并不起作用,这意味着ShareThis按钮在同步数据中定义。 Because that moving/copying the content from <div id="title1"> into fancybox's title won't work regardless that the ShareThis buttons' html is perfectly rendered. 因为无论ShareThis按钮的html是否完美呈现,将内容从<div id="title1">移动/复制到fancybox的title都无法正常工作。

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 function stButtons.locateElements(); 解决方法是在打开fancybox时动态地构建按钮,并使用它们的函数stButtons.locateElements();提示ShareThis脚本再次将这些按钮(在titlestButtons.locateElements(); read mroe HERE . 这里阅读mroe。 Of course, we have to use fancybox's callbacks to sync the task. 当然,我们必须使用fancybox的回调来同步任务。

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 首先,由于我们要共享的内容专门是fancybox(我认为)中的内容,而不是整个页面,因此我们需要创建构建ShareThis按钮并传递URL以便共享的功能。

function buildShareThis(url){
 var customShareThis  = "<div class='share'>"; // class for styling maybe
     customShareThis += "<span class='st_facebook_hcount' displayText='Facebook' st_url='"+url+"'></span> ";
     customShareThis += "<span class='st_twitter_hcount' displayText='Tweet' st_url='"+url+"'></span>";
     customShareThis += "<span class='st_pinterest_hcount' displayText='Pinterest' st_url='"+url+"' st_img='"+url+"' ></span>";
     customShareThis += "<span class='st_email_hcount' displayText='Email' st_url='"+url+"'></span>";
     customShareThis += "</div>";
     return customShareThis;
}

... the function above basically builds the same html structure as in the code you wanted to show in the title . ...上面的函数基本上与您想要在title显示的代码建立相同的html结构。 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) 请注意 ,我已经添加了一些ShareThis属性( 在pinterest的情况下为 st_url和st_img ...有关共享属性和信息,请参阅ShareThis文档

Then the html can be something like this 那么html可以是这样的

<a href="images/01.jpg" class="fancybox" data-fancybox-group="gallery" data-caption="some description 01"><img src="images/01t.jpg" alt="thumb 01" /></a>
<a href="images/07.jpg" class="fancybox" data-fancybox-group="gallery" data-caption="description two" ><img src="images/07t.jpg" alt="thumb 02" /></a>
<a href="images/08.jpg" class="fancybox" data-fancybox-group="gallery" data-caption="third description 03" ><img src="images/08t.jpg" alt="thumb 03" /></a>

Notice the data-* attributes set for each anchor so we can pass additional information to fancybox. 注意为每个锚设置的data-*属性,以便我们可以将其他信息传递给fancybox。

Then, the fancybox callbacks : 然后,fancybox回调:

1) Use beforeShow to build the title calling buildShareThis(url) and add the caption from the data-caption attribute (if any) : 1)使用beforeShow构建title调用buildShareThis(url)并从data-caption属性(如果有)中添加标题:

  beforeShow: function() {
     var caption =  $(this.element).data("caption") ? $(this.element).data("caption") : "";
     this.title = this.title ? this.title + buildShareThis(this.href) + caption : buildShareThis(this.href) + caption;
  }

2) call ShareThis' stButtons.locateElements() using afterShow : 2)使用afterShow调用ShareThis'stButtons.locateElements stButtons.locateElements()

  afterShow: function(){
     stButtons.locateElements();
  }

.... that should do the trick. .... 这应该够了吧。

NOTE : You still need to bind the ShareThis scripts in your document like 注意 :您仍然需要像文档一样绑定ShareThis脚本

<script type="text/javascript">var switchTo5x=true;</script>
<script type="text/javascript" src="http://w.sharethis.com/button/buttons.js"></script>
<script type="text/javascript">stLight.options({publisher: "ur-7957af2-87a7-2408-5269-db75628d3a14"});</script>

(use your own publisher ID) (使用您自己的publisher ID)

This was tested using the latest fancybox version to date (v2.1.2), see WORKING DEMO HERE . 已使用最新的fancybox版本(v2.1.2)对它进行了测试,请参阅此处的工作演示 Feel free to check the source code and modify it to fit your needs. 随时检查源代码并进行修改以满足您的需求。

I'm guessing that it's not working for this reason. 我猜因为这个原因它不起作用。

You are loading the fancy box code when the document is ready using $(document).ready (which is fine). 当文档准备就绪时,您可以使用$(document).ready加载花哨的框代码(可以)。 The code that initializes the sharethis elements has probably already run (it's unclear from your code example when you're loading it). 初始化sharethis元素的代码可能已经运行(加载时从代码示例中尚不清楚)。 So when you create a new set of sharethis elements using $(document).ready, they are not getting processed by the sharethis code. 因此,当您使用$(document).ready创建一组新的sharethis元素时,sharethis代码不会对其进行处理。

Try this: 尝试这个:

$(document).ready(function() {
    $(".wrap").fancybox({
        ## no change to your code above ##
    });
    // put your publisher code in below
    stLight.options({publisher:'12345'});
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM