css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <p>Each swipe slider can define it's own snap to allow it to position cleanly wherever you might need it.</p>
<div class="swipe-slider">
  <div class="swipe-slider-front">
    drag me to the left
  </div>
  <div class="swipe-slider-back">
    <button>Add</button>
    <button>Rem</button>
  </div>
</div>


<div class="swipe-slider">
  <div class="swipe-slider-front">
    drag me to the left
  </div>
  <div class="swipe-slider-back">
    <button style="width:100px">Delete</button>
  </div>
</div>

<p>There is also an onupdate method that can be passed into a swipe slider which can allow it to do something unique to the slider, for instance here the back is offset and dragged into position with the front.</p>
<div class="swipe-slider">
  <div class="swipe-slider-front">
    drag at least 30% to the left
  </div>
  <div class="swipe-slider-back" style="left:100%;font-size:2em;font-family:arial">
    <button style="flex:1;color:red;">&hearts;</button>
    <button style="flex:1;color:black">&clubs;</button>
    <button style="flex:1;color:red">&diams;</button>
    <button style="flex:1;color:black;">&spades;</button>

  </div>
</div>
            
          
!
            
              /* Recommended Styles */
.swipe-slider {
  position:relative;
  overflow:hidden;
}
.swipe-slider-back {
  position:relative;
  z-index:1;
  width:100%;
  height:100%;
}
.swipe-slider-front {
  position:absolute;
  top:0;
  right:0;
  width:200%;
  height:100%;
  z-index:2;
}


* {
  box-sizing:border-box;
}
/* Not required styles */
.swipe-slider {
  width:100%;
  height:3em;
  
  margin:1em 0;
  
  background-color:#888;
}
.swipe-slider-front {
  border:1px solid darkred;
  padding-right:1em;
  background-color:red;
  display:flex;
  align-items:center;
  justify-content:flex-end;
  user-select:none;
}
.swipe-slider-back {
  display:flex;
  justify-content:flex-end;
}
.swipe-slider-back button {
  background-color:#eee;
  border:1px solid #ccc;
  text-align:center;
  font-size:inherit;
  font-family:inherit;
  width:80px;
}
.swipe-slider-back button:focus {
  outline:0;
  border-color:blue;
}
            
          
!
            
              const getEventXY = (e,o) => {
  const p = e.type.substring(0,5)!="touch"?e:!e.touches.length?e.changedTouches[0]:e.touches[0];
  const rect = (o||p.target).getBoundingClientRect();
  return { x:p.pageX-rect.left, y:p.pageY-rect.top };
}

class SwipeSlider {
  constructor(o) {
    // this.snap = {
    //   [0,-1,0.5],
    //   [0,0,0]
    // }
    this.pos = {start:{},offset:{},x:0};    
    
    Object.assign(this,o);
    
    if(!this.swiperElement) throw "issue: No valid swiperElement provided";
    this.backElement = this.swiperElement.querySelectorAll('.swipe-slider-back');
    this.frontElement = this.swiperElement.querySelector('.swipe-slider-front');
    

    this.swiperElement.addEventListener('mousedown',e=>this.swipeStart(e));
    this.swiperElement.addEventListener('mouseup',e=>this.swipeEnd(e));
    this.swiperElement.addEventListener('mouseleave',e=>this.swipeEnd(e));
    this.swiperElement.addEventListener('mousemove',e=>this.swipeMove(e));
    this.swiperElement.addEventListener('touchstart',e=>this.swipeStart(e));
    this.swiperElement.addEventListener('touchend',e=>this.swipeEnd(e));
    this.swiperElement.addEventListener('touchmove',e=>this.swipeMove(e));
  }
  
  getSnap() {
    return this.snap!='100%'?this.snap:this.width();
  }
  canSnapToEnd() {
    const disttarget = this.getSnap()*(+this.snapPercentage||0.5);
    const dist = this.getDistance();
    return (dist < -disttarget && !this.pos.open)?true:
      (dist > disttarget && this.pos.open)?false:
      this.pos.open;
  }
  getDistance() {
    return this.pos.offset.x + this.pos.x - this.pos.start.x;
  }
  
  width() { return this.swiperElement.getBoundingClientRect().width; }
  
  
  swipeStart(e) {
    if(this.swiping) return;
    this.swiping = true;
    Object.assign(this.pos.start,getEventXY(e,this.swiperElement));
    this.pos.offset.x = this.pos.start.x - this.pos.x;
    this.pos.open = this.pos.x!=0;
  }
  swipeEnd(e) {
    if(!this.swiping) return;
    this.swiping = false;
    if(this.canSnapToEnd()) this.pos.x = -this.getSnap();
    else this.pos.x = 0;
    this.setPosition();
  }
  swipeMove(e) {
    if(!this.swiping) return;
    e.preventDefault();
    let pos = getEventXY(e,this.swiperElement);
    this.pos.x = pos.x-this.pos.offset.x;
    this.setPosition();
  }
  setPosition(){
    this.frontElement.style.transform = `translateX(${this.pos.x}px)`;
    if(this.onupdate) this.onupdate(this);
  }
}










/* Examples */

new SwipeSlider({
  swiperElement:document.querySelectorAll('.swipe-slider')[0],
  snap:160
});

new SwipeSlider({
  swiperElement:document.querySelectorAll('.swipe-slider')[1],
  snap:80
});

new SwipeSlider({
  swiperElement:document.querySelectorAll('.swipe-slider')[2],
  snap:'100%',
  snapPercentage:0.3,
  onupdate:s=>{
    if(s.pos.x<-s.width()) return;
    Object.assign(s.backElement[0].style,{
      transform:`translateX(${s.pos.x}px)`
    });
  }
});

// new SwipeSlider({
//   swiperElement:document.querySelectorAll('.swipe-slider')[3],
//   snap:'100%',
//   snapPercentage:0.3,
//   onupdate:s=>{
//     // if(s.pos.x<-s.width()) return;
//     // Object.assign(s.backElement.style,{
//     //   transform:`translateX(${s.pos.x}px)`
//     // });
//   }
// });
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console