首页 > 解决方案 > 使用 Jquery 将图像定位到 div 的顶部

问题描述

我有一个带有 id.css 的图像:

#imgP {
  max-width:300px ;
  max-height:300px ;
  position: relative;
  top:0 ;
  left: calc(1em + 1vw);
}

我尝试将顶部定位到他的父 id.css:

#parent {
  top:-60vh;
  background-image: url('../Image/infome.png');
  border-radius: 10px;
  background-size: cover;
  background-position: center;
  background-repeat: none; 
  padding: 10px; 
}

使用 jQuery

$(window).load(function() {
    y = $('#parent').offset().top;
    $("#imgP").css({top:-y});
});`

但是顶部设置为 px 数字并且不粘在 div 顶部位置。

标签: jquery

解决方案


推荐阅读