百度了很久,想在这个Kubrick制作的wordpress经典默认主题default1.72上添加分享和打赏的功能,使用了网上那个集合了点赞、分享、打赏、海报功能的插件,但是不知道为什么,估计是和这个原始主题冲突或者和我添加的其他功能冲突,所以安装上去后点击分享等按钮都没有弹出效果。最后在张戈(zhang.ge)上找到了《分享张戈博客自用的仿百度打赏功能》,虽然没有分享和点赞以及海报功能,但是勉强使用吧。以下分享下自己的美化版,以供自己收藏。
一、建立js代码:
- /*!
- * 仿百度打赏的博客打赏组件
- * Date: 2016-09-10 11:00
- * https://zhang.ge/5110.html
- * (c) 2013-2016 张戈博客保留所有权利.
- *
- * 给博客添加模仿百度打赏的打赏组件
- * 张戈博客基于百度打赏的原创功能,引用或转载请保留版权申明,谢谢合作!
- */
- (function($){
- var id = Date.now();
- if($("#STYLE_"+id).size()<1){
- document.writeln("<style id='STYLE_"+id+"'>@CHARSET \"UTF-8\";*{-webkit-tap-highlight-color:rgba(255,0,0,0)}.box-size{box-sizing:border-box;-moz-box-sizing:border-box;-webkit-box-sizing:border-box}.ds-hide{display:none}.ds-reward-stl{font-family:\"microsoft yahei\";text-align:center;padding:10px 0;color:#666;margin:20px auto;width:90%}#dsRewardBtn:hover{background:#569ad5;-webkit-transition: all .4s ease-in-out;-moz-transition: all .4s ease-in-out;-ms-transition: all .4s ease-in-out;-o-transition: all .4s ease-in-out;}#dsRewardBtn {-webkit-transition: all .4s ease-in-out;-moz-transition: all .4s ease-in-out;-ms-transition: all .4s ease-in-out;-o-transition: all .4s ease-in-out;padding: 0;margin: auto;background: #4482b8;left: 110px;top: -7px;width: 50px;height: 50px;font-size: 16px;font-weight: 600;line-height: 43px;display: block;border: 4px solid #fff;border-radius: 40px;color: #FFF;}#dsRewardBtn span{display:inline-block;width:50px;height:50px;border-radius:100%;line-height:58px;color:#fff;font:400 25px/50px 'microsoft yahei';background:#FEC22C}#dsRewardBtn:hover{cursor:pointer}.ds-dialog{z-index:9999;width:100%;height:100%;position:fixed;top:0;left:0;border:1px solid #d9d9d9}.ds-dialog .ds-close-dialog{position:absolute;top:15px;right:20px;font:400 24px/24px Arial;width:20px;height:20px;text-align:center;padding:0;cursor:pointer;background:transparent;border:0;-webkit-appearance:none;font-weight:700;line-height:20px;opacity:.6;filter:alpha(opacity=20)}.ds-dialog .ds-close-dialog:hover{color:#000;text-decoration:none;cursor:pointer;opacity:.6;filter:alpha(opacity=40)}.ds-dialog-bg{position:absolute;opacity:.6;filter:alpha(opacity=30);background:#4482b8;z-index:9999;left:0;top:0;width:100%;height:100%}.ds-dialog-content{font-family:'microsoft yahei';font-size:14px;background-color:#FFF;position:fixed;padding:0 20px;z-index:10000;overflow:hidden;border-radius:6px;-webkit-box-shadow:0 3px 7px rgba(0,0,0,.3);-moz-box-shadow:0 3px 7px rgba(0,0,0,.3);box-shadow:0 3px 7px rgba(0,0,0,.3);-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box}.ds-dialog-pc{width:278px;height:355px;top:50%;left:50%;box-shadow: 0 1px 9px #000, 0 0 1px #000, 0 0 1px #000;margin:-190px 0 0 -140px}.ds-dialog-wx{width:90%;height:280px;top:50%;margin-top:-140px;margin-left:5%}.ds-dialog-content h5{text-align:left;font-size:15px;font-weight:700;margin:15px 0;color:#555}.ds-payment-way{text-align:center;color: white;}.ds-payment-way label{cursor:pointer;font-weight:400;display:inline-block;font-size:12px;margin: 0 2px 0 2px;padding:0}.ds-payment-way input[type=radio]{vertical-align:middle;margin:-2px 1px 0 0}.ds-payment-img{margin:15px 0;text-align:center}p.ds-pay-info{font-size:15px;margin:0 0 10px}.ds-pay-money{font-size:14px;margin-top:10px}.ds-pay-money p{margin:0}.ds-pay-money .ds-pay-money-sum{margin-bottom:4px}.ds-payment-img img{margin:0 auto;width:185px;}.ds-payment-img #qrCode_1{display:none}.ds-payment-img .qrcode-border{margin:0 auto}.ds-payment-img .qrcode-tip{width:48.13px;position:relative;margin:0 auto;font-size:12px;font-weight:500;background:#fff;height:15px;line-height:15px;margin-top:-12px}#qrCode_0 .qrcode-tip{color: #3bd154;}#qrCode_3 .qrcode-tip{color:#e10602}.ds-payment-img #qrCode_3{display:none}.ds-payment-img #qrCode_2{display:none}#qrCode_2 .qrcode-tip{color:#1976ff}#qrCode_1 .qrcode-tip{color: #ff5722;}.wx_qrcode_container{text-align:center}.wx_qrcode_container h2{font-size:17px}.wx_qrcode_container p{font-size:14px}.ds-reward-stl{border-radius: 100%;background: #4482b8;text-align: center;padding: 3px;color: #666;margin:20px auto;width: 50px;}#dsRewardBtn span{position:absolute;left:115px;top:-7px;background:#7ab951;width:50px;height:50px;font-size:16px;font-weight:600;line-height:43px;border:4px solid #fff;border-radius:40px}.share-s a{margin-top:-25px} .ds-payment-img .qrcode-border{border-radius: 29.97px; width: 236.89px; height: 236.89px; padding: 18.05px; margin-top: 25.53px; } </style>");
- }
- function write(){
- var content = "<div class=\"ds-dialog\" id='PAY_"+id+"'>"
- +" <div class=\"ds-dialog-bg\" onclick=\"PaymentUtils.hide();\"></div>"
- +" <div class=\"ds-dialog-content ds-dialog-pc \">"
- +" <i class=\"ds-close-dialog\">×</i>"
- +" <h5><i class=\"zm zm-xuanshang\" style=\"margin-right: 0px; font-weight: normal;font-size: 1.2em!important; color: #ff0000;\"></i>选择打赏方式:</h5>"
- +" <div class=\"ds-payment-way\">"
- +" <label for=\"wechat\" id=\"wepay\" style=\"border-radius: 3px;padding: 3px 5px 3px 2px;background: #3bd154;\"><input type=\"radio\" id=\"wechat\" class=\"reward-radio\" value=\"0\" checked=\"checked\" name=\"reward-way\" /><span class=\"weixin\"><i class=\"zm zm-weixinzhifu\" style=\"color: #fff;margin-right: 1px;font-size: 13px!important;\"></i>微信</span></label>"
- + " <label for=\"qqqb\" id=\"qqpay\" style=\"border-radius: 3px;padding: 3px 5px 3px 2px;color: white;background: #ff5722;\"><input type=\"radio\" id=\"qqqb\" class=\"reward-radio\" value=\"1\" name=\"reward-way\" /><span class=\"qqzhifu\"><i class=\"zm zm-QQ\" style=\"margin-right: 0px; font-size: 13px!important; color: #ffffff;\"></i>QQ钱包</span></label>"
- +" <label for=\"alipay\" id=\"zhifubao\" style=\"border-radius: 3px;padding: 3px 4px 3px 2px;background: #1976ff;\"><input type=\"radio\" id=\"alipay\" class=\"reward-radio\" value=\"2\" name=\"reward-way\" /><span class=\"zhifubao\"><i class=\"zm zm-alipay\" style=\"margin-right: 1px; font-size: 13px!important; color: #fff;\"></i>支付宝</span></label>"
- + " </div>"
- + " <div class=\"ds-payment-img\">"
- + " <div class=\"qrcode-img qrCode_0\" id=\"qrCode_0\">"
- + " <div class=\"qrcode-border box-size\" style=\"border: 9.02px solid #3bd154\">"
- + " <img class=\"qrcode-img qrCode_0\" id=\"qrCode_0\" src=\"https://long.ge/wp-content/themes/default.1.7.2/images/wepay.svg\" />"
- + " </div>"
- + " <p class=\"qrcode-tip\">打赏</p>"
- + " </div>"
- + " <div class=\"qrcode-img qrCode_1\" id=\"qrCode_1\">"
- + " <div class=\"qrcode-border box-size\" style=\"border: 9.02px solid #ff5722;\">"
- + " <img class=\"qrcode-img qrCode_1\" id=\"qrCode_1\" src=\"https://long.ge/wp-content/themes/default.1.7.2/images/qqpay.svg\" />"
- + " </div>"
- + " <p class=\"qrcode-tip\">打赏</p>"
- + " </div>"
- + " <div class=\"qrcode-img qrCode_2\" id=\"qrCode_2\">"
- + " <div class=\"qrcode-border box-size\" style=\"border: 9.02px solid #1976ff\">"
- + " <img class=\"qrcode-img qrCode_2\" id=\"qrCode_2\" src=\"https://long.ge/wp-content/themes/default.1.7.2/images/alipay.svg\" />"
- + " </div>"
- + " <p class=\"qrcode-tip\">打赏</p>"
- + " </div>"
- + " </div>"
- + " </div>"
- + " </div> ";
- $("body").append(content);
- }
- $(function(){
- write();
- var $pay = $("#PAY_"+id).hide();
- $pay.find(".ds-payment-way").bind("click",function(){
- $pay.find(".qrcode-img").hide();
- $pay.find(".qrCode_"+$pay.find("input[name=reward-way]:checked").val()).show();
- });
- $pay.find(".ds-close-dialog").bind("click",function(){
- $pay.hide();
- });
- });
- var PaymentUtils = window['PaymentUtils']={};
- PaymentUtils.show=function(){
- $("#PAY_"+id).show();
- }
- PaymentUtils.hide=function(){
- $("#PAY_"+id).hide();
- }
- })(jQuery);
建议复制以上代码,将代码中的如下地址替换你对应的付款二维码图片,然后保存为 js 文件,比如share.js,并且上传到主题目录下,比如放在主题新建文件夹share下。
二、添加打赏按钮代码
- <div class="ds-reward-stl">
- <button id="dsRewardBtn" onclick="PaymentUtils.show();">赏</button>
- </div>
- <!-- 下面的 js 地址修改和上一步保存的 js 地址一致 -->
- <script type="text/javascript" src="http://xxx.com/wp-content/themes/xxxx/share/.js"></script>
不建议js加在<header>里,因为这不是全局js,要不然会拖慢其他页面速度。
三、在主题文章页面添加美化代码:
- <style type='text/css'>
- input[type="radio"] {
- opacity: 0;
- width: 0;
- height: 0;
- }
- .weixin::before {
- display: inline-flex;
- width: 13px;
- height: 13px;
- padding: 1px;
- margin-top: -1px;
- margin-right: 2px;
- border-radius: 50%;
- background-color: white;
- border: 1.5px solid #fff;
- -webkit-box-sizing: border-box;
- box-sizing: border-box;
- content: "";
- }
- input[type="radio"]:checked + .weixin::before {
- margin-right: 2px;
- background-color: white;
- border: 1.5px solid #d9d9d9;
- background-clip: content-box;
- border-color: white;
- padding: 1px;
- margin-top: -1px;
- }
- .qqzhifu::before {
- display: inline-flex;
- width: 13px;
- height: 13px;
- margin-right: 2px;
- border-radius: 50%;
- background-color: white;
- border: 1.5px solid #fff;
- -webkit-box-sizing: border-box;
- box-sizing: border-box;
- content: "";
- padding: 1px;
- margin-top: -1px;
- }
- input[type="radio"]:checked + .qqzhifu::before {
- margin-right: 2px;
- background-color: white;
- border: 1.5px solid #d9d9d9;
- background-clip: content-box;
- border-color: white;
- padding: 1px;
- margin-top: -1px;
- }
- .zhifubao::before {
- display: inline-flex;
- width: 13px;
- height: 13px;
- margin-right: 2px;
- border-radius: 50%;
- background-color: white;
- border: 1.5px solid #fff;
- -webkit-box-sizing: border-box;
- box-sizing: border-box;
- content: "";
- padding: 1px;
- margin-top: -1px;
- }
- input[type="radio"]:checked + .zhifubao::before {
- margin-right: 2px;
- background-color: white;
- border: 1.5px solid #d9d9d9;
- background-clip: content-box;
- border-color: white;
- padding: 1px;
- margin-top: -1px;
- }
- </style>
以上代码是替换原始input中单选按钮radio颜色,以达到美化效果。
另外,以上打赏功能可以使用到任何网站,不一定是wordpress哦!
备注:打赏里的小图标我是使用了阿里云图标库里面的,所以可以自行去注册替换。
以上三步完工。效果如下:
打赏按钮效果
打赏界面效果