~ function() {
function Back2top(config) {
this.config = config;
this.init();
}
// 创建构造函数Back2top,在它的原型上添加init属性
Back2top.prototype.init = function() {
var btn = document.createElement("div"),
time = null;
btn.style.position = "fixed";
btn.style.backgroundColor = this.config.bgcolor;
btn.style.color = this.config.color;
btn.style.bottom = "1.5em";
btn.style.right = "1.5em";
btn.style.visibility = "hidden";
btn.style.zIndex = "9998";
btn.innerText = "回到顶部";
btn.style.padding = "1em";
btn.style.width = "2em";
btn.style.lineHeight = "1.2em";
btn.style.borderRadius = ".5em";
btn.style.opacity = ".6";
btn.style.cursor = "default";
// 监测使用设备是手机还是pc
var userAgent = window.navigator.userAgent,
isMoblie = (!!userAgent.match(/iphone/i)) || (!!userAgent.match(/ipad/i)) || (!!userAgent.match(/Android/i));
document.body.appendChild(btn);
// 如果是移动端就给fontsize重新赋值,如果是pc端就监测它的视口宽度,在小于768时,改变字体大小
if (isMoblie) {
btn.style.fontSize = document.documentElement.clientWidth / 25 + "px";
} else {
btn.style.fontSize = document.documentElement.clientWidth < 768 ? "12px" : "14px";
window.onresize = function() {
btn.style.fontSize = document.documentElement.clientWidth < 768 ? "12px" : "14px";
};
}
// 监测滚动情况
window.onscroll = function() {
var objH = document.documentElement.clientHeight;
btn.style.visibility = document.body.scrollTop >= objH ? "visible" : "hidden";
};
// 回到顶部按钮点击,卷去内容逐渐减小,当到顶部时clear掉
btn.onclick = function() {
time = setInterval(function() {
document.body.scrollTop = document.body.scrollTop - document.body.scrollTop / 6;
if (document.body.scrollTop <= 0) {
clearInterval(time);
}
}, 50);
};
};
// 监听在文档全部加载完,将构造函数Back2top实例化;
document.addEventListener('DOMContentLoaded', function() {
var back2top = new Back2top({
bgcolor: "#777",
color: "#fff"
});
});
}();
function Back2top(config) {
this.config = config;
this.init();
}
// 创建构造函数Back2top,在它的原型上添加init属性
Back2top.prototype.init = function() {
var btn = document.createElement("div"),
time = null;
btn.style.position = "fixed";
btn.style.backgroundColor = this.config.bgcolor;
btn.style.color = this.config.color;
btn.style.bottom = "1.5em";
btn.style.right = "1.5em";
btn.style.visibility = "hidden";
btn.style.zIndex = "9998";
btn.innerText = "回到顶部";
btn.style.padding = "1em";
btn.style.width = "2em";
btn.style.lineHeight = "1.2em";
btn.style.borderRadius = ".5em";
btn.style.opacity = ".6";
btn.style.cursor = "default";
// 监测使用设备是手机还是pc
var userAgent = window.navigator.userAgent,
isMoblie = (!!userAgent.match(/iphone/i)) || (!!userAgent.match(/ipad/i)) || (!!userAgent.match(/Android/i));
document.body.appendChild(btn);
// 如果是移动端就给fontsize重新赋值,如果是pc端就监测它的视口宽度,在小于768时,改变字体大小
if (isMoblie) {
btn.style.fontSize = document.documentElement.clientWidth / 25 + "px";
} else {
btn.style.fontSize = document.documentElement.clientWidth < 768 ? "12px" : "14px";
window.onresize = function() {
btn.style.fontSize = document.documentElement.clientWidth < 768 ? "12px" : "14px";
};
}
// 监测滚动情况
window.onscroll = function() {
var objH = document.documentElement.clientHeight;
btn.style.visibility = document.body.scrollTop >= objH ? "visible" : "hidden";
};
// 回到顶部按钮点击,卷去内容逐渐减小,当到顶部时clear掉
btn.onclick = function() {
time = setInterval(function() {
document.body.scrollTop = document.body.scrollTop - document.body.scrollTop / 6;
if (document.body.scrollTop <= 0) {
clearInterval(time);
}
}, 50);
};
};
// 监听在文档全部加载完,将构造函数Back2top实例化;
document.addEventListener('DOMContentLoaded', function() {
var back2top = new Back2top({
bgcolor: "#777",
color: "#fff"
});
});
}();