3

I've built a prototype using a code snippet that was shared here in stackoverflow. I'm looking to add a delayed timer so that the auto scrolling happens only when there's no activity from the user. Also, how can I make this more touch friendly, instead of using hover?

Here's a fiddle of the prototype: http://jsfiddle.net/creativesplash/4xuy7fc6/1/

And here's the snippet:

// global level variables.
var isPaused = false;
var direction = 1;
var element = $('#box');

// interval for scroll.
setInterval(function () {
    if (!isPaused) {
        var pos = element.scrollLeft();
        var offset = 1 * direction;
        element.scrollLeft(pos + offset);

        // Change the scroll direction when hit an end.
        if ((element[0].scrollWidth - element.scrollLeft() == element.outerWidth()) || (element.scrollLeft() <= 0)) {
            direction *= -1;
        }
    }

}, 10);

$('#box').hover(

function () {
    isPaused = true;
}, function () {
    isPaused = false;
});
.boxes {width:75px; height:75px; display:inline-block; border:1px solid #ccc; text-align:center; padding-top:50px;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="box" class="timeline container" style="overflow-y:scroll; width:100%; white-space:nowrap">
  <div class="boxes">1</div>
  <div class="boxes">2</div>
  <div class="boxes">3</div>
  <div class="boxes">4</div>
  <div class="boxes">5</div>
  <div class="boxes">6</div>
  <div class="boxes">7</div>
  <div class="boxes">8</div>
  <div class="boxes">9</div>
  <div class="boxes">10</div>
  <div class="boxes">1</div>
  <div class="boxes">2</div>
  <div class="boxes">3</div>
  <div class="boxes">4</div>
  <div class="boxes">5</div>
  <div class="boxes">6</div>
  <div class="boxes">7</div>
  <div class="boxes">8</div>
  <div class="boxes">9</div>
  <div class="boxes">10</div>
  <div class="boxes">1</div>
  <div class="boxes">2</div>
  <div class="boxes">3</div>
  <div class="boxes">4</div>
  <div class="boxes">5</div>
  <div class="boxes">6</div>
  <div class="boxes">7</div>
  <div class="boxes">8</div>
  <div class="boxes">9</div>
  <div class="boxes">10</div>
  <div class="boxes">1</div>
  <div class="boxes">2</div>
  <div class="boxes">3</div>
  <div class="boxes">4</div>
  <div class="boxes">5</div>
  <div class="boxes">6</div>
  <div class="boxes">7</div>
  <div class="boxes">8</div>
  <div class="boxes">9</div>
  <div class="boxes">10</div>
</div>
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Vasanth
  • 49
  • 7

1 Answers1

1

If I understood correct, your code is working well, but you want a more efficient way for devices such as mobiles, that doesn't have hover?

What about touchstart event? something like this:

$('#box').on('touchstart', function(ev){
  if (isPaused){
    isPaused = false;
  }else{
    isPaused = true;
  }
});

I didn't test this, I don't know if it will work as you expect, but my logic here is:
The boxes are scrolling, then user touch #box and it stops, if he touch again, it starts scrolling again. This is just an idea.

EDIT

Here's a snippet of what we were talking in the comments
IDLE TIMER UNTIL SCROLL + HOVER AND TOUCH EVENT TO STOP SCROLL
open snippet and run to test

$(document).ready(function(){
  // global level variables.
  var isPaused = false;
  var direction = 1;
  var element = $('#box');
  var waitTimerUntilScroll = 4000;
  var idleTimer = setTimeout(setScrolling, waitTimerUntilScroll);
  var scroller;

  window.onmousemove = resetTimer;
  window.onmousedown = resetTimer; 
  window.ontouchstart = resetTimer; 
  window.onclick = resetTimer;     
  window.onkeypress = resetTimer;   
  window.addEventListener('scroll', resetTimer, true); 
  
  $('#box').on('touchstart', function(ev){
    if (isPaused){
      isPaused = false;
    }else{
      isPaused = true;
    }
 });
  
  $('#box').hover(
   function () {
     isPaused = true;
    }, 
    function () {
      isPaused = false;
    }
  );  
  
  function resetTimer() {
    clearTimeout(idleTimer);
    idleTimer = setTimeout(setScrolling, waitTimerUntilScroll);  // time is in milliseconds
  }
  
  // interval for scroll - called only when idle
  function setScrolling(){
    scroller = setInterval(function () {
      if (!isPaused) {
        clearTimeout(idleTimer);
        var pos = element.scrollLeft();
        var offset = 1 * direction;
        element.scrollLeft(pos + offset);

        // Change the scroll direction when hit an end.
        if ((element[0].scrollWidth - element.scrollLeft() == element.outerWidth()) || (element.scrollLeft() <= 0)) {
            direction *= -1;
          }
      }

    }, 10);
 }
});
.boxes {width:75px; height:75px; display:inline-block; border:1px solid #ccc; text-align:center; padding-top:50px;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="box" class="timeline container" style="overflow-y:scroll; width:100%; white-space:nowrap">
  <div class="boxes">1</div>
  <div class="boxes">2</div>
  <div class="boxes">3</div>
  <div class="boxes">4</div>
  <div class="boxes">5</div>
  <div class="boxes">6</div>
  <div class="boxes">7</div>
  <div class="boxes">8</div>
  <div class="boxes">9</div>
  <div class="boxes">10</div>
  <div class="boxes">1</div>
  <div class="boxes">2</div>
  <div class="boxes">3</div>
  <div class="boxes">4</div>
  <div class="boxes">5</div>
  <div class="boxes">6</div>
  <div class="boxes">7</div>
  <div class="boxes">8</div>
  <div class="boxes">9</div>
  <div class="boxes">10</div>
  <div class="boxes">1</div>
  <div class="boxes">2</div>
  <div class="boxes">3</div>
  <div class="boxes">4</div>
  <div class="boxes">5</div>
  <div class="boxes">6</div>
  <div class="boxes">7</div>
  <div class="boxes">8</div>
  <div class="boxes">9</div>
  <div class="boxes">10</div>
  <div class="boxes">1</div>
  <div class="boxes">2</div>
  <div class="boxes">3</div>
  <div class="boxes">4</div>
  <div class="boxes">5</div>
  <div class="boxes">6</div>
  <div class="boxes">7</div>
  <div class="boxes">8</div>
  <div class="boxes">9</div>
  <div class="boxes">10</div>
</div>
Calvin Nunes
  • 6,376
  • 4
  • 20
  • 48
  • Thanks! I'll certainly try that out. I'm also looking for a way to autoscroll only if there's no activity from the user for more than 20 seconds – Vasanth May 09 '18 at 16:39
  • about no acitivity from the user, check this: https://stackoverflow.com/questions/667555/detecting-idle-time-in-javascript-elegantly – Calvin Nunes May 09 '18 at 18:08
  • Thanks Calvin, I had seen this post, but due to my lack of js knowledge I am unable to integrate the two effectively! Can I request you to fork my jsfiddle and integrate the 2 pieces? – Vasanth May 09 '18 at 20:33
  • Hi Calvin, not sure what I'm doing wrong! Here's the forked fiddle with the no activity timer - http://jsfiddle.net/creativesplash/mmd2kzay/1/ Let me know what you think of it! – Vasanth May 09 '18 at 22:00
  • Please, to not spam this question that is already answered, create e new question here in the S.O, that way many other people can see your problem and try to help. Also, if my answer was useful to your original question, please, mark as accepted – Calvin Nunes May 11 '18 at 14:18
  • ok, but I suggest you to make some researches about javascript functions and scopes, because there's a lot of things created in wrong places that will never work... for example, you declare a `var t`, and don't use it inside the function where you declared it, you try to use it inside another function, where `t` doesn't exist. I will try to make some fixes – Calvin Nunes May 11 '18 at 18:05
  • Yes I agree, I am a novice and dont understand the basics and thats why I came here to ask the experts :) I copied the code from stackoverflow but I do not have enough points to add a comment there. -> https://stackoverflow.com/questions/32116928/pause-auto-scrolling-div – Vasanth May 11 '18 at 18:11
  • I'm looking for a solution that does the autoscroll only when the user is idle (no activity) which includes touch, scroll, hover for desktop is nice to have. – Vasanth May 11 '18 at 18:29
  • Thanks! This helps! Can you also suggest a way to pause the autoscroll upto 20 seconds after the user has done his activity? – Vasanth May 11 '18 at 18:35
  • for now, I think I did huge help, but I can't stay here coding for free. with the code I answered above, you can have a big start. If you are a novice, I suggest to not go for hard tasks/codes, learn first, then go for it. Search suggestions: 'Javascript variables scope', 'Javascript timers', 'JS oriented object programming ' – Calvin Nunes May 11 '18 at 18:37
  • You've been huge help! I cannot thank you enough for all the work! I'll accept this answer! – Vasanth May 11 '18 at 18:45