jQuery位置div在页面中间 [英] jQuery position div in middle of page
本文介绍了jQuery位置div在页面中间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我到目前为止
$(#a)。css('margin-top', '(document).height()/ 2 - (#a)。height()/ 2');
这是否正确?
解决方案
**不应该在引号中。此外,您需要使用 $()
术语。试试这个:
$(#a)。css('margin-top',$(document).height )/ 2 - $(#a)。height()/ 2);
甚至更好:
var $ a = $(#a);
$ a.css('margin-top',$(document).height()/ 2 - $ a.height()/ 2);
编辑:要清楚,您无法将其放入引号,因为它会尝试将margin-top属性字面设置为该字符串。这是不正确的。
Okay, so I have a div and I want to position it in the middle of a page. I've gotten so far
$("#a").css('margin-top', '(document).height()/2 - ("#a").height()/2');
Is this correct?
解决方案
**It shouldn't be in quotes. Also, you need to use the $()
terminology. Try this:
$("#a").css('margin-top', $(document).height()/2 - $("#a").height()/2);
Or even better:
var $a = $("#a");
$a.css('margin-top', $(document).height()/2 - $a.height()/2);
Edit: Just to be clear, you can't put it in quotes because it will try to set the margin-top property literally to that string. Which is incorrect.
这篇关于jQuery位置div在页面中间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文