Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

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

CSS

              
                /* 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;
}
              
            
!

JS

              
                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

Console