当我增加窗口宽度时,边距将增加,当我减小窗口宽度时,边距将减少。

huangapple go评论97阅读模式
英文:

when I increase window width then margin will increase and when i decrease window width margin will reduce

问题

  1. let getElement = document.getElementById("resize");
  2. let width = window.innerWidth;
  3. let arr = [];
  4. arr.push(width);
  5. let x = 1;
  6. window.addEventListener("resize", () => {
  7. arr.reduce((current, old) => {
  8. if (current) {
  9. getElement.style.marginTop = `${(x += 1)}px`;
  10. } else {
  11. getElement.style.marginTop = `${(x -= 1)}px`;
  12. }
  13. }, width - 1);
  14. });

if current value margin will increase and if old value margin will reduce

英文:
  1. let getElement = document.getElementById("resize");
  2. let width = window.innerWidth;
  3. let arr = [];
  4. arr.push(width);
  5. let x = 1;
  6. window.addEventListener("resize", () => {
  7. arr.reduce((current, old) => {
  8. if (current) {
  9. getElement.style.marginTop = `${(x += 1)}px`;
  10. } else {
  11. getElement.style.marginTop = `${(x -= 1)}px`;
  12. }
  13. }, width - 1);
  14. });

if current value margin will increase and if old value margin will reduce

答案1

得分: 0

尝试以下代码

  1. let getElement = document.getElementById("resize");
  2. let arr = [window.innerWidth];
  3. let x = 1;
  4. window.addEventListener("resize", () => {
  5. let width = window.innerWidth;
  6. let diff = width - arr[arr.length - 1];
  7. if (diff > 0) {
  8. x += 1;
  9. } else if (diff < 0) {
  10. x -= 1;
  11. }
  12. getElement.style.marginTop = `${x}px`;
  13. arr.push(width);
  14. });
英文:

Try the below code

  1. let getElement = document.getElementById(&quot;resize&quot;);
  2. let arr = [window.innerWidth];
  3. let x = 1;
  4. window.addEventListener(&quot;resize&quot;, () =&gt; {
  5. let width = window.innerWidth;
  6. let diff = width - arr[arr.length - 1];
  7. if (diff &gt; 0) {
  8. x += 1;
  9. } else if (diff &lt; 0) {
  10. x -= 1;
  11. }
  12. getElement.style.marginTop = `${x}px`;
  13. arr.push(width);
  14. });

huangapple
  • 本文由 发表于 2023年2月18日 16:07:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75492013.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定