如何将偏移量 .top 应用于 div 的高度?
How to apply the offset .top to the height of a div?
在这个博客上,我想编辑#content div 的高度并使其成为高度:x.top px(article:last-child),这样背景垂直重复。
http://manutdstream.tumblr.com/
我试过这样做:
$(document).ready(function(){
x=$("article:last-child").offset();
$('#content').css('height' : 'x.top px');
});
我认为问题出在 .css() 中,因为当我运行它来提醒 x.top 它运行良好。
您的变量被视为字符串,将其放在引号外并使用 +:
添加到字符串中
$(document).ready(function(){
var x = $("article:last-child").offset();
$('#content').css('height' : x.top + 'px');
});
.css()
的语法不正确,应该是
$('#content').css('height', x.top + 'px');
Fiddle 例子:
如果您只是设置高度,则无需费心css
。像素是 height
函数的默认单位:
$(document).ready(function(){
var lastArticle = $("article:last-child");
$('#content').height(lastArticle.offset().top);
});
不要吹毛求疵,但我建议不要使用像 x
这样命名的变量,即使对于简单的事情也是如此——除非你 实际上是指 x
(例如,坐标空间)。代码本来就很难读懂,但好的名字可以让它变得更容易。
在这个博客上,我想编辑#content div 的高度并使其成为高度:x.top px(article:last-child),这样背景垂直重复。
http://manutdstream.tumblr.com/
我试过这样做:
$(document).ready(function(){
x=$("article:last-child").offset();
$('#content').css('height' : 'x.top px');
});
我认为问题出在 .css() 中,因为当我运行它来提醒 x.top 它运行良好。
您的变量被视为字符串,将其放在引号外并使用 +:
添加到字符串中 $(document).ready(function(){
var x = $("article:last-child").offset();
$('#content').css('height' : x.top + 'px');
});
.css()
的语法不正确,应该是
$('#content').css('height', x.top + 'px');
Fiddle 例子:
如果您只是设置高度,则无需费心css
。像素是 height
函数的默认单位:
$(document).ready(function(){
var lastArticle = $("article:last-child");
$('#content').height(lastArticle.offset().top);
});
不要吹毛求疵,但我建议不要使用像 x
这样命名的变量,即使对于简单的事情也是如此——除非你 实际上是指 x
(例如,坐标空间)。代码本来就很难读懂,但好的名字可以让它变得更容易。