简体   繁体   English

使用javascript,范围输入和CSS转换的数学:scale()

[英]Mathematics with javascript, range input, and css transform: scale()

I have a div with the width 794px and a range input with max 250 and min 0. I am scaling the div with the css transform: scale(1), where 1 is when the div is actually shown at 794px. 我有一个宽度为794px的div,范围输入的最大值为250,最小值为0。我正在使用css变换:scale(1)缩放div,其中1表示div实际显示为794px。

However, when the width of the browser window is greater than that I want to be able to scale up even more so that when the window is at 1588px, the div should have the value scale(2). 但是,当浏览器窗口的宽度大于该宽度时,我希望能够进一步放大,以便当窗口为1588px时,div的值应为scale(2)。 And when the browser window is smaller than 794px the maximum value (250) should represent the width of the window. 当浏览器窗口小于794px时,最大值(250)应该表示窗口的宽度。

I have tried several different solutions for this, I will only post the last one so I won't confuse anyone. 我为此尝试了几种不同的解决方案,我只会发布最后一种,所以不会混淆任何人。

Javascript: Javascript:

const baseWidth = 794;
const maxWidth = (baseWidth * 2) + 50;
const workspace = document.getElementById('workspace');
const oWidth = workspace.offsetWidth;
const rangeValue = zoomElement.target.value;
const rangeStepSizeByPixels = (oWidth - 0) / 250;
const rangeStepSizeByPercent = (rangeStepSizeByPixels / oWidth) * 100;
const trueZoom = rangeValue * this.state.rangeStepSize;

this.setState({
    ...this.state,
    actualZoom: trueZoom,
    height: (700 * trueZoom) / 100,
    width: (550 * trueZoom) / 100,
});

HTML: HTML:

<section id="infobar" class="inner-margin">
    <input type="range" step="0.4" min="0" max="250" class="zoomslider" value="250">
</section>

<section id="workspace" class="scrollable tender-edit orange-midnight-them">

    <div class="doccontainer" style="width: 794px; height: 1123px;">
        <section class="init-scale" style="transform: scale(1);">
            <div class="doc-header">
            ...
            </div>
            <div class="doc-inner">
            ...
            </div>
            <div class="doc-footer">
            ...
            </div>
        </section>
    </div>

</section>

This resulted in a situation where the range value 250 was the same as setting scale(1), which is not what I am going for. 这导致范围值250与设置scale(1)相同的情况,这不是我想要的。

Well, after getting some help from a friend it turns out that the answer was much simpler than I thought. 好吧,在朋友的帮助下,事实证明答案比我想的要简单得多。

const factor = (oWidth - 50) / baseWidth;

(That -50 is only there to set some padding on the parent element.) (只有-50可以在父元素上设置一些填充。)

With this number I get the multiplication factor that I can then use to set the value of the percentage that I am after, and also to calculate the value that my range input should have as per default. 有了这个数字,我得到了乘数因子,然后可以用它来设置我所追求的百分比值,还可以计算我的范围输入默认应具有的值。

const rangeValue = zoomElement.target.value;
const trueZoom = (rangeValue / 250) * 100 * this.state.factor;

That is the value that I get when the range input is changed. 那是我改变范围输入时得到的值。

The following is my calculation to set the default zoom and the default range value based on the parent element's actual width. 以下是我根据父元素的实际宽度设置默认缩放比例和默认范围值的计算。

const defaultDocWidth = ((oWidth - 50) * 0.5) / 2;
const newFactor = (defaultDocWidth / (oWidth - 50));
const defaultZoom = (defaultDocWidth) * newFactor;
const defaultRangeValue = (250 / 100) * defaultZoom;

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

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