好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Wordpress中将选中内容分享到新浪腾讯微博 - Word

Wordpress中将选中内容分享到新浪腾讯微博

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

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

选中即分享的功能看上去比较高级,其实实现是相当简单的,其中的会让人头大,一般人也不感兴趣的原理这里就直接跳过,这个js文字选中后分享到新浪微博的功能我简单的封装了下,方法名是:$sinaMiniBlogShare

实例代码如下:

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

可以看到$sinaMiniBlogShare方法有两个参数,eleShare和eleContainer,其中,前一个参数是必须的,指的是文字选中后出现的浮动层元素(在本文demo中就是新浪眼睛图标),后面一个参数指文字选择的容器元素,可选参数,如果不设置则指document元素,也就是整个页面文字选中都会触发分享的功能.

假设新浪微博分享图标的HTML如下:

< img   id = "imgSinaShare"   class = "img_sina_share"   title = "将选中内容分享到新浪微博"   src = "http://simg.sinajs.cn/blog7style/images/common/share.gif"   />  

则直接使用如下代码:

$sinaMiniBlogShare(document.getElementById("imgSinaShare"));

查看更多关于Wordpress中将选中内容分享到新浪腾讯微博 - Word的详细内容...

  阅读:60次