【问题标题】:Fancybox 2.0 Facebook Like Buttons to Anchor LinksFancybox 2.0 Facebook 点赞按钮来锚定链接
【发布时间】:2014-03-29 08:04:36
【问题描述】:

我觉得这是不可能的,但我想我还是会问。有几个类似的问题,但没有一个能解决我的特定问题。

我有一个包含几个fancybox 画廊的网站(链接here)。客户希望在每个图像上都有一个赞按钮,该按钮将链接回该特定图像。由于该网站已上线,因此我暂时取消了“赞”按钮。

我已经为每张图片添加了锚链接,这样我就可以从 URL 打开它们了——就像http://www.izzyhodge.com/portfolio/index.html#image01。我也可以得到实际的like按钮没问题,因为我已经使用了一些java来添加“诗歌”悬停在事物上(它只包括一堆关于每个图像的div的内容)。但是,当我使用 Facebook 代码添加一个赞按钮时,它不仅会在下一张图片的不同位置添加另一个赞按钮,而且该按钮喜欢主投资组合页面而不是锚链接。

我能找到的唯一解决方案是使用带有 Facebook 按钮的锚链接,在喜欢的页面的标题中使用元标记,我当然不能这样做。

非常感谢您对此提供的帮助!顺便说一句,我使用的是 Fancybox 2.0,并且我使用的是 HTML5 添加按钮的方法(不确定是否必须为此更改文档类型?)。

编辑:我现在在这里尝试使用社交按钮 JSfiddle 来执行此操作,但是我使用 alt 标记作为标题,我不确定如何组合这两段代码。我的 JS 现在看起来像:

$(document).ready(function() {
  $(".fancybox").fancybox({
   helpers : { 
    title : { type : 'inside' }
   }, // helpers
       beforeShow : function() {
            if (this.title) {         
                // New line
                this.title += '<br />';

                // Add FaceBook like button
                this.title += '<iframe src="//www.facebook.com/plugins/like.php?href=' + this.href + '&amp;layout=button_count&amp;show_faces=true&amp;width=500&amp;action=like&amp;font&amp;colorscheme=light&amp;height=23" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:110px; height:23px;" allowTransparency="true"></iframe>';
            }
        var alt = this.element.find('img').attr('alt');

        this.inner.find('img').attr('alt', alt);

        this.title = alt;

        var toolbar = "<div id='tools'>" + $("#appendContent div").eq(this.index).html() + "</div>";

   $(".fancybox-inner").append(toolbar);
}
  }); // fancybox
 }); // ready

如果我删除;

this.title = alt;

我得到了一个 div,它的大小与 like 按钮的大小差不多,但里面什么都没有!这可能是由于我在 CSS 中的自定义标题格式。

Fancybox 的 HTML:

                <div id="thumb-container">
                    <a id="image01" class="fancybox thumb" rel="group" href="/images/portfolio/large/big_1.jpg"><img src="/images/portfolio/thumbs/th_1.jpg" alt="<h2>LOLA LURKING IN THE LILYPADS <br/> Book Cover (Book in progress) <br/> Pencil and Digital</h2>"/></a>
                    <a id="image02" class="fancybox thumb" rel="group" href="/images/portfolio/large/big_2.jpg"><img src="/images/portfolio/thumbs/th_2.jpg" alt="<h2>THE TREMENDOUS TAILS OF TRAVELLERS TRAILS - THE OTTER<br/>Collection published by Zeitgeist Fine Art<br/>Ink and Digital</h2>" /></a>
                    <a id="image03" class="fancybox thumb" rel="group" href="/images/portfolio/large/big_3.jpg"><img src="/images/portfolio/thumbs/th_3.jpg" alt="<h2>THE TREMENDOUS TAILS OF TRAVELLERS TRAILS - THE POLAR BEAR<br/>Collection published by Zeitgeist Fine Art<br/>Ink and Digital</h2>" /></a>

ETC
                </div>
                <div id="appendContent" style="display: none;">
                    <div><div class="fb-like" data-href="http://www.izzyhodge.com/portfolio/index.html#image01" data-layout="button_count" data-action="like" data-show-faces="true" data-share="false"></div></div>
                    <div><h2><a>READ POEM<img class="hoverpoem" src="/images/poems/otterpoem.png" alt=""/></a></h2></div>
                    <div><h2><a>READ POEM<img class="hoverpoem" src="/images/poems/polarbearpoem.png" alt=""/></a></h2></div>

ETC

干杯!

【问题讨论】:

  • 这fancyapps.com/fancybox/#usefulNo.6 不适合你吗?否则我不明白这个问题。
  • 太棒了,看起来它可以解决问题。我什至没想到那里看!感谢您的提醒:)
  • @JFK 我已经更新了这个问题,感谢您对此的见解! :)
  • this.element.find('img')... 应该是$(this.element).find('img')...
  • 如果你做this.title = alt;,你将覆盖你之前在你的条件下设置的title

标签: fancybox facebook-like anchor fancybox-2


【解决方案1】:

问题在于这一行

this.title = alt;

将覆盖您之前的 title,其中包括 facebook 按钮。你应该这样做:

this.title += alt;

完整代码:

$(".fancybox").fancybox({
    beforeShow: function () {
        if (this.title) {
            // New line
            this.title += '<br />';
            // Add FaceBook like button
            this.title += '<iframe src="//www.facebook.com/plugins/like.php?href=' + this.href + '&amp;layout=button_count&amp;show_faces=true&amp;width=500&amp;action=like&amp;font&amp;colorscheme=light&amp;height=23" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:110px; height:23px;" allowTransparency="true"></iframe>';
        }
        var alt = this.element.find('img').attr('alt');
        this.inner.find('img').attr('alt', alt);
        this.title += alt;
        var toolbar = "<div id='tools'>" + $("#appendContent div").eq(this.index).html() + "</div>";
        $(".fancybox-inner").append(toolbar);
    },
    helpers: {
        title: {
            type: 'inside'
        }
    }
});

JSFIDDLE

另一方面,请记住这一行

if (this.title)

验证title 属性是否存在于&lt;a&gt; 元素上......在你的情况下它确实存在,它简单地说image01,image02。等等,所以它也显示在fancybox'a title。

如果您只想使用title 属性在fancybox 的title 中显示鼠标悬停而不是,则删除条件并在其中从头开始构建title像这样的回调:

$(".fancybox").fancybox({
    beforeShow: function () {
        // New line
        this.title = '<br />'; // here we start the title
        // Add FaceBook like button
        this.title += '<iframe src="//www.facebook.com/plugins/like.php?href=' + this.href + '&amp;layout=button_count&amp;show_faces=true&amp;width=500&amp;action=like&amp;font&amp;colorscheme=light&amp;height=23" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:110px; height:23px;" allowTransparency="true"></iframe>';
        var alt = this.element.find('img').attr('alt');
        this.inner.find('img').attr('alt', alt);
        this.title += alt;
        var toolbar = "<div id='tools'>" + $("#appendContent div").eq(this.index).html() + "</div>";
        $(".fancybox-inner").append(toolbar);
    },
    helpers: {
        title: {
            type: 'inside'
        }
    }
});

JSFIDDLE(更新)

【讨论】:

  • 太棒了,这似乎解决了 alt 标题问题。 image01 标题只是让类似按钮起作用的占位符。我最初使用 alt 是为了不显示工具提示,但如果我能让这些按钮工作,我不介意让它们(我会将 image01 更改为相关的东西)。但是,点赞按钮仍然是不可见的!我已经尝试在“检查元素”中编辑我能想到的所有内容,但我无法弄清楚。我还注意到,如果我直接打开 iframe 链接,也会发生同样的事情,但是您的示例有效!我确定我在做一些愚蠢的事情:)
  • 虽然我刚刚意识到如果它使用 JS 调用 url 可能不会显示。我的 JS 知识真的很缺乏,你可能知道!
猜你喜欢
  • 1970-01-01
  • 2011-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多