$(function () { // 分享插件功能 $.fn.globalShare = function () { // 获取对象 var _this = this; var win_w = $(window).width(); var headObj = $(document.head); // 初始化 var defaults = { url: location.href, site_url: location.origin, title: headObj.find('[name=title], [name=Title]').attr('content') || document.title, description: headObj.find('[name=description], [name=Description]').attr('content') || '', image: $('img:first').prop('src') || '', wechatQrcodeTitle: $("html").hasClass("LANG-cn") ? '使用微信扫描二维码' : "Use Wechat scan QR code", sites: ['favorite', 'weibo', 'qq', 'wechat', 'tencent', 'douban', 'qzone', 'linkedin', 'diandian', 'facebook', 'twitter', 'google'], isInit: false, email: "information@huawei.com", } // 定义模块 var templates = { wechat: 'javascript:;', weibo: 'https://service.weibo.com/share/share.php?url={{URL}}&title={{TITLE}}&pic={{IMAGE}}', linkedin: 'https://www.linkedin.com/shareArticle?mini=true&ro=true&title={{TITLE}}&url={{URL}}&summary={{DESCRIPTION}}&source={{TITLE}}&armin=armin', facebook: 'https://www.facebook.com/sharer/sharer.php?u={{URL}}', twitter: 'https://twitter.com/intent/tweet?text={{TITLE}}&url={{URL}}&via={{SITE_URL}}', google: 'https://plus.google.com/share?url={{URL}}', email: 'mailto:{{EMAIL}}?subject={{TITLE}}&body={{URL}}' + encodeURIComponent('\n') + '{{DESCRIPTION}}', print: 'javascript:window.print();void(0)' } // 初始化 init(); function init() { _this.each(function () { var _this = $(this); // 是否需要初始化 if (_this.data('isInit')) { return true; } // 合并最新的配置参数 var newDefaults = $.extend({}, defaults, _this.data()); // 获取html结构 var getHtml = createHtml(_this, newDefaults); // 把结构放进social-share dom里 _this.html('') _this.append(getHtml) // 创建微信二维码 createWechat(_this, newDefaults); }); } // 创建微信 function createWechat(conObj, newDefaults) { var wechatObj = conObj.find('a.icon-wechat'); wechatObj.append('

' + newDefaults.wechatQrcodeTitle + '

'); var width = 138; var margin = 2; var qr = qrcode(0, 'H'); qr.addData(newDefaults.url); qr.make(); var moduleCount = qr.getModuleCount(); var cell = (width - margin * 2) / moduleCount; wechatObj.find('.qrcode').html(qr.createImgTag(cell, margin)); } // 创建url function createUrl(name, options) { var template = templates[name]; for (var key in options) { if (options.hasOwnProperty(key)) { var value = encodeURIComponent(options[key]); template = template.replace(new RegExp('{{' + key.toUpperCase() + '}}', 'g'), value); } } return template; } // 创建icon function createHtml(obj, options) { var htmlStr = ''; var sites = (options['sites']).split(','); for (var i = 0; i < sites.length; i++) { var urlStr = createUrl(sites[i], options); if (sites[i] == 'wechat') { htmlStr += ''; } else { htmlStr += ''; } } return htmlStr; } // 判断设备 function isMobileScreen() { return win_w <= 750; } } // 调用分享插件 $(function () { $('.share-component,.social-share').globalShare(); }); }) // 侧边栏 $(function () { var win_w = $(window).width(); if (win_w > 1200) { // 设置定位位置 $('.global-share .pop-up-layer').css('top', -($('.global-share .pop-up-layer').height() / 2 - 20)); // 复制Url链接 $('.global-share .J-copy-link').on('click', function () { copyUrl($(this)); //出现提示 $('.global-share .pop-up-layer .con .copy-link .success').fadeIn(200); setTimeout(function () { $('.global-share .pop-up-layer .con .copy-link .success').fadeOut(200); }, 400); }); function copyUrl(obj) { if ($('#urlText').length == 0) { // 创建input obj.after(''); } $('#urlText').select(); //选择对象 document.execCommand("Copy"); //执行浏览器复制命令 } } })