在線QQ懸浮插件

http://vipshow.iteye.com/blog/1861281


http://jingyan.baidu.com/article/6181c3e0482621152ef153da.html


最近做網站需要在線QQ懸浮功能,且QQ是通過從數據庫獲取的集合。

下面例子爲靜態的QQ list集合。在此感謝 zhaizhai1988 的分享,對原文我做了相應的調整,代碼看起來更整齊,而且我上傳整個例子,供有需要的朋友下載,下載後直接就可以預覽效果。

①js工具類 qqOnline.js

Js代碼  收藏代碼
  1. /** 
  2. * 
  3. *  qqOnline - Sonline 
  4. *  author:selina 
  5. * 
  6. **/  
  7. (function($) {  
  8.     $.fn.Sonline = function(options) {  
  9.         var opts = $.extend({}, $.fn.Sonline.defualts, options);  
  10.         $.fn.setList(opts); // 調用列表設置  
  11.         if (opts.DefaultsOpen == false) {  
  12.             $.fn.Sonline.close(opts.Position, 0);  
  13.         }  
  14.         // 展開  
  15.         $("#SonlineBox > .openTrigger").live("click"function() {  
  16.             $.fn.Sonline.open(opts.Position);  
  17.         });  
  18.         // 關閉  
  19.         $("#SonlineBox > .contentBox > .closeTrigger").live("click",  
  20.                 function() {  
  21.                     $.fn.Sonline.close(opts.Position, "fast");  
  22.                 });  
  23.         // Ie6兼容或滾動方式顯示  
  24.         if ($.browser.msie && ($.browser.version == "6.0") && !$.support.style  
  25.                 || opts.Effect == true) {  
  26.             $.fn.Sonline.scrollType();  
  27.         } else if (opts.Effect == false) {  
  28.             $("#SonlineBox").css({  
  29.                 position : "fixed"  
  30.             });  
  31.         }  
  32.     }  
  33.     // plugin defaults  
  34.     $.fn.Sonline.defualts = {  
  35.         Position : "left",// left或right  
  36.         Top : 200,// 頂部距離,默認200px  
  37.         Effect : true// 滾動或者固定兩種方式,布爾值:true或  
  38.         DefaultsOpen : true// 默認展開:true,默認收縮:false  
  39.         Qqlist : "" // 多個QQ用','隔開,QQ和客服名用'|'隔開  
  40.     }  
  41.     // 展開  
  42.     $.fn.Sonline.open = function(positionType) {  
  43.         var widthValue = $("#SonlineBox > .contentBox").width();  
  44.         if (positionType == "left") {  
  45.             $("#SonlineBox > .contentBox").animate({  
  46.                 left : 0  
  47.             }, "fast");  
  48.         } else if (positionType == "right") {  
  49.             $("#SonlineBox > .contentBox").animate({  
  50.                 right : 0  
  51.             }, "fast");  
  52.         }  
  53.         $("#SonlineBox").css({  
  54.             width : widthValue + 4  
  55.         });  
  56.         $("#SonlineBox > .openTrigger").hide();  
  57.     }  
  58.     // 關閉  
  59.     $.fn.Sonline.close = function(positionType, speed) {  
  60.         $("#SonlineBox > .openTrigger").show();  
  61.         var widthValue = $("#SonlineBox > .openTrigger").width();  
  62.         var allWidth = (-($("#SonlineBox > .contentBox").width()) - 6);  
  63.         if (positionType == "left") {  
  64.             $("#SonlineBox > .contentBox").animate({  
  65.                 left : allWidth  
  66.             }, speed);  
  67.         } else if (positionType == "right") {  
  68.             $("#SonlineBox > .contentBox").animate({  
  69.                 right : allWidth  
  70.             }, speed);  
  71.         }  
  72.         $("#SonlineBox").animate({  
  73.             width : widthValue  
  74.         }, speed);  
  75.     }  
  76.     // 子插件:設置列表參數  
  77.     $.fn.setList = function(opts) {  
  78.         $("body")  
  79.                 .append(  
  80.                         "<div class='SonlineBox' id='SonlineBox' style='top:-600px;'><div class='openTrigger' style='display:none' title='展開'></div><div class='contentBox'><div class='closeTrigger'><img src='icon-open.png' title='關閉' /></div><div class='titleBox'><span>客服中心</span></div><div class='listBox'></div></div></div>");  
  81.         if (opts.Qqlist == "") {  
  82.             $("#SonlineBox > .contentBox > .listBox").append(  
  83.                     "<p style='padding:15px'>暫無在線客服。</p>")  
  84.         } else {  
  85.             var qqListHtml = $.fn.Sonline.splitStr(opts);  
  86.             $("#SonlineBox > .contentBox > .listBox").append(qqListHtml);  
  87.         }  
  88.         if (opts.Position == "left") {  
  89.             $("#SonlineBox").css({  
  90.                 left : 0  
  91.             });  
  92.         } else if (opts.Position == "right") {  
  93.             $("#SonlineBox").css({  
  94.                 right : 0  
  95.             })  
  96.         }  
  97.         $("#SonlineBox").css({  
  98.             top : opts.Top  
  99.         });  
  100.         var allHeights = 0;  
  101.         if ($("#SonlineBox > .contentBox").height() < $(  
  102.                 "#SonlineBox > .openTrigger").height()) {  
  103.             allHeights = $("#SonlineBox > .openTrigger").height() + 4;  
  104.         } else {  
  105.             allHeights = $("#SonlineBox > .contentBox").height() + 4;  
  106.         }  
  107.         $("#SonlineBox").height(allHeights);  
  108.         if (opts.Position == "left") {  
  109.             $("#SonlineBox > .openTrigger").css({  
  110.                 left : 0  
  111.             });  
  112.         } else if (opts.Position == "right") {  
  113.             $("#SonlineBox > .openTrigger").css({  
  114.                 right : 0  
  115.             });  
  116.         }  
  117.     }  
  118.     // 滑動式效果  
  119.     $.fn.Sonline.scrollType = function() {  
  120.         $("#SonlineBox").css({  
  121.             position : "absolute"  
  122.         });  
  123.         var topNum = parseInt($("#SonlineBox").css("top") + "");  
  124.         $(window).scroll(function() {  
  125.             var scrollTopNum = $(window).scrollTop();// 獲取網頁被捲去的高  
  126.             $("#SonlineBox").stop(truetrue).delay(0).animate({  
  127.                 top : scrollTopNum + topNum  
  128.             }, "slow");  
  129.         });  
  130.     }  
  131.     // 分割QQ  
  132.     $.fn.Sonline.splitStr = function(opts) {  
  133.         var strs = new Array(); // 定義一數組  
  134.         var QqlistText = opts.Qqlist;  
  135.         strs = QqlistText.split(","); // 字符分割  
  136.         var QqHtml = ""  
  137.         for ( var i = 0; i < strs.length; i++) {  
  138.             var subStrs = new Array(); // 定義一數組  
  139.             var subQqlist = strs[i];  
  140.             subStrs = subQqlist.split("|"); // 字符分割  
  141.             QqHtml = QqHtml  
  142.                     + "<div class='QQList'><span>"  
  143.                     + subStrs[1]  
  144.                     + ":</span><a target='_blank' href='http://wpa.qq.com/msgrd?v=3&uin="  
  145.                     + subStrs[0]  
  146.                     + "&site=qq&menu=yes'><img border='0' src='http://wpa.qq.com/pa?p=2:"  
  147.                     + subStrs[0]  
  148.                     + ":41 &amp;r=0.22914223582483828' alt='點擊這裏'></a></div>"  
  149.         }  
  150.         return QqHtml;  
  151.     }  
  152. })(jQuery);  

 

②css樣式 qqOnline.css

Html代碼  收藏代碼
  1. .SonlineBox{ width:162px; font-size:12px;overflow:hidden; z-index:9999;}  
  2. .SonlineBox .openTrigger{ width:30px; height:110px; position:absolute; top:0px;  z-index:1; cursor:pointer;  background:#0176ba url(icon-close.png) no-repeat;}  
  3. .SonlineBox .titleBox{ width:158px; height:35px; line-height:35px; background:#038bdc url(icon-bg.png) repeat-x; border-bottom:2px solid #0176ba;}  
  4. .SonlineBox .titleBox span{ margin-left:10px; color:#fff; font-size:14px; font-family:'微軟雅黑','黑體';}  
  5. .SonlineBox .contentBox{ width:158px; height:auto; border:2px solid #0176ba; background:#fff; position:absolute; z-index:2;}  
  6. .SonlineBox .contentBox .closeTrigger{ width:25px; height:25px; display:block; cursor:pointer;  position:absolute; top:5px;right:5px;-webkit-transition:all 0.8s ease-out;}  
  7. .SonlineBox .contentBox .closeTrigger:hover{-webkit-transform:scale(1) rotate(360deg);}  
  8. .SonlineBox .contentBox .listBox{overflow:hidden; margin-bottom:10px;}  
  9. .SonlineBox .contentBox .listBox .QQList{ display:block; width:86%; height:22px; margin:10px auto 0px auto;}  
  10. .SonlineBox .contentBox .listBox .QQList span{float:left; line-height:22px;}  
  11. .SonlineBox .contentBox .listBox .QQList a{float:left;}  

 ③ html頁面

Html代碼  收藏代碼
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  5. <title>在線QQ客服懸浮插件</title>  
  6. <script type="text/javascript" src="jquery-1.6.4.js"></script>  
  7. <link href="qqOnline.css" rel="stylesheet" type="text/css"  
  8.     id="" media="all"></link>  
  9. <script type="text/javascript" src="qqOnline.js"></script>  
  10. <script>  
  11. $(function(){  
  12.     $("body").Sonline({  
  13.         Position:"right",//left或right  
  14.         Top:200,//頂部距離,默認200px  
  15.         Effect:true, //滾動或者固定兩種方式,布爾值:true或false  
  16.         DefaultsOpen:true, //默認展開:true,默認收縮:false  
  17.         Qqlist:"418114362|客服a,418114362|客服b,418114362|客服c,418114362|客服d,418114362|客服e" //多個QQ用','隔開,QQ和客服名用'|'隔開  
  18.     });  
  19. })  
  20. </script>  
  21. </head>  
  22. <body>  
  23.       
  24. </body>  
  25. </html> 
發佈了91 篇原創文章 · 獲贊 7 · 訪問量 50萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章