简单的 JQuery 数学语法 - 如何减法?
你好,精彩的社区,
我正在尝试用 JQuery 做一些非常简单的数学,但我找不到带有数学语法的作弊(这与我想做的事情相关)。我知道如何进行添加和添加乘法但减法却让我困惑!
我需要做的是将样式分配给“#photo”,其值为(视口总宽度 - (241px + (height*0.32)))。这段代码成功地获取了视口的总宽度并将其指定为 #photo 的宽度:
$(document).ready(function(){
var width = $(window).width();
$('#photo').css('width', width);
});
但是我通过 ('width', (width- (value + value))) 尝试过的任何方法都不起作用 - 有人可以吗让我知道减法和嵌套方程的语法是什么?
或者,不是再次计算 (height*0.32),而是如何将 (height*0.32) 的输出分配给 var,以便我可以重新使用它,使宽度计算类似于 ('width', (width- ( 281px - varname)))?
这是整个函数,包括(高度*0.32)计算:
$(document).ready(function(){
var height = $(window).height();
var width = $(window).width();
$('#menu').css('height', height);
$('#menu').css('width', (height*0.29));
$('#menu').css('position','fixed');
$('#menu').removeClass('nojavascript');
$('#novel').css('padding-left', (height*0.32));
$('#novel').removeClass('nojavascript');
$('#photo').css('width', width);
});
呃,关于如何整理我的代码的进一步提示也很感激。只要看看它,我就可以猜测出一些很可能是不必要的重复的东西!
根据要求编辑: 这是减法问题,我现在正在处理这个问题:
$('img').css('width', (width - (281+(height*0.32))) + 'px');
现在我正在努力解决的问题是如何计算 img 的高度,以便它保留 4:3 的照片比例!不过现在已经凌晨四点了,所以要等到早上。
Hello wonderful community,
I am trying to do some really simple math with JQuery, but I can't find a cheatsheat with math syntax on it (that is relevant to what I'm trying to do). I know how to do additions & multiplication but subtraction is eluding me!
What I need to do is assign a style to "#photo" that has a value of (total width of viewport - (241px + (height*0.32))). This code is successfully grabbing the total width of the viewport and assigning it as the width of #photo:
$(document).ready(function(){
var width = $(window).width();
$('#photo').css('width', width);
});
But nothing I've tried by way of ('width', (width- (value + value))) has worked - can someone let me know what the syntax is for subtraction and nested equations?
OR, rather than calculate (height*0.32) again, how would I assign the output of (height*0.32) to a var so I can re-use it, making the width calculation something like ('width', (width- (281px - varname)))?
Here's the whole function including the (height*0.32) calc:
$(document).ready(function(){
var height = $(window).height();
var width = $(window).width();
$('#menu').css('height', height);
$('#menu').css('width', (height*0.29));
$('#menu').css('position','fixed');
$('#menu').removeClass('nojavascript');
$('#novel').css('padding-left', (height*0.32));
$('#novel').removeClass('nojavascript');
$('#photo').css('width', width);
});
Er, further tips on how to tidy my code also appreciated. Just looking at it I can guess at some things that might well be unnecessary duplications!
Edited per request:
Here is the subtraction problem, which I've got working now:
$('img').css('width', (width - (281+(height*0.32))) + 'px');
Now the problem I'm wrestling with is how to calculate a height for img so that it preserves a 4:3 photo ratio! But it's nearly 4am so that will have to wait til morning.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于格式化,有一些事情。 jQuery 可以获取 CSS 对象,因此一次性将所有内容传递进去。其次,只有当键不是有效的 javascript 标识符时(请参阅“padding-left”),才需要在其周围加上引号。第三,jQuery 对象链,因此您可以在
css()
调用之后内联removeclass()
调用。最后,缩进确实是你的朋友。最后,
height
和width
函数返回裸整数,但css()
函数要求您包含单位。最基本的单位(由height
和width
返回的单位)是像素:For formatting, a few things. jQuery can take objects for CSS, so pass everything in, in a single go. Secondly, only when the key isn't a valid javascript identifier (see "padding-left") do you need quotes around it. Third, jQuery objects chain, so you can follow the
css()
call with theremoveclass()
call in-line. And finally, indenting is really, really your friend.And finally, the
height
andwidth
functions return bare integers, but thecss()
function requires that you include units. The most fundamental unit, and the one returned byheight
andwidth
, is pixels: