从某个特定点开始CSS缩放,何时该特定点可能会随之改变?

时间:2018-07-11 08:20:06

标签: javascript css css3 css-transitions css-transforms

我正在尝试构建一个应用程序,在该应用程序中您可以使用鼠标滚轮将一点放大到一个点,然后再在另一点进一步放大。

换句话说,缩放的“原点”可以沿路径改变。

看看这个例子:https://codesandbox.io/s/4w4m1k5zlx

var phase = 1;

var box1 = document.getElementById("box1");
var box2 = document.getElementById("box2");

box1.style.transformOrigin = "0 0";
box2.style.transformOrigin = "0 0";

var width = 100;
var height = 100;

function transform(originX, originY, translateX, translateY, scale) {
  transformElement(1, box1, originX, originY, translateX, translateY, scale);
  transformElement(2, box2, originX, originY, translateX, translateY, scale);
}

function transformElement(
  method,
  element,
  originX,
  originY,
  translateX,
  translateY,
  scale
) {
  element.style.transition = "transform 1s linear";

  if (method === 1) {
    element.style.transform = `translate(${originX}px, ${originY}px) translate(${translateX}px, ${translateY}px) scale(${scale}) translate(-${originX}px, -${originY}px)`;
  } else if (method === 2) {
    element.style.transformOrigin = `${originX}px ${originY}px`;
    element.style.transform = `translate(${translateX}px, ${translateY}px) scale(${scale})`;
  }

  var pointElement = document.createElement("div");
  pointElement.classList.add("point");
  pointElement.style.transform = `translate(${originX}px, ${originY -
    2 * scale}px)`;

  element.appendChild(pointElement);
}

function reset() {
  resetElement(box1);
  resetElement(box2);
}

function resetElement(element) {
  while (element.children.length > 0) {
    element.removeChild(element.children[0]);
  }

  element.style.transform = "";
  element.style.transition = "";

  void element.clientWidth;
}

function phase1() {
  transform(width * 0.75, height / 2, 0, 0, 1.5);
}

function phase2() {
  transform(width * 0.25, height / 2, 0, 0, 2);
}

function phase3() {
  transform(width / 2, height, 0, 0, 2.5);
}

function phase4() {
  transform(width / 2, 0, 0, 0, 3);
}

const phases = [reset, phase1, phase2, phase3, phase4];

setInterval(() => phases[phase++ % phases.length](), 1500);
* {
  box-sizing: border-box;
}

body {
  background-color: black;
}

.container {
  position: relative;
  margin: 60px;
  background-color: lightgray;
  width: 200px;
  height: 200px;
}

.point {
  width: 2px;
  height: 2px;
  background-color: white;
}

.box {
  position: absolute;
  top: 25%;
  left: 25%;
  transform-origin: 0 0;
  background-color: teal;
  opacity: 0.8;
  width: 100px;
  height: 100px;
}

.outline {
  background-color: transparent;
  border: 1px solid black;
}
<div class="container">
  <div class="box outline">
  </div>
  <div id="box1" class="box"></div>
</div>
<div class="container">
  <div class="box outline">
  </div>
  <div id="box2" class="box"></div>
</div>

示例中的顶部框:在这里,我尝试使用transform-origin模拟transform,以便能够转换一个属性。但是,动画不是“均匀”的-尤其是在第一个比例(它将放大和缩小一点,很难解释,但希望您能看到它)。

该示例的底部框:同时更改transform-origintransform的过程非常容易,因为transform具有过渡并且transform-origin没有。

最上面的示例是最理想的,但是由于缩放不平滑,因此看起来仍然不好。如何在不进行转换的情况下缩放不同阶段的点?

1 个答案:

答案 0 :(得分:3)

我认为您能做的最好就是依靠选项2 。您可以对transform-origin应用过渡,并为transform添加延迟,以便先更改原点,然后进行变换:

transform 1s linear 0.5s, transform-origin 0.5s

完整代码:

var phase = 1;

var box2 = document.getElementById("box2");

box2.style.transformOrigin = "0 0";

var width = 100;
var height = 100;

function transform(originX, originY, translateX, translateY, scale) {
  transformElement(2, box2, originX, originY, translateX, translateY, scale);
}

function transformElement(
  method,
  element,
  originX,
  originY,
  translateX,
  translateY,
  scale
) {
  element.style.transition = "transform 1s linear 0.5s,transform-origin 0.5s";

  element.style.transformOrigin = `${originX}px ${originY}px`;
  element.style.transform = `translate(${translateX}px, ${translateY}px) scale(${scale})`;

  var pointElement = document.createElement("div");
  pointElement.classList.add("point");
  pointElement.style.transform = `translate(${originX}px, ${originY -
    2 * scale}px)`;

  element.appendChild(pointElement);
}

function reset() {
  resetElement(box2);
}

function resetElement(element) {
  while (element.children.length > 0) {
    element.removeChild(element.children[0]);
  }

  element.style.transform = "";
  element.style.transition = "";

  void element.clientWidth;
}

function phase1() {
  transform(width * 0.75, height / 2, 0, 0, 1.5);
}

function phase2() {
  transform(width * 0.25, height / 2, 0, 0, 2);
}

function phase3() {
  transform(width / 2, height, 0, 0, 2.5);
}

function phase4() {
  transform(width / 2, 0, 0, 0, 3);
}

const phases = [reset, phase1, phase2, phase3, phase4];

setInterval(() => phases[phase++ % phases.length](), 1500);
* {
  box-sizing: border-box;
}

body {
  background-color: black;
}

.container {
  position: relative;
  margin: 50px;
  background-color: lightgray;
  width: 200px;
  height: 200px;
}

.point {
  width: 2px;
  height: 2px;
  background-color: white;
}

.box {
  position: absolute;
  top: 25%;
  left: 25%;
  transform-origin: 0 0;
  background-color: teal;
  opacity: 0.8;
  width: 100px;
  height: 100px;
}

.outline {
  background-color: transparent;
  border: 1px solid black;
}
<div class="container">
  <div class="box outline">
  </div>
  <div id="box2" class="box"></div>
</div>

更新

考虑到选项1 ,放大/缩小效果为(我认为),原因是您使用的是两种翻译方式(模拟{{1 }})围绕着您要执行的主要转换。因此效果类似于:移至A点,缩放元素,移至B点。

这是一个简单的示例,用于说明这种弹跳效果的问题。我们可以清楚地看到从右到左的轻微移动。

transform-origin
.box {
  width:100px;
  height:100px;
  margin-left:200px;
  background:red;
  transition:2s all;
  transform-origin:0 0;
}

body:hover .box{
  transform:translate(100px,0) scale(2) translate(-100px,0);
}

body {
 margin:0;
 background:repeating-linear-gradient(to right,transparent 0px,transparent 98px,#000 98px,#000 100px)
}

我们的目标是避免这种情况,并在进行缩放时使元素直接到达其最终位置。一个想法是将转换分为两部分。诀窍是应用带有刻度的一个翻译,然后再应用其他翻译。

这里是一个例子:

<div class="box">

</div>
document.querySelector('body').addEventListener('mouseover',function(){
  document.querySelector('.box').style.transform="scale(2) translate(-100px,0)";
  setTimeout(function(){
      document.querySelector('.box').style.transform="translate(100px,0) scale(2) translate(-100px,0)";
  },500);
})
document.querySelector('body').addEventListener('mouseleave',function(){
  document.querySelector('.box').style.transform="none";
})
.box {
  width:100px;
  height:100px;
  margin-left:200px;
  background:red;
  transition:1s all linear .5s;
  transform-origin:0 0;
}

body {
 margin:0;
 background:repeating-linear-gradient(to right,transparent 0px,transparent 98px,#000 98px,#000 100px)
}

如我们所见,我们不再具有弹跳效果。红色的<div class="box"> </div>不再是从右边到左边,而是在左边。这个想法有些疯狂,难以解释,但诀窍是使用延迟。

在悬停时,我们将转换添加一次平移和缩放,然后由于延迟,div将不会移动。延迟结束后,我们立即添加其他转换来更改转换。这将使div仅考虑我们在两种翻译中都模拟过的新来源来进行缩放。


这是完整的代码:

div
var phase = 1;

var box1 = document.getElementById("box1");
var box2 = document.getElementById("box2");


var width = 100;
var height = 100;

function transform(originX, originY,  scale) {
  transformElement(1, box1, originX, originY, scale);
  transformElement(2, box2, originX, originY, scale);
}

function transformElement(
  method,
  element,
  originX,
  originY,
  scale
) {

  if (method === 1) {
    element.style.transform =`scale(${scale}) translate(-${originX}px, -${originY}px)`;
    setTimeout(function(){
				element.style.transform = `translate(${originX}px, ${originY}px) scale(${scale}) translate(-${originX}px, -${originY}px)`;
    },200,element,scale,originX,originY)
    
    
  } else if (method === 2) {
    element.style.transformOrigin = `${originX}px ${originY}px`;
    element.style.transform = ` scale(${scale})`;
  }

  var pointElement = document.createElement("div");
  pointElement.classList.add("point");
  pointElement.style.transform = `translate(${originX}px, ${originY -
    2 * scale}px)`;

  element.appendChild(pointElement);
}

function reset() {
  resetElement(box1);
  resetElement(box2);
}

function resetElement(element) {
  while (element.children.length > 0) {
    element.removeChild(element.children[0]);
  }

  element.style.transform = "";
  element.style.transition = "";

  void element.clientWidth;
}

function phase1() {
  transform(width * 0.75, height / 2, 1.5);
}

function phase2() {
  transform(width * 0.25, height / 2,  2);
}

function phase3() {
  transform(width / 2, height,  2.5);
}

function phase4() {
  transform(width / 2, 0,  3);
}

const phases = [reset, phase1, phase2, phase3, phase4];

setInterval(() => phases[phase++ % phases.length](), 1400);
* {
  box-sizing: border-box;
}

.container {
  position: relative;
  display:inline-block;
  margin: 50px;
  background-color: lightgray;
  width: 200px;
  height: 200px;
}

.point {
  width: 2px;
  height: 2px;
  background-color: white;
}

.box {
  position: absolute;
  top: 25%;
  left: 25%;
  transform-origin: 0 0;
  background-color: teal;
  opacity: 0.8;
  width: 100px;
  height: 100px;
  transition:transform 1s linear 0.2s, transform-origin 0.2s;
}

.outline {
  background-color: transparent;
  border: 1px solid black;
}

我简化了JS,并进行了第一次尝试,即首先更改<div class="container"> <div class="box outline"> </div> <div id="box1" class="box"></div> </div> <div class="container"> <div class="box outline"> </div> <div id="box2" class="box"></div> </div>以进行比较。