0
我想知道如何將brackground-position
從百分比轉換爲px。將background-position%轉換爲px
在某些情況下,我使用background-size: contain
來調整圖像大小。
的jsfiddle:
https://jsfiddle.net/tng3spqb/1/
EX:
$(document).ready(function() {
alert("RED: BACKGROUND CONTAIN: "+$(".box").css("backgroundPositionX")+" - "+$(".box").css("backgroundPositionY")+"\n"+"BLACK - NOT BACKGROUND CONTAIN: "+$(".all").css("backgroundPositionX")+" - "+$(".all").css("backgroundPositionY")+"\n"+"GREEN: BACKGROUND CONTAIN: "+$(".box2").css("backgroundPositionX")+" - "+$(".box2").css("backgroundPositionY")+"\n"+"PINK: NOT BACKGROUND CONTAIN: "+$(".all2").css("backgroundPositionX")+" - "+$(".all2").css("backgroundPositionY"))
})
.box {
width: 120px;
height: 200px;
background-color: red;
background-image: url('http://www.guiaturismo.net/blog/wp-content/gallery/fotos-ilha-grande/fotos-ilha-grande-1.jpg');
background-size: contain;
background-repeat: no-repeat;
background-position: center;
float:left;
}
.all {
width: 120px;
height: 200px;
background-color: black;
background-image: url('https://jsfiddle.net/img/logo.png');
background-repeat: no-repeat;
background-position: center;
float:left;
margin-left:25px;
}
.box2 {
width: 200px;
height: 120px;
background-color: green;
background-image: url('http://www.guiaturismo.net/blog/wp-content/gallery/fotos-ilha-grande/fotos-ilha-grande-1.jpg');
background-size: contain;
background-repeat: no-repeat;
background-position: center;
float:left;
margin-left:25px;
}
.all2 {
width: 200px;
height: 120px;
background-color: pink;
background-image: url('https://jsfiddle.net/img/logo.png');
background-repeat: no-repeat;
background-position: center;
float:left;
margin-left:25px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="box">
</div>
<div class="all">
</div>
<div class="box2">
</div>
<div class="all2">
</div>
親愛的,因爲程序具有不同大小和不同大小的圖像div的,我不能在PX設置。我可以,但必須執行包含圖像大小和div大小的計算。我找到了一個替代方案,它是我想要完成的:$(「。result」)。css({'backgroundPositionX':'calc('+ $(「。result」)。css('backgroundPositionX')) +' - 1px)'}); '。 ATT – abcd