/** * 常用组件库 * **/ var linkCss_1 = document.createElement('link') linkCss_1.setAttribute('rel', 'stylesheet') linkCss_1.setAttribute('href', 'plugins/css/common.css') document.head.appendChild(linkCss_1) var linkCss_2 = document.createElement('link') linkCss_2.setAttribute('rel', 'stylesheet') linkCss_2.setAttribute('href', 'plugins/css/conponent.css') document.head.appendChild(linkCss_2) /* 对象继承 */ function extend(defaultVal, newVal) { for(var e in newVal) { defaultVal[e] = newVal[e] } } /* 网站右下角悬浮常用组件 */ // 构造悬浮组件 function Fixed() { this.fixed = null this.setting = { fixedStyle: 'default', dataValue: { phone: '0551-6668888', vxQrcode: 'plugins/img/icon-qrcode_1.png' } } } // 初始化 Fixed.prototype.init = function(opt){ extend(this.setting, opt) this.create() this.addEvent() } // 创建元素 Fixed.prototype.create = function() { this.fixed = document.createElement('div') this.fixed.className = 'conponent_fixed' this.fixed.innerHTML = `
意见
反馈

微信公众号

客服
电话
${this.setting.dataValue.phone}
置顶
` if (this.setting.fixedStyle !== '') { this.fixed.classList.add(this.setting.fixedStyle) } document.body.appendChild(this.fixed) } // 事件绑定 Fixed.prototype.addEvent = function() { var totop = document.getElementsByClassName('totop')[0]; var scrollTop = document.documentElement.scrollTop setToTop(); totop.onclick = function() { scrollTop = document.documentElement.scrollTop var timer = setInterval(function() { if (scrollTop > 0) { scrollTop -= 20 scrollTo(0, scrollTop) } else { clearInterval(timer) } }, 10) } window.onscroll = function() { scrollTop = document.documentElement.scrollTop; setToTop(); } function setToTop() { if (scrollTop > 400) { totop.classList.add('_show'); totop.classList.remove('_hide'); } else { totop.classList.add('_hide'); totop.classList.remove('_show'); } } }