简体   繁体   English

导入 var 并调整 windows

[英]Import var and resize windows

I have some problems with an importing variable.我在导入变量时遇到了一些问题。 despite different events, the var is never updated...尽管发生了不同的事件,但 var 永远不会更新......

This is utilities file:这是实用程序文件:

function sizes() {
    let contentWidth = [...document.body.children].reduce(
        (a, el) => Math.max(a, el.getBoundingClientRect().right), 0)
        - document.body.getBoundingClientRect().x;

    window.addEventListener('resize', sizes)

    return {
        windowWidth:  document.documentElement.clientWidth,
        windowHeight: document.documentElement.clientHeight,
        pageWidth:    Math.min(document.body.scrollWidth, contentWidth),
        pageHeight:   document.body.scrollHeight,
        screenWidth:  window.screen.width,
        screenHeight: window.screen.height,
        pageX:        document.body.getBoundingClientRect().x,
        pageY:        document.body.getBoundingClientRect().y,
        screenX:     -window.screenX,
        screenY:     -window.screenY - (window.outerHeight-window.innerHeight),
    }
}

export default sizes();

Here, I import my var在这里,我导入我的 var

import size from "../../utilities/size";

window.addEventListener('resize', () => {
    console.log(size.windowWidth);
})

When you call export default sizes();当您调用export default sizes(); you are actually evaluating the size function.您实际上是在评估 function 的size Your utilities file is actually the same as this:您的实用程序文件实际上与此相同:

const size = {
        windowWidth:  document.documentElement.clientWidth,
        windowHeight: document.documentElement.clientHeight,
        pageWidth:    Math.min(document.body.scrollWidth, contentWidth),
        pageHeight:   document.body.scrollHeight,
        screenWidth:  window.screen.width,
        screenHeight: window.screen.height,
        pageX:        document.body.getBoundingClientRect().x,
        pageY:        document.body.getBoundingClientRect().y,
        screenX:     -window.screenX,
        screenY:     -window.screenY - (window.outerHeight-window.innerHeight),
    }
export default size;

Now if you want that code to be reactive, and calculate the variables each time you call them, you can use a simple Accessor Get Method (MDN) which will evaluate those for you, each time you access those properties:现在,如果您希望该代码具有反应性,并在每次调用它们时计算变量,您可以使用一个简单的Accessor Get Method (MDN) ,它会在您每次访问这些属性时为您评估这些:

const size = {
  get windowWidth() {
    return document.documentElement.clientWidth
  },
  get windowHeight() {
    return document.documentElement.clientHeight
  },
  get pageWidth() {
     let contentWidth = [...document.body.children].reduce(
        (a, el) => Math.max(a, el.getBoundingClientRect().right), 0)
        - document.body.getBoundingClientRect().x;
    return Math.min(document.body.scrollWidth, contentWidth)
  },
  get pageHeight() {
    return document.body.scrollHeight
  },
  get screenWidth() {
    return window.screen.width
  },
  get screenHeight() {
    return window.screen.height
  },
  get pageX() {
    return document.body.getBoundingClientRect().x
  },
  get pageY() {
    return document.body.getBoundingClientRect().y
  },
  get screenX() {
    return -window.screenX
  },
  get screenY() {
    return -window.screenY - (window.outerHeight - window.innerHeight)
  },
}
export default size;

By doing this you will neither need to bind any event handler in the utils file, just plug them where you need.通过这样做,您无需在 utils 文件中绑定任何事件处理程序,只需将它们插入您需要的位置。

Now your user file will be:现在您的用户文件将是:

import size from "../../utilities/size";

window.addEventListener('resize', () => {
    console.log(size.windowWidth);
})

And it will log a changed value each time you resize your window!每次调整窗口大小时,它都会记录一个更改的值!

Further analysis of your code:进一步分析您的代码:

export default sizes();

// is actually the same as
const $tmp = sizes();
export default $tmp;

// Now take a look at what sizes does:
function sizes() {
    ...    
    
    // this evaluates instantly your object
    return { windowWidth, ... }
    
    // is the same as
    const result = { windowWidth, ... }
    return result
    // if you now console.log one of those properties, they are fixed numbers
    // and they will not be changed inside this function
    
}


// But, why the object is not changing when the window resizes?
// Every time you call the function, it returns a new instance of the object
function example() { return { } }
let A = size();
let B = size();
console.log(A == B) // false

// Now when you export the constant sizes() you actually
// referring (for example) to the object A
// When you bind the resize event to sizes, it will create another object B
// and change it:
window.addEventListener('resize', example);
// is the same as
window.addEventListener('resize', () => { return {} });

This snippet tries to explicate what is happening behind the scenes:这个片段试图解释幕后发生的事情:

 const btn = document.getElementById('resize'); let OBJECT_ID = 0; let exportedObject = null; // emulates sizes() function makeMyObject() { OBJECT_ID += 1; const result = { id: OBJECT_ID }; // first time just print the export value if (.exportedObject) { console:log('exported ID,'. result;id). } // the other times print exported value and current returned value else { console:log('exported ID,'. exportedObject,id: 'returned ID,'. result;id) } return result. } // emulates window,addEventListener('resize'; sizes). btn,addEventListener('click'; makeMyObject); // emulates export default sizes() exportedObject = makeMyObject();
 <p>Click the button to emulate the <code>winow.resize Event</code></p> <button id="resize">Emulate</button>

You should not call the function in the export.您不应在导出中调用 function。 Leave that up to whatever is importing the function.将其留给导入 function 的任何事情。

Also, do not declare the resize event listener inside of the function.此外,不要在 function 中声明调整大小事件侦听器。

Additionally, the variable/function "dimensions" is slightly-less cryptic that "size/sizes".此外,变量/函数“维度”比“大小/大小”稍微不那么神秘。

Definition定义

/app/utilities/dimensions.js

const dimensions = () => {
  let contentWidth = [...document.body.children].reduce(
      (a, el) => Math.max(a, el.getBoundingClientRect().right), 0) -
    document.body.getBoundingClientRect().x;

  return {
    windowWidth  : document.documentElement.clientWidth,
    windowHeight : document.documentElement.clientHeight,
    pageWidth    : Math.min(document.body.scrollWidth, contentWidth),
    pageHeight   : document.body.scrollHeight,
    screenWidth  : window.screen.width,
    screenHeight : window.screen.height,
    pageX        : document.body.getBoundingClientRect().x,
    pageY        : document.body.getBoundingClientRect().y,
    screenX      : -window.screenX,
    screenY      : -window.screenY - (window.outerHeight - window.innerHeight),
  }
};

export default dimensions; // Do not call as a function...

Usage用法

/app/view/components/main.js

import dimensions from '../../utilities/dimensions';

window.addEventListener('resize', () => {
  const { windowWidth } = dimensions(); // Call as a function...
  console.log(windowWidth);
})

I think what you are trying to do is我认为你想要做的是

function sizes() {
let contentWidth = [...document.body.children].reduce(
    (a, el) => Math.max(a, el.getBoundingClientRect().right), 0)
    - document.body.getBoundingClientRect().x;

return {
    windowWidth:  document.documentElement.clientWidth,
    windowHeight: document.documentElement.clientHeight,
    pageWidth:    Math.min(document.body.scrollWidth, contentWidth),
    pageHeight:   document.body.scrollHeight,
    screenWidth:  window.screen.width,
    screenHeight: window.screen.height,
    pageX:        document.body.getBoundingClientRect().x,
    pageY:        document.body.getBoundingClientRect().y,
    screenX:     -window.screenX,
    screenY:     -window.screenY - (window.outerHeight-window.innerHeight),
}}

window.addEventListener('resize', () => {
    console.log(sizes());
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM