gpt4 book ai didi

jquery - 缩放 div 并相对移动子元素

转载 作者:行者123 更新时间:2023-11-28 07:31:48 26 4
gpt4 key购买 nike

我需要放大或缩小背景图像,并根据比例保持其中的子 div

当我向上或向下缩放父 div 时,绿色方 block 应保持其位置(与图像最初对齐)

绿色的 div 不应改变它们的大小。

如果您需要更多信息,请告诉我

JS fiddle 在这里 http://jsfiddle.net/87cmgp8a/

HTML

<div id="container">

<div id="imageContainer">

<div id="square1" class="square"></div>
<div id="square2" class="square"></div>
<div id="square3" class="square"></div>

</div>
</div>
<br><br>
<button id="scaleup">Scale Up </button>
<button id="scaledown">Scale Down </button>

CSS

#container {
overflow: scroll;
height:350px;
width:350px;
}

#imageContainer {
background-image: url("http://www.portangelesschools.org/students/images/clip_image015.jpg");
background-repeat: no-repeat;
background-size: cover;
position: relative;
height:400px;
width: 400px;
}

.square {
border: 1px solid;
background-color: green;
width: 35px;
height: 35px;
position: absolute;
}

#square1{ top: 8px; left: 87px; }
#square2{ top: 88px; left: 87px; }
#square3{ top: 165px; left: 87px; }

JS

var $imageContainer = $("#imageContainer");
var scaleFactor = 50;

$("button").click(function(){

var id = $(this).attr("id");
if( id === "scaleup" ) {
$imageContainer.css({
height: $imageContainer.height() + scaleFactor,
width: $imageContainer.width() + scaleFactor
});
}
else {
$imageContainer.css({
height: $imageContainer.height() - scaleFactor,
width: $imageContainer.width() - scaleFactor
});
}

});

最佳答案

尝试设置 CSS 变换而不是高度和宽度:

transform: scale(x, y);

关于jquery - 缩放 div 并相对移动子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31461536/

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