Instantly share code, notes, and snippets.
Created
November 25, 2016 09:10
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save IvanofSA/e37d07af1981c26f366a178ba459fee8 to your computer and use it in GitHub Desktop.
Share social
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.step__shared.share | |
.share__title Поделиться | |
ul.share__container | |
li.share__wrap.share__wrap_ok(data-social-type = "ok" data-purl="" data-pimg="http://site.ru/img3.png", data-ptitle="Заголовок", data-ptext="техт") | |
li.share__wrap.share__wrap_fb(data-social-type = "facebookApp" data-purl="" data-pimg="http://site.ru/img3.png", data-ptitle="Заголовок", data-ptext="техт") | |
li.share__wrap.share__wrap_vk(data-social-type = "vk" data-purl="" data-pimg="http://site.ru/img3.png", data-ptitle="Заголовок", data-ptext="техт") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//fb sdk для активирования приложения | |
$(function () { | |
window.fbAsyncInit = function() { | |
FB.init({ | |
appId : '601733036681205', --взять с https://developers.facebook.com/ | |
xfbml : true, | |
version : 'v2.8' --взять с https://developers.facebook.com/ | |
}); | |
}; | |
(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/sdk.js"; | |
fjs.parentNode.insertBefore(js, fjs); | |
}(document, 'script', 'facebook-jssdk')); | |
}); | |
var Share = { | |
go: function(_element, _options) { | |
console.log(_options); | |
var | |
self = Share, | |
options = $.extend( | |
{ | |
type: _options.type, // тип соцсети | |
url: location.href, // какую ссылку шарим | |
count_url: location.href, // для какой ссылки крутим счётчик | |
title: document.title, // заголовок шаринга | |
image: _options.pimg, // картинка шаринга | |
text: _options.ptext, // текст шаринга | |
}, | |
$(_element).data(), // Если параметры заданы в data, то читаем их | |
_options // Параметры из вызова метода имеют наивысший приоритет | |
); | |
if (self.popup(link = self[options.type](options)) === null) { | |
// Если не удалось открыть попап | |
if ( $(_element).is('a') ) { | |
// Если это <a>, то подставляем адрес и просим браузер продолжить переход по ссылке | |
$(_element).prop('href', link); | |
return true; | |
} | |
else { | |
// Если это не <a>, то пытаемся перейти по адресу | |
location.href = link; | |
return false; | |
} | |
} | |
else { | |
// Попап успешно открыт, просим браузер не продолжать обработку | |
return false; | |
} | |
}, | |
// ВКонтакте | |
vk: function(_options) { | |
return 'http://vkontakte.ru/share.php?' | |
+ 'url=' + encodeURIComponent(_options.purl) | |
+ '&title=' + encodeURIComponent(_options.ptitle) | |
+ '&description=' + encodeURIComponent(_options.ptext) | |
+ '&image=' + encodeURIComponent(_options.pimg) | |
+ '&noparse=true'; | |
}, | |
// Одноклассники | |
ok: function(_options) { | |
return 'https://connect.ok.ru/dk?cmd=WidgetSharePreview&st.cmd=WidgetSharePreview&st._aid=ExternalShareWidget_SharePreview' | |
+ '&st.imageUrl=' + encodeURIComponent(_options.pimg) | |
+ '&st.description=' + encodeURIComponent(_options.ptext) | |
+ '&st.shareUrl=' + encodeURIComponent(_options.purl) | |
+ '&st.title=' + encodeURIComponent(_options.ptitle) | |
}, | |
нужно создать приложение https://developers.facebook.com/ | |
1) проставить домен | |
2) выбрать сайт | |
facebookApp: function(_options) { | |
FB.ui({ | |
method: 'share_open_graph', | |
action_type: 'og.shares', | |
action_properties: JSON.stringify({ | |
object : { | |
'og:url': _options.purl, | |
'og:title': _options.ptitle, | |
'og:description': _options.ptext, //todo: IE sharing crushes on that line - long strings are not supported | |
'og:image:width': '2560', | |
'og:image:height': '960', | |
'og:image': _options.pimg | |
} | |
}) | |
}) | |
}, | |
// Открыть окно шаринга | |
popup: function(url) { | |
return window.open(url,'','toolbar=0,status=0,scrollbars=1,width=626,height=436'); | |
} | |
} | |
var btn = моя кнопка | |
btn.on('click', function (e) { | |
e.preventDefault(); | |
var target = $(e.target); | |
var socialType = target.attr('data-social-type'); | |
var args = { | |
type: target.attr('data-social-type'), | |
purl: target.attr('data-purl'), | |
ptitle: target.attr('data-ptitle'), | |
pimg: target.attr('data-pimg'), | |
ptext: target.attr('data-ptext') | |
}; | |
Share.go(socialType, args); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment