Skip to content

Instantly share code, notes, and snippets.

@cyokodog
Last active October 12, 2015 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cyokodog/4087991 to your computer and use it in GitHub Desktop.
Save cyokodog/4087991 to your computer and use it in GitHub Desktop.
jQuery Twitter Share

#jQuery Twitter Share

ツイートボタンを生成する jQuery プラグイン。

記事ページでの使用例。

//ボタンタグの生成、挿入
$.twitterShare().appendTo('#social-buttons');

//レンダリング
$.twitterShare.rendar();

一覧ページでの使用例。

//ボタンタグの生成、挿入
var social = $('.post .social-buttons');
$('.post h2 a').each(function(idx){
	var target = $(this);
	$.twitterShare({
		'data-url' : target.prop('href'),
		'data-text' : target.text()
	}).appendTo(social.eq(idx));
});

//レンダリング
$.twitterShare.rendar();

下記パラメータを指定可能。

data-url : 共有されるURL

data-text : ツイート本文

data-via : ユーザー名

data-lang : 言語

data-size : ボタンの大きさ

data-related : 関連アカウント

data-count : ツイート数のカウントの有無

data-hashtags : ハッシュタグ

(function($){
$.twitterShare = function(option){
var c = $.extend({},$.twitterShare.defaults,option);
var data = {};
for(var i in c) !c[i] || !(/^data-.+/.test(i)) || (data[i] = c[i]);
return $('<a/>').
text(c.text).
addClass('twitter-share-button').
prop({href : "https://twitter.com/share"}).
attr(data);
}
$.twitterShare.rendar = function(){
$('<script/>').prop({
charset : 'utf-8',
src : 'http://platform.twitter.com/widgets.js'
}).appendTo('body');
}
$.twitterShare.defaults = {
'text' : 'Tweet',
'data-url' : '',
'data-text' : '',
'data-via' : '',
'data-lang' : '',
'data-size' : '',
'data-related' : '',
'data-count' : '',
'data-hashtags' : ''
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment