gpt4 book ai didi

javascript - 如何缩放以使容器也增长并占用空间

转载 作者:IT王子 更新时间:2023-10-29 03:13:49 29 4
gpt4 key购买 nike

所以我有一个容器,我想放大和缩小(放大和缩小)但也有它的扩展/收缩形式来占用空间而不是仅仅重叠其他东西。

更新

有一张图片,其中有放置在坐标中的 absolute div,在调整大小时和缩小大小时它们必须保持它们的相对位置(因此我使用比例尺)。

var b = document.getElementById("outer");
var scale = 1;

function increase() {
scale += 0.1
b.style.transform = `scale(${scale})`;
}

function decrease() {
scale -= 0.1
b.style.transform = `scale(${scale})`;
}
#outer {
overflow-x: auto position: relative;
transform-origin: left top;
}

.pointer {
width: 20px;
height: 20px;
background-color: orange;
position: absolute;
}

#a1 {
top: 50px;
left: 150px;
}

#a2 {
top: 150px;
left: 50px;
}

#a3 {
top: 250px;
left: 550px;
}
<div>
<button onclick="increase()">Increase</button>
<button onclick="decrease()">Decrease</button>
</div>
<div id=outer>
<img src="http://via.placeholder.com/600x350" />
<div id="a1" class='pointer'>
</div>
<div id="a2" class='pointer'>
</div>
<div id="a3" class='pointer'>
</div>
</div>
<div>
please don't cover me
</div>

宁愿没有第三方库(除了 jQuery),但可以考虑。

最佳答案

CSS3 scale 过渡效果that。不幸的是,缩放与其他元素重叠,因为它通过创建一个新的堆栈上下文(本质上是将其所有内容放置相对 到容器) - 请参阅相关文档说明:

If the property has a value different than none, a stacking context will be created.

Source: MDN

请参阅下面的演示,通过暴力缩放所有元素:

var b, scale = 1, offset, pointers;

window.onload = function() {
b = document.getElementById("outer");
offset = b.getBoundingClientRect();
pointers = Array.prototype.map.call(b.querySelectorAll('.pointer'), function(e) {
return {
el: e,
offset: e.getBoundingClientRect()
}
});
}

function increase() {
scale += 0.1;
scaleIt();
}

function decrease() {
scale -= 0.1;
scaleIt();
}

function scaleIt() {
b.style.width = scale * offset.width + 'px';
b.style.height = scale * offset.height + 'px';
Array.prototype.forEach.call(pointers, function(e) {
e.el.style.width = scale * e.offset.width + 'px';
e.el.style.height = scale * e.offset.height + 'px';
e.el.style.top = scale * e.offset.top + 'px';
e.el.style.left = scale * e.offset.left + 'px';
});
}
#outer {
/*overflow-x: auto;*/
position: relative;
transform-origin: left top;
}
.pointer {
width: 20px;
height: 20px;
background-color: orange;
position: absolute;
}
#outer > img {
height: 100%;
}

#a1 {
top: 50px;
left: 150px;
}
#a2 {
top: 150px;
left: 50px;
}
#a3 {
top: 250px;
left: 550px;
}
<div>
<button onclick="increase()">Increase</button>
<button onclick="decrease()">Decrease</button>
</div>
<div id=outer>
<img src="http://via.placeholder.com/600x350" />
<div id="a1" class='pointer'>
</div>
<div id="a2" class='pointer'>
</div>
<div id="a3" class='pointer'>
</div>
</div>
<div>
please don't cover me
</div>

关于javascript - 如何缩放以使容器也增长并占用空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46154992/

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