Search code examples
htmlcssresponsive-designsliderresponsive

Diagonal image slider is not working on mobile


I have a diagonal image slider, including 2 images and 1 slider. The slider is clickable on web (i.e., i can slide it), but I can't interact with the slider on mobile devices.

In mobile devices the slider is simply not clickable, therefore it is not slidable.

What could be the problem?

Here is the code:

<style>
 #slider{
        display: flex;
        align-items: center;
        justify-content: center;
    }
        
    .wrapper{
        width: 30vw;
        height: 20vw;
        position:relative;
        overflow:hidden;
        box-shadow: 0 10px 20px rgba(0,0,0,0.1), 0 6px 6px rgba(0,0,0,0.2);
    }
    
    .before, .after {
        width:100%;
        height:100%;
        background-repeat:no-repeat;
        background-color: white;
        background-size: cover;
        background-position: center;
        position: absolute;
        top:0;
        left:0;
        pointer-events:none;
        overflow: hidden;
    }
    
    .content-image{
        height:100%;
    }
    
    .after{
        transform: skewX(-30deg) translate(-35vw); 
    }
    
    .content-image.apres{
        transform: skewX(30deg) translate(35vw);
    }
    
    .scroller{
        width: 50px;
        height:50px;
        position: absolute;
        top:50%;
        transform:translateY(-50%);
        border-radius:50%;
        background-color: transparent;
        opacity:0.9;
        pointer-events:auto;
        cursor: move;
        box-sizing: border-box;
        border: 3px solid #fff;
    }
    
    .scroller:hover{
        opacity:1;
    }
    
    .scrolling{
        pointer-events:none;
        opacity:1;
    }

    .scroller__thumb{
        margin:0;
        box-sizing: border-box;
        width:100%;
        height:100%;
        padding:5px;
    }
    
    .scroller:before, .scroller:after{
        background: #fff;
        content:" ";
        display: block;
        width: 3px;
        height: 9999px;
        position: absolute;
        left: 50%;
        margin-left: -3.5px;
        z-index: 30;
        transition:0.1s;
    }
    .scroller:before{
        top:95%;
        left:28%;
        transform-origin:top;
        transform:rotate(30deg)
    }
    .scroller:after{
        bottom:95%;
        left:83%;
        transform-origin:bottom;
        transform:rotate(30deg)
    }
</style>

<main>
  <section id="slider">
    <div class="wrapper">
        <div class="before">
          <img class="content-image" src="https://i.imgur.com/PNBfFe2.png" draggable="false"/>
        </div>
        <div class="after">
          <img class="content-image apres" src="https://i.imgur.com/hxNAvu7.png" draggable="false"/>
        </div>
      <div class="scroller">
        <svg class="scroller__thumb" xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100"><polygon points="0 50 37 68 37 32 0 50" style="fill:#fff"/><polygon points="100 50 64 32 64 68 100 50" style="fill:#fff"/></svg>
      </div>
    </div>
  </section>
</main>

<script>
  let active = false;
  var scrollers = document.querySelectorAll('.scroller');

  scrollers.forEach(element => {
    element.addEventListener('mousedown',function scrolling(){
      active = true;
      element.classList.add('scrolling');
    });
  });

  document.body.addEventListener('mouseup',function(){
    active = false;
    document.querySelector('.scroller').classList.remove('scrolling');
  });

  document.body.addEventListener('mouseleave',function(){
    active = false;
    document.querySelector('.scroller').classList.remove('scrolling');
  });

  document.body.addEventListener('mousemove',function(e){
    if (!active) return;
    let x = e.pageX;
    x -= document.querySelector('.wrapper').getBoundingClientRect().left;
    scrollIt(x);
  });

  function scrollIt(x){
      let transform = Math.max(0,(Math.min(x,document.querySelector('.wrapper').offsetWidth)));
      document.querySelector('.after').style.width = 'calc(' + transform + 'px + 35vw)';
      document.querySelector('.scroller').style.left = transform-25+"px";
  }

  scrollIt(300);
</script>

I think the problem is related with the mousedown, mouseup, mousemove, etc. functions. How can I use them in mobile?

Thanks in advice.


Solution

  • I found what the solution is. I found the functions required in mobile screens:

    mousedown --> touchstart

    mouseup and mouseleave --> touchend

    mousemove --> touchmove

    So I copy-pasted the current functions and simply changed the things above.

      scrollers.forEach(element => {
        element.addEventListener('touchstart',function scrolling(){
          active = true;
          element.classList.add('scrolling');
        });
      });
    
      document.body.addEventListener('touchend',function(){
        active = false;
        document.querySelector('.scroller').classList.remove('scrolling');
      });
    
      document.body.addEventListener('touchmove',function(e){
        if (!active) return;
        let x = e.pageX;
        x -= document.querySelector('.wrapper').getBoundingClientRect().left;
        scrollIt(x);
      });