英文:
when I increase window width then margin will increase and when i decrease window width margin will reduce
问题
let getElement = document.getElementById("resize");
let width = window.innerWidth;
let arr = [];
arr.push(width);
let x = 1;
window.addEventListener("resize", () => {
arr.reduce((current, old) => {
if (current) {
getElement.style.marginTop = `${(x += 1)}px`;
} else {
getElement.style.marginTop = `${(x -= 1)}px`;
}
}, width - 1);
});
if current value margin will increase and if old value margin will reduce
英文:
let getElement = document.getElementById("resize");
let width = window.innerWidth;
let arr = [];
arr.push(width);
let x = 1;
window.addEventListener("resize", () => {
arr.reduce((current, old) => {
if (current) {
getElement.style.marginTop = `${(x += 1)}px`;
} else {
getElement.style.marginTop = `${(x -= 1)}px`;
}
}, width - 1);
});
if current value margin will increase and if old value margin will reduce
答案1
得分: 0
尝试以下代码
let getElement = document.getElementById("resize");
let arr = [window.innerWidth];
let x = 1;
window.addEventListener("resize", () => {
let width = window.innerWidth;
let diff = width - arr[arr.length - 1];
if (diff > 0) {
x += 1;
} else if (diff < 0) {
x -= 1;
}
getElement.style.marginTop = `${x}px`;
arr.push(width);
});
英文:
Try the below code
let getElement = document.getElementById("resize");
let arr = [window.innerWidth];
let x = 1;
window.addEventListener("resize", () => {
let width = window.innerWidth;
let diff = width - arr[arr.length - 1];
if (diff > 0) {
x += 1;
} else if (diff < 0) {
x -= 1;
}
getElement.style.marginTop = `${x}px`;
arr.push(width);
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论