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

标签 javascript jquery html css

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

更新

有一张图片,其中有放置在坐标中的 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/

相关文章:

Javascript。备份和恢复对象

javascript - 单击按钮时的动态 DIV 元素

php - jQuery 表格行突出显示 onClick 和恢复 onChange 交替的 css 类

javascript - 如何缩短代码或将其存储在数据文件中

javascript - 如何部署 Angular/Node 应用程序

javascript - var 上的 "+"字符出现问题

javascript - jQuery - UI Resizable 调整所有子元素及其字体大小

javascript - 运行 javascript 函数后无法识别 Css 格式

javascript - JQuery,左位置错误

php - 导航下拉列表有时会消失