分享一个选中文字转发到微博的js代码

2012.10.24 分享 7845 人浏览 留言

今天发的新模板DD-Athem中,其中有个选中文字能分享到新浪微博和腾讯微博的效果,感觉效果还是挺好的,那我把他单独提取出来,分享一下吧,方便大家可以单独加在自己的模板中使用。

首先当然是jq库支持啦!!(如果你的主题已经有jq库,那么直接跳过这一步吧)

<script type="text/javascript" src="http://t.papaapp.com/js/libs/jquery/jquery-1.7-latest.js"></script>

然后就是关键的js内容了,内容如下

<script type="text/javascript">
//记忆碎片分享 https://huilang.me
jQuery(document).ready(function(){
var miniBlogShare = function() {
$('<img id="imgSinaShare" class="img_share" title="将选中内容分享到新浪微博" src="https://huilang.me/wp-content/uploads/2013/08/095419PQv.gif" /><img id="imgQqShare" class="img_share" title="将选中内容分享到腾讯微博" src="https://huilang.me/wp-content/uploads/2013/08/095419NWF.gif" />').appendTo('body');
$('.img_share').css({
display : 'none',
position : 'absolute',
cursor : 'pointer'
}); var funGetSelectTxt = function() {
var txt = '';
if(document.selection) { txt = document.selection.createRange().text;
} else { txt = document.getSelection(); }
return txt.toString();};
$('html,body').mouseup(function(e) {
if (e.target.id == 'imgSinaShare' || e.target.id == 'imgQqShare') {
return }
e = e || window.event;
var txt = funGetSelectTxt(),
sh = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0,
left = (e.clientX - 40 < 0) ? e.clientX + 20 : e.clientX - 40,
top = (e.clientY - 40 < 0) ? e.clientY + sh + 20 : e.clientY + sh - 40;
if (txt) {
$('#imgSinaShare').css({
display : 'inline',
left : left,
top : top });
$('#imgQqShare').css({
display : 'inline',
left : left + 30,
top : top });
} else { $('#imgSinaShare').css('display', 'none');
$('#imgQqShare').css('display', 'none');
} });
$('#imgSinaShare').click(function() {
var txt = funGetSelectTxt(), title = $('title').html();
if (txt) {
window.open('http://v.t.sina.com.cn/share/share.php?title=' + txt + ' —— 转载自:' + title + '&url=' + window.location.href);
} });
$('#imgQqShare').click(function() {
var txt = funGetSelectTxt(), title = $('title').html();
if (txt) {
window.open('http://v.t.qq.com/share/share.php?title=' + encodeURIComponent(txt + ' —— 转载自:' + title) + '&url=' + window.location.href);
} }); }(); }); // JavaScript Document
</script>

js部分放在底部body前即可。

效果图

要演示的话,选中本文的文字即可。现用主题未添加这个功能,所以演示不了了

文章地址:https://huilang.me/chose-share/

回复本文

您的电子邮箱地址不会被公开。 必填项已用 * 标注

icon_wink.gif icon_neutral.gif icon_mad.gif icon_twisted.gif icon_smile.gif icon_eek.gif icon_sad.gif icon_rolleyes.gif icon_razz.gif icon_redface.gif icon_surprised.gif icon_mrgreen.gif icon_lol.gif icon_idea.gif icon_biggrin.gif icon_evil.gif icon_cry.gif icon_cool.gif icon_arrow.gif icon_confused.gif icon_question.gif icon_exclaim.gif