简体   繁体   English

JavaScript变量串联不起作用

[英]Javascript variable concatenation not working

I know there have been may related posts but for the life of me I can't seem to figure out why the script isn't implementing my variables associated value. 我知道可能有相关的文章,但是对于我来说,我似乎无法弄清楚为什么脚本没有实现与变量相关的值。

Here is the fiddle: https://jsfiddle.net/9s4k9449/5/ 这是小提琴: https : //jsfiddle.net/9s4k9449/5/

In brief: 简单来说:

  1. A red div is fixed at 743px wide until the width of the window gets to 734px at which point the red div's width changes to 100%. 红色div的宽度固定为743px,直到窗口的宽度变为734px,此时红色div的宽度变为100%。

  2. A function called getFraction() measures the width of the red div and divides it by the red divs original width of 743 to create a variable named myfraction. 名为getFraction()的函数将测量红色div的宽度,并将其除以红色divs的原始宽度743,以创建名为myfraction的变量。 The result is that when the window is larger than 734px wide myfraction is equal to 1, otherwise the fraction displays as a you got it, a fraction. 结果是,当窗口大于734px时,myfraction等于1,否则分数显示为您所获得的分数。

  3. A blue div is contained within the red div, and is scaled using webkitTransform using myfraction's value. 蓝色div包含在红色div中,并使用myfraction的值使用webkitTransform进行缩放。 The result should be that when the window is greater than 734px wide the blue div is not scaled because myfraction = 1 (scale = 1.), but when the window is less than 734px the blue div scales to myfraction. 结果应该是,当窗口大于734px宽时,由于myfraction = 1(比例= 1),蓝色div不会缩放。但是,当窗口小于734px时,蓝色div会缩放至myfraction。

NOTES: The problem lies in the function "scale()". 注意:问题在于函数“ scale()”。 I believe that this issue has to do with the function not grabbing the value of myfraction; 我认为,此问题与不获取myfraction值的功能有关。 probably a concatenation mistake that I can't find. 可能是我找不到的串联错误。 I believe this because when the variable "myfraction" is replaced with an actual number say 0.5, the entire process works perfectly (though the blue div is not scaled dynamically with the windows width the function does execute on window resize at a scale of 0.5). 我相信这是因为,当变量“ myfraction”被替换为实际数字(例如0.5)时,整个过程运行良好(尽管blue div不会随窗口宽度动态缩放,但该功能确实会在窗口缩放时以0.5的比例执行) 。

Here is the code: 这是代码:

 window.onresize = resizefunc; function resizefunc(){ getFraction(); scale(); } /*This function takes the red div's current width and divides it by its original width to create a fraction */ function getFraction() { var myfraction = document.getElementById("reddiv").offsetWidth / 743; document.getElementById("dimensions").innerHTML = myfraction ; } /*This function should take the blue div and scale it by the fraction */ function scale() { document.getElementById("bluediv").style.webkitTransform = 'scale(' + myfraction + ',' + myfraction + ')'; } 
 .red { width: 743px; height: 380px; background-color: red; margin: auto; } @media screen and (max-width: 743px) {.red {width: 100%}} #bluediv { background-color: blue; width: 400px; height: 380px; margin: auto; } 
 <div style="width: 100%; height: 500px;"> <div id="reddiv" class="red"> <div id ="bluediv"></div> </div> <p id="dimensions"></p> </div> 

Your size() function never had the myfraction value 您的size()函数从没有myfraction

https://jsfiddle.net/9s4k9449/6/ https://jsfiddle.net/9s4k9449/6/

window.onresize = resizefunc;

function resizefunc(){    
    scale(getFraction());
}
//This function takes the red div's current width and divides it by its original width
// to create a fraction
function getFraction() {    
    var myfraction = document.getElementById("reddiv").offsetWidth / 743; 
    document.getElementById("dimensions").innerHTML = myfraction ;
    return myfraction;
}   

//This function should take the blue div and scale it by the fraction
function scale(frac) {
    document.getElementById("bluediv").style.webkitTransform = 'scale(' + frac + ',' + frac + ')';
}   

The problem is with the scope of myfraction variable. 问题在于myfraction变量的范围。 You did not global it. 您没有全局它。

 window.onresize = resizefunc; var myfraction = 0; function resizefunc(){ getFraction(); scale(); } /*This function takes the red div's current width and divides it by its original width to create a fraction */ function getFraction() { myfraction = document.getElementById("reddiv").offsetWidth / 743; document.getElementById("dimensions").innerHTML = myfraction ; } /*This function should take the blue div and scale it by the fraction */ function scale() { document.getElementById("bluediv").style.webkitTransform = 'scale(' + myfraction + ',' + myfraction + ')'; } 
 .red { width: 743px; height: 380px; background-color: red; margin: auto;} @media screen and (max-width: 743px) {.red {width: 100%}} #bluediv { background-color: blue; width: 400px; height: 380px; margin: auto; } 
 <div style="width: 100%; height: 500px;"> <div id="reddiv" class="red"> <div id ="bluediv"></div> </div> <p id="dimensions"></p> </div> 

If i undersand your question :) I just joined two functions to just one, you can give it a try 如果我理解您的问题:)我只是将两个功能合并为一个,您可以尝试一下

 function getFraction() { var myfraction = document.getElementById("reddiv").offsetWidth / 743; document.getElementById("dimensions").innerHTML = myfraction ; document.getElementById("bluediv").style.webkitTransform = 'scale(' + myfraction + ',' + myfraction + ')'; } 

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

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