【问题标题】:Mixing percentage and pixels within css method syntax在 css 方法语法中混合百分比和像素
【发布时间】:2015-05-04 12:25:59
【问题描述】:

我的目标是使绝对元素居中。像 calc(50% - 123px) 一样,但在 jQuery 的帮助下。它的正确语法是什么?

element.css({
    width: pageWidth,
    left: '50%' - pageWidth / 2
})

【问题讨论】:

  • 有什么特别的原因为什么你想要在 jQuery 中使用它?您可以仅使用 CSS 来实现此效果。使用:left:50%; transform:translate(-50%, 0);,动态更改宽度无关紧要,因为元素将始终居中

标签: jquery css methods syntax calc


【解决方案1】:

我敢打赌是这个

element.css({
    width: pageWidth,
    left: '50%',
    margin-left: - pageWidth / 2
})

【讨论】:

  • 应该是margin-left: - pageWidth / 2
猜你喜欢
  • 1970-01-01
  • 2011-04-05
  • 2017-07-14
  • 1970-01-01
  • 2013-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-13
相关资源
最近更新 更多