查看上次更改时值是增加还是减少

时间:2018-01-30 23:44:20

标签: javascript jquery html jquery-ui draggable

我上下拖动一个元素,我希望它根据属性的变化来做特定的事情。我需要知道它的顶级'属性自上次更改后增加或减少。怎么做?

header.draggable({
    axis: 'y',
    containment: [0, 0, 0, '800px'],
    stop: function () {
        if (header.css('top') > '200px' && header.css('top') != 'auto') {
            header.animate({
                top: '800px'
            }, 1000);
            $('.nav').addClass('nav--active');
            $('.nav__wrapper').addClass('nav__wrapper--active');
            $('.nav__item').addClass('nav__item--active');
            $('.header__wrapper').addClass('header__wrapper--active');
        } else {
            header.animate({
                top: '0px'
            }, 500);
            $('.nav').removeClass('nav--active');
            $('.nav__wrapper').removeClass('nav__wrapper--active');
            $('.nav__item').removeClass('nav__item--active');
            $('.header__wrapper').removeClass('header__wrapper--active');
        }
    },
});

我使用jQuery UI Draggable拖动标题,我希望它向上拖动,如果我向上拖动它类似于设置顶部= 800px如果我向下拖动它。

2 个答案:

答案 0 :(得分:0)

使用两个变量yStartyStopui.offset.top存储在daggable start:和可拖动stop:个事件



$("#dragHandle").draggable({
  axis: "y",
  containment: "parent",
  start: function(ev, ui) {
    this.yStart = ui.offset.top;
  },
  stop: function(ev, ui) {
    this.yStop = ui.offset.top;
    
    var px = this.yStop - this.yStart;
    if (px === 0) {
      console.log("Not moved");
    } else if (px > 0) {
      console.log("Moved Down by " + Math.abs(px) + "px");
    } else {
      console.log("Moved Up by " + Math.abs(px) + "px");
    }
  }
});

#dragWrapper {
  background: #ddd;
  position: relative;
  width: 30px;
  height: 100px;
}

#dragHandle {
  background: chocolate;
  position: absolute;
  top: 0;
  width: 30px;
  height: 30px;
}

<div id="dragWrapper">
  <div id="dragHandle"></div>
</div>

<script src="//code.jquery.com/jquery-3.1.0.js"></script>
<script src="//code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:-1)

例如,您可以向元素添加属性     <div number="0"> </div>,每次拖动它都会更改该数字<div number="2">,然后将其与最后一个数字进行比较,以查看是大还是小。 (你在改变前比较)