对角图像滑块在移动设备上不起作用

问题描述 投票:0回答:1

我有一个对角图像滑块,包括 2 个图像和 1 个滑块。滑块在网络上是可点击的(即我可以滑动它),但我无法与移动设备上的滑块进行交互。

在移动设备中,滑块根本不可点击,因此它不可滑动。

可能是什么问题?

这是代码:

<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>

我认为问题与 mousedown、mouseup、mousemove 等函数有关。我如何在移动设备上使用它们?

谢谢您的建议。

html css responsive-design slider responsive
1个回答
0
投票

我找到了解决方案。我找到了手机屏幕需要的功能:

mousedown
-->
touchstart

mouseup
mouseleave
-->
touchend

mousemove
-->
touchmove

所以我复制粘贴了当前的函数并简单地更改了上面的内容。

  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);
  });
© www.soinside.com 2019 - 2024. All rights reserved.