简体   繁体   English

如何在JavaScript中将常数加到innerWidth?

[英]How do I sum constant numbers to innerWidth in JavaScript?

I'm trying to sum the innerWidth and constant numbers in JS, but the browser keeps just attaching those numbers, not calculating. 我试图在JS中总结innerWidthconstant numbers ,但浏览器只是附加这些数字,而不是计算。

在此输入图像描述

The weird thing is giving minus values are working fine. 奇怪的是,给负值的工作正常。 The browser doesn't calculate only when I try to sum the numbers to innerWidth . 仅当我尝试将数字加到innerWidth时,浏览器才会计算。

Are there any ways to solve this issue? 有什么方法可以解决这个问题吗?

  Slider.prototype.sizeReseting = function(event) {
    this.frame.style.height = (window.innerWidth - '300') + 'px';
    this.frame.style.width = (window.innerWidth - '300') + 'px';
    this.film.style.height = (window.innerWidth - '400') + 'px';
    this.film.style.width = (window.innerWidth + '100') + 'px';
  }

Because you are concatenating strings . 因为你是连接字符串

In JavaScript, strings are defined between quotes or double quotes. 在JavaScript中,字符串在引号或双引号之间定义。 Hence var thing = 100; 因此var thing = 100; is a number but var thing = '100'; 是一个数字但var thing = '100'; is a string. 是一个字符串。

Strings are just a bunch of characters, with no numeral value (Not actually true, but for you to understand this easily). 字符串只是一堆字符, 没有数字值 (实际上并非如此,但是你很容易理解这一点)。 Having a string var thing = '100'; 字符串var thing = '100'; has the same number value as having the string var thing = 'asd'; 具有与字符串var thing = 'asd';相同的数值var thing = 'asd'; , which is none. ,没有。

Concatenating two strings gives the concatenation as a result, hence "asd" + "zxc" result in "asdzxc" , the same way you concatenate two strings like this: "100" + "100" results in "100100" . 连接两个字符串会产生串联,因此"asd" + "zxc"产生"asdzxc" ,就像你连接两个字符串一样: "100" + "100"产生"100100"

For your code to work, just remove the string delimitation and sum actual numbers, like this: 要使代码正常工作,只需删除字符串分隔并将实际数字相加,如下所示:

this.film.style.width = (window.innerWidth + 100) + 'px';

Note how I removed the single quotes around 100 for it to be an actual number. 请注意我如何删除100左右的单引号,因为它是实际数字。

Explanation: 说明:

This 2 lines have very different results: 这2行有非常不同的结果:

this.film.style.height = (window.innerWidth - '400') + 'px';
this.film.style.width = (window.innerWidth + '100') + 'px';
  • In the first one, the sign - converts your sting 400 to a number. 在第一个中,符号-将您的刺痛400转换为数字。 Therefore, you're substracting 400 to the value of window.innerWidth (and then adding the string 'px') 因此,您将window.innerWidth的值减去 400(然后添加字符串'px')

  • In the second one, the sign + is acting as a concatenation operator . 在第二个中,符号+充当连接运算符 Therefore, you're concatenating the result of window.innerWidth + the string '100' + the string 'px' . 因此,你串联的结果window.innerWidth +字符串'100' +字符串'px'

Solution: 解:

Just remove the single quotes from your code: 只需从代码中删除单引号即可:

this.film.style.width = (window.innerWidth + 100) + 'px'; 

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

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