Scrolling issue on .animate () and .prop ()?

I have two divs with one class. If I scroll one div, the other scroll divs will be 0. I can easily achieve this with the .prop () property. But when I use .animate () this event happens once and then stops working (commented out the code in my example snippet). What I want is a scroll when it comes to zero, if animate when the scroll comes to 0 with animation, for example displaying it with .animate ().

Note. The divs classes will be the same and there may be more divs as well.

Here is the code I tried, please let me know where I am going wrong.

$(document).ready(function() {

  $('.swipe_div').scroll(function() {


    // $(this).siblings(".swipe_div").animate({scrollLeft: 0},100);
    $(this).siblings(".swipe_div").prop({
      scrollLeft: 0
    });

  });
});
      

body,
html {
  width: 100%;
  height: 100%;
  background-color: green;
  padding: 0;
  margin: 0;
}

.swipe_div {
  display: block;
  float: left;
  width: 100%;
  height: 100px;
  overflow-x: scroll;
  background-color: white;
}

.content,
.operation,
.swipe_container {
  display: block;
  float: left;
  height: 100%;
}

.swipe_container {
  width: 150%;
}

.content {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  flex-direction: row;
  text-align: right;
  font-size: 30pt;
  width: 67%;
  background-color: grey;
}

.operation {
  width: 33%;
  background-color: red;
}
      

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="swipe_div">
  <div class="swipe_container">
    <div class="content">
      &#x3e;
    </div>
    <div class="operation">

    </div>
  </div>

</div>

<div class="swipe_div">
  <div class="swipe_container">
    <div class="content">
      &#x3e;
    </div>
    <div class="operation">

    </div>
  </div>

</div>
      

Run codeHide result


+3


source to share


1 answer


When you animate scrollLeft

, you are activating scroll()

for the sibling that is trying to animate the scrolling in the div you are actively scrolling. Therefore, you need to mark when you start scrolling and throttle()

all subsequent calls on scroll()

until you scroll the scroll.

trailing:true

calls this one more time after it hasn't called for throttle_interval ( 250

in this example), turning the marker scrolling

by false

:

$(document).ready(function() {
  var scrolling;
  $('.swipe_div').scroll(_.throttle(function() {
    if (!scrolling) {
      scrolling = true;
      $(this).siblings(".swipe_div").animate({scrollLeft: 0},150);
    } else {
      scrolling = false;
    }
  }, 250, {leading:true,trailing:true}));
});
      

body,
html {
  width: 100%;
  height: 100%;
  background-color: green;
  padding: 0;
  margin: 0;
}

.swipe_div {
  display: block;
  float: left;
  width: 100%;
  height: 100px;
  overflow-x: scroll;
  background-color: white;
}

.content,
.operation,
.swipe_container {
  display: block;
  float: left;
  height: 100%;
}

.swipe_container {
  width: 150%;
}

.content {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  flex-direction: row;
  text-align: right;
  font-size: 30pt;
  width: 67%;
  background-color: grey;
}

.operation {
  width: 33%;
  background-color: red;
}
      

<script src="https://cdn.jsdelivr.net/lodash/4.17.4/lodash.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="swipe_div">
  <div class="swipe_container">
    <div class="content">
      &#x3e;
    </div>
    <div class="operation">

    </div>
  </div>

</div>

<div class="swipe_div">
  <div class="swipe_container">
    <div class="content">
      &#x3e;
    </div>
    <div class="operation">

    </div>
  </div>

</div>
      

Run codeHide result




I tested it a bit and actually found a slight glitch / limitation: the throttle interval should be less than the animation time. If it is not, the animation will exceed the throttle spacing and trigger, in turn, the close animation for the original scrolled element.

But this is the web (nothing is possible): if and when your animation needs to be longer than the throttle interval, you will need to mark the start element with a class that excludes it from animation. The class will be removed using a timeout at the end of the animation equal to the throttle interval:

$(document).ready(function() {
  var scrolling;
  $('.swipe_div').scroll(_.throttle(function() {
    if (!scrolling) {
      scrolling = true;
      $(this).addClass('original');
      $(this).siblings(".swipe_div:not(.original)").animate(
        {scrollLeft:0},
        250,
        function(){
          setTimeout(function() {
            $('.swipe_div').removeClass('original')
          }, 150)
        }
      );
    } else {
      scrolling = false;
    }
  }, 150, {leading:true,trailing:true}));
});

      

+7


source







All Articles