WordPress将选中文字转发到微博

时间:2012-02-04 09:24:43 

最近在看腾讯新闻的时候,无意中发现,当我选中新闻中的文字的时候,鼠标右上角会显示一个“转播至微博”的按钮,点击后就会将选中的文字转发到微博上。这是一个很不错的用户体验,如果能把它引入到WordPress博客中,那不是很好吗?

为此我还特地去注册了一个腾讯微博开放平台的开发者,当我开始阅读开发文档的时候,才发现,他妹的,腾讯官方已经推出一个相同功能的应用,叫作 “Q-Share”,再翻阅了一下其他资料,原来已经有前辈写出了js页面文字选中后分享到新浪微博的方法,那我就省力多了,两者结合一下,把新浪微博和腾讯微博两个按钮都加上了,然后闲的蛋疼的我又把它翻译成了jQuery的方法。

效果的话就可以看本站了,选中任何文字,就会在右上角显示两个微博按钮,点击试试吧。

实现的方法也很简单,只需要两步:

1、引入jQuery,相信大多数WordPress博客都已经引入了jQuery,那就可以直接进行第二步了。

2、在页面底部,或者更确切的说,在引入jQuery库的后面加上这样一段JS,你就可以看到和本站一样的效果了。

var miniBlogShare = function() {
    //指定位置驻入节点
    $('<img id="imgSinaShare" class="img_share" title="将选中内容分享到新浪微博" src="https://img.aspxhome.com/file/UploadPic/20122/4/1328255868614-46.gif" /><img id="imgQqShare" class="img_share" title="将选中内容分享到腾讯微博" src="https://img.aspxhome.com/file/UploadPic/20122/4/1328255868314-33.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);
        }
    });
}();

是不是很简单呀?

赶紧选中本文的标题,在微博上通知你的好友来围观吧,哈哈~

PS:最后,说一句,其实这个方法不仅仅适用于WordPress,JS是通用的,你可以把这段 JS 挂载到任何网站上都可以得到相同的效果。

PS2:最最最后,再说一句,麻烦各位在使用前,把JS里的两张微博的图片路径替换成自己的地址,因为小站流量有限,大家帮我省着点花呗,嘿嘿。

原文地址:http://wange.im

标签:WordPress,微博
0
投稿

猜你喜欢

  • 帝国cms采集图文教程(中)

    2012-03-12 20:26:52
  • 谷歌下调链接 交换网站页面排名

    2007-10-25 23:08:00
  • 个人服务器架设其实很简单

    2010-08-23 18:49:00
  • Twitter攻击事件 俄罗斯疑是幕后黑手

    2009-12-08 13:37:00
  • 应用:基于Unix的Web服务器安全指南

    2009-07-23 20:00:00
  • 急功近利是做站的最大忌讳

    2008-09-28 12:59:00
  • 美国主机GoDaddy的Windows主机IIS6和IIS7有什么区别

    2010-04-08 12:57:00
  • GoDaddy主机 什么是ron Job?

    2010-04-12 12:42:00
  • 不同类型虚拟主机服务器权限设置实例

    2009-10-18 07:44:00
  • 百度更改网页权重算法 网站如何被顺利收录

    2008-12-04 08:55:00
  • 谈谈网赚行业里的一些潜规则

    2010-05-10 18:56:00
  • IIS使用十大原则

    2010-04-04 18:16:00
  • google adsense专员AdSensePro答站长问

    2007-08-22 10:44:00
  • 魅力炫彩妆 美图秀秀缔造可爱MM自拍秀

    2009-10-12 13:22:00
  • 针对Web服务器的八种攻击方式

    2008-11-22 11:28:00
  • DEDECMS更换模板的方法

    2009-05-31 17:09:00
  • 如何去掉wordpress中的category

    2011-08-10 19:09:21
  • 了解DNS服务器的工作原理及其过程

    2008-12-26 14:05:00
  • 草根的坚持 黑客的攻击

    2007-12-23 17:26:00
  • 盗链解决:服务器ISAPI Rewrite设置

    2011-08-14 06:34:29
  • asp之家 网站运营 m.aspxhome.com