优化zoom重复代码

This commit is contained in:
piaoyidage
2022-10-30 10:06:08 +08:00
parent 440a4cfdae
commit daf44a7523

View File

@@ -113,18 +113,18 @@ function setDefaultZoom() {
} }
} }
function zoomIn() { function zoomCommon(callback) {
const htmlZoom = window.localStorage.getItem('htmlZoom') || '100%'; const htmlZoom = window.localStorage.getItem('htmlZoom') || '100%';
const html = document.getElementsByTagName('html')[0]; const html = document.getElementsByTagName('html')[0];
const zoom = parseInt(htmlZoom) < 200 ? (parseInt(htmlZoom) + 10 +'%') : '200%'; const zoom = callback(htmlZoom);
html.style.zoom = zoom; html.style.zoom = zoom;
window.localStorage.setItem('htmlZoom', zoom); window.localStorage.setItem('htmlZoom', zoom);
} }
function zoomOut() { function zoomIn() {
const htmlZoom = window.localStorage.getItem('htmlZoom') || '100%'; zoomCommon(htmlZoom => parseInt(htmlZoom) < 200 ? (parseInt(htmlZoom) + 10 +'%') : '200%');
const html = document.getElementsByTagName('html')[0]; }
const zoom = parseInt(htmlZoom) > 30 ? (parseInt(htmlZoom) - 10 +'%') : '30%';
html.style.zoom = zoom; function zoomOut() {
window.localStorage.setItem('htmlZoom', zoom); zoomCommon(htmlZoom => parseInt(htmlZoom) > 30 ? (parseInt(htmlZoom) - 10 +'%') : '30%');
} }