jquery - 用 jquery 改变几个 div css onload (this)

标签 jquery css

当页面加载时,我尝试更改每个 .partition div 的顶部边距。 我曾尝试使用 $(this) 但找不到好的解决方案:

  $(document).ready(function() {
      $('.partition').css({
      'marginTop':  - ($(this).height() / 2)
      });
  });

有什么想法吗? 非常感谢!

最佳答案

无需使用.each()。您可以直接从函数返回值:

$('.partition').css({
    'marginTop': function () {
        return -($(this).height() / 2)
    }
});

关于jquery - 用 jquery 改变几个 div css onload (this),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25511108/

相关文章:

javascript - 如何为两个类添加相同的功能?

javascript - 动画在两个 div 之间移动元素

javascript - JS 变量 onclick 增量

html - float :left display border but not center of the page?

html - CSS - 简单的文本纹理

javascript - 根据页眉和页脚高度改变高度的动态内容

css - 圆边,不是圆 Angular

jquery - 延迟启用和禁用 fancybox 关闭按钮

javascript - jQuery Tabs - 防止 anchor 在单击时将页面滚动到顶部

css - ngx-gallery 不在 Angular 中的图像上显示左右箭头