gpt4 book ai didi

jQuery 定位 div 在页面中间

转载 作者:搜寻专家 更新时间:2023-10-31 02:25:04 24 4
gpt4 key购买 nike

好的,我有一个 div,我想将它放在页面的中间。我已经走了这么远

$("#a").css('margin-top', '(document).height()/2 - ("#a").height()/2');

这是正确的吗?

最佳答案

我建议你使用 .offset() .

描述:设置匹配元素集中每个元素的当前坐标,相对文档

$("#a").offset({
top: $(document).height()/2 - $("#a").height()/2,
left: $(document).width()/2 - $("#a").width()/2
})

关于jQuery 定位 div 在页面中间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4952533/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com