在模态弹出窗口或标准弹出窗口中打开Facebook共享

时间:2014-04-26 10:41:39

标签: javascript jquery facebook

我正试图让我的Facebook分享按钮在模态窗口中打开(有点像灯箱打开图像),但到目前为止我所能做到的只是在一个全新的窗口打开,我不知道不想要。

我注意到Twitter会在一个小弹出窗口中打开,如果我无法将Facebook Share窗口作为模态窗口打开,那将是理想的选择。但我再也无法通过Facebook窗口来做到这一点。

关于我如何做到这一点的任何想法?

3 个答案:

答案 0 :(得分:46)

试试这个:

HTML

<a class="fb-share" href="https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fwww.manojyadav.co.in" target="_blank">
    Share on Facebook
</a>

的jQuery

$(document).ready(function() {
    $('.fb-share').click(function(e) {
        e.preventDefault();
        window.open($(this).attr('href'), 'fbShareWindow', 'height=450, width=550, top=' + ($(window).height() / 2 - 275) + ', left=' + ($(window).width() / 2 - 225) + ', toolbar=0, location=0, menubar=0, directories=0, scrollbars=0');
        return false;
    });
});

<强> Demo Link

使用Facebook JavaScript SDK的示例

HTML

<a href="#" class="fb-share">Share on Facebook</a>
<div id="fb-root"></div>

JavaScript&amp; jQuery的

(function(d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id))
        return;
    js = d.createElement(s);
    js.id = id;
    js.src = "//connect.facebook.net/en_US/all.js";
    fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

window.fbAsyncInit = function() {
    FB.init({
        appId: '409742669131720',  // Change appId 409742669131720 with your Facebook Application ID
        status: true,
        xfbml: true,
        cookie: true
    });
};

$(document).ready(function() {
    $('.fb-share').click(function() {
        FB.ui({
            method: 'feed',
            name: 'Manoj Yadav',
            link: 'http://stackoverflow.com/users/2502457/manoj-yadav',
            picture: 'https://www.gravatar.com/avatar/119a8e2d668922c32083445b01189d67',
            description: 'Manoj Yadav from Mumbai, India'
        });
    });
});

要使用Facebook JavaScript SDK,您需要创建Facebook App并将appId 409742669131720替换为appId

<强> Demo Link

答案 1 :(得分:14)

Facebook,Twitter,Google plus,LinkedIn的一个不错的工作解决方案...... 找到HERE(法文) 在Wordpress中也很棒(Merci Pierro(si tu通过par ici;))

HTML

<a href="https://www.facebook.com/sharer/sharer.php?u=URLENCODED&t=TITLE" class="share facebook" data-network="facebook">Share on Facebook</a>
<a href="https://twitter.com/share?url=URLENCODED&text=TITLE" class="share twitter" data-network="twitter">Share on Twitter</a>
<a href="https://plus.google.com/share?url=URLENCODED" class="share google" data-network="google">Share on Google+</a>
<a href="https://www.linkedin.com/shareArticle?mini=true&url=URLENCODED&title=TITLE&source=COMPANY" class="share linkedin" data-network="linkedin">Share on LinkedIn</a>

JS

$('a.share').click(function(e){
e.preventDefault();
var $link   = $(this);
var href    = $link.attr('href');
var network = $link.attr('data-network');

var networks = {
    facebook : { width : 600, height : 300 },
    twitter  : { width : 600, height : 254 },
    google   : { width : 515, height : 490 },
    linkedin : { width : 600, height : 473 }
};

var popup = function(network){
    var options = 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,';
    window.open(href, '', options+'height='+networks[network].height+',width='+networks[network].width);
}

popup(network);
});

答案 2 :(得分:11)

我想我找到了一个更好的解决方案,你可以将弹出式样式注入onclick =&#34;&#34;。

以下是codepen:http://codepen.io/artchibald/pen/OXGOVm

这个解决方案的工作要少得多。

<a target="_blank" OnClick="window.open(this.href,'targetWindow','toolbar=no,location=0,status=no,menubar=no,scrollbars=yes,resizable=yes,width=600,height=250'); return false;" href="https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fyoursite.com%2Fem%2F4881-01%2F&picture=http%3A%2F%2Fyoursite.com%2Fem%2F4881-01%2Fimg%2Fmercedes-maybach.png&title=title+here&description=Your+description">
  <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADkAAAAUCAMAAAAA9GVfAAACnVBMVEU0TI01TIw1TY01TY41TY81To82TY42TY85UZU6U5k7VJs7VZ07VaE8VZs8Vp08VqE8VqI8V6I9Vp49V6A9V6M+WJ8+WKA+WKQ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