Javascript 是否可以将元素还原到它';s上一个位置,而不将该位置作为参数传递?

Javascript 是否可以将元素还原到它';s上一个位置,而不将该位置作为参数传递?,javascript,Javascript,以下函数采用选项参数,并根据起始值将元素设置为特定的像素量: options: { property: 'right', startValue: '-250px', amount: '250px' }, function (options) { const $el = $(this.el) $el.click(() => { const startValue = $el.parent().css(slide.property) const calcVa

以下函数采用
选项
参数,并根据
起始值
将元素设置为特定的
像素量

options: {
  property: 'right',
  startValue: '-250px',
  amount: '250px'
},

function (options) {
  const $el = $(this.el)
  $el.click(() => {
    const startValue = $el.parent().css(slide.property)
    const calcValue = parseInt(startValue, 10) + parseInt(slide.amount, 10)
    if (startValue === slide.startValue) {
      $el.parent().animate({ [slide.property]: calcValue }, 200)
    } else {
      $el.parent().animate({ [slide.property]: slide.startValue }, 200)
    }
  })
 }

但我想知道,是否可以在不向函数提供
startValue
的情况下实现同样的功能?(例如,如果
right
的初始值为
0
,则在第二次单击该元素时将其恢复为
0

可以利用这样一个事实,即调用
.animate()
时正在添加内联样式属性。因此,如果要将元素还原回CSS中指定的
值,可以调用
.removeAttr(“style”)
。要获得动画效果,您必须在CSS中包含一个transition属性

例如,查看此工作小提琴:



否则,据我所知,在调用
.animate()
之前,您需要获取并保存原始的
right
值。

恢复到以前的位置是什么意思?@snookieordie我在问题中澄清了这一点。
$("#slideButton").on("click", function() {
    $("div").animate({right:"-=50px"}, 200);
});

$("#restoreButton").on("click", function() {
    $("div").removeAttr("style");
});
div {
    height: 50px;
    width: 50px;
    top: 20px;
    right: 300px;
    background-color: red;
    position: fixed;
    -webkit-transition: right 0.2s;
    -moz-transition: right 0.2s;
    transition: right 0.2s;
}