新闻中心

WordPress建设:添加jQuery代码实现把选中文字转发至微博

作者 / 无忧主机 时间 2014-08-31 09:43:39

不知道大家在看腾讯新闻的时候发现没有,你看到一段很精辟的文字,或者很优美的句子,就选中一下,在选中的文字右上角上有“微博转播”,无忧主机小编在想,如果把这个功能引进到我们的Wordpress站点里面,那岂不是锦上添花? 去查阅相关资料的时候,才知道,腾讯官方已经推出一个相同功能的应用,叫作 “Q-Share”,在继续查阅资料的时候,又发现已经有前辈写出了js页面文字选中后分享到新浪微博的方法,那我就省力多了,把双方的功能代码相互结合一下,就可以实现像腾讯新闻类似的功能啦!下面就是对Wordpress实现此功能的方法: 第一步:引入jQuery,相信大多数WordPress博客站点都已经引入了jQuery,那就可以直接进行第二步了。 第二步:在页面底部,更确切的说,在引入jQuery库的后面加上这样一段JS,你就可以看到和本站一样的效果了。 var miniBlogShare = function() { //指定位置驻入节点 $('<img id="imgSinaShare" class="img_share" title="将选中内容分享到新浪微博" src="http://upload.chinaz.com/2012/0203/1328255868614.gif" /><img id="imgQqShare" class="img_share" title="将选中内容分享到腾讯微博" src="http://upload.chinaz.com/2012/0203/1328255868314.png" />').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); } }); }(); 添加成功后,赶紧去查看效果吧!   无忧主机相关文章推荐阅读: WORDPRESS多站点功能教程 WORDPRESS评论头像不显示问题解决办法 WORDPRESS 网站后台自动压缩PNG格式图片方法 WORDPRESS限制会员或者游客评论内容的最小字数

本文地址:https://www.51php.com/wordpress/15989.html

1
1
1
1
1
1
1

客户服务热线

0791-8623-3537

在线客服