Rumah > hujung hadapan web > html tutorial > html实现以一个简单的分享功能介绍

html实现以一个简单的分享功能介绍

黄舟
Lepaskan: 2017-05-26 16:17:13
asal
3481 orang telah melayarinya

超级简单的分享。

包括:QQ、QQ空间、新浪微博、腾讯微博,微信(只是一个二维码);

1、首先是html代码:

(前端我并不太会,一直用的都是bootstrap)

 1  <div class="col-sm-5 col-xs-5 btn btn-success img-fen"> 
 2                         <a href="#" class="a-link " onclick="open_share(&#39;qq&#39;)"> 
 3                             <img src="~/Content/WapHomeicon/qq.png" /> 
 4                             QQ好友 
 5                         </a> 
 6                     </div> 
 7  
 8                     <div class="col-sm-5 col-xs-5  btn btn-success img-fen"> 
 9                         <a href="#" class="a-link" data-toggle="modal" data-target="#myWeixin">
 10                             <img src="~/Content/WapHomeicon/weixin.png" />
 11                             微信
 12                         </a>
 13                     </div>
 14                     <div class="col-sm-5 col-xs-5  btn btn-success img-fen">
 15                         <a href="#" class="a-link" onclick="open_share(&#39;qzone&#39;)">
 16                             <img src="~/Content/WapHomeicon/qz.png" />
 17                             QQ空间
 18                         </a>
 19                     </div>
 20                     <div class="col-sm-5 col-xs-5  btn btn-success img-fen">
 21                         <a href="#" class="a-link" onclick="open_share(&#39;weibo&#39;)">
 22                             <img src="~/Content/WapHomeicon/weibo.png" />
 23                             新浪微博
 24                         </a>
 25                     </div><!-- /.modal-content -->
Salin selepas log masuk

1、然后js代码:

(这里不包括微信的,)

1  function open_share(type) { 
2         var shareUrl = ‘http://www.baidu.com’; 
3         var shareTitle = &#39;自定义标题&#39;; 
4         var shareImg = &#39;http://s.jiathis.com/qrcode.php?url=&#39; + shareUrl; 
5         var shareDesc = &#39;自定义内容&#39;; 
6         var openUrl = &#39;&#39;; 
7         switch (type) { 
8             case &#39;qzone&#39;: 
9                 openUrl = &#39;http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=&#39; + shareUrl + &#39;&title=&#39; + shareTitle + &#39;&pics=&#39; + shareImg
12                 break;
13             case &#39;weixin&#39;:
14                 t_delay(&#39;请在微信客户端使用&#39;);
15                 return false;
16                 break;
17             case &#39;qq&#39;:
18 openUrl = &#39;http://connect.qq.com/widget/shareqq/index.html?url=&#39; + shareUrl + &#39;&desc=&#39; + shareDesc + &#39;
&summary=&#39; + shareDesc + &#39;&site=&#39; + shareUrl + &#39;&pics=&#39; + shareImg;
21                 break;
22             case &#39;tqq&#39;:
23  openUrl = &#39;http://v.t.qq.com/share/share.php?title=&#39; + shareTitle + &#39;&url=&#39; + shareUrl + &#39;&site=&#39; + shareUrl + 
&#39;&pic=&#39; + shareImg;
26                 break;
27             case &#39;weibo&#39;:
28          openUrl = &#39; 
=&#39; + shareUrl + &#39;&title=&#39; +  shareTitle + &#39;&&source=&#39; + shareUrl + &#39;
&sourceUrl=&#39; + shareUrl + &#39;&content=&#39; + shareDesc + &#39;&pic=&#39; + shareImg;
33                 break;
34         }
35         window.open(openUrl);   
}
Salin selepas log masuk

3、然后微信的是弹出二维码:

(用的还是bootstrap模态框)

1  <!-- 模态框(Modal) --> 
2             <div class="modal fade" id="myWeixin" tabindex="-1" role="dialog" 
3                  aria-labelledby="myModalLabel" aria-hidden="true"> 
4                 <div class="modal-dialog" id="xian"> 
5                     <div class="modal-content"> 
6                         <div class="modal-header"> 
7                             <button type="button" class="close" data-dismiss="modal" 
8                                     aria-hidden="true"> 
9                                 ×
10                             </button>
11                             <h4 class="modal-title" id="myModalLabel">
12                                 用微信扫描二维码分享到朋友圈
13                             </h4>
14                         </div>
15                         <div class="modal-body erweima">
16                             <img src="http://s.jiathis.com/qrcode.php?url=http://www.baidu.com" alt="微信二维码" />
19                         </div>
20                         <div class="modal-footer">
21                             <button type="button" class="btn btn-default"
22                                     data-dismiss="modal">
23                                 关闭
24                             </button>
25                         </div>
26                     </div><!-- /.modal-content -->
27                 </div><!-- /.modal-dialog -->
28             </div><!-- /.modal -->
Salin selepas log masuk

4、最后是判断是否在微信中打开:

(从别处扒来的,我这里做的是如果是微信中打开,就把原先弹出的二维码和模态框给移除,放上一张带箭头的提示图片,让用户用微信自带的分享。)

1    //判断微信 
2    function is_weixn(){ 
3     var ua = navigator.userAgent.toLowerCase(); 
4     if(ua.match(/MicroMessenger/i)=="micromessenger") { 
5         $("div").remove("div[class=modal-content]"); 
6         var $htmlLi = $(&#39;<img src="~/Content/WapHomeicon/xian.png" style="margin-left:120px;" alt="xian"/>&#39;); 
7  
8         //创建DOM对象 
9          var $ul = $("#xian");   //获取UL对象
10          $ul.append($htmlLi); //将$htmlLi追加到$ul元素的li列表
11 
12         } else {
13             //不是微信
14            }
15    
}
Salin selepas log masuk

Atas ialah kandungan terperinci html实现以一个简单的分享功能介绍. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan