Pen Settings

HTML

CSS

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

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

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.

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

              
                <div id="root">
</div>
              
            
!

CSS

              
                #root {
  display: flex;
  justify-content: center;
  align-items: center;
}
.card {
  margin: 0;
}
.Card {
  width: 300px;
  box-shadow: 0 0 9px 1px rgba(0, 0, 0, 0.3);
  cursor: pointer;
}
.card:active {
  box-shadow: 0 0 15px 3px rgba(0, 0, 0, 0.3);
}

.card-image:before {
  content: ' ';
  position: absolute;
  top: 0;
  left: 0;
  z-index: 555;
  width: 100%;
  height: 100%;
  display: block;
}

.cardWrapper {
  position: relative;
  z-index: 999999;
}

.backCard {
  position: absolute;
  
  .glower {
    width: 100%;
    height: 100%;
    background-color: white;
    opacity: 1;
    position: absolute;
    z-index: 200;
  }
}

img {
  height: 200px;
}
.SwipableCards {
  width: 500px;
  height: 600px;
  background-color: #ccc;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  
  * {
    -webkit-touch-callout: none; /* iOS Safari */
    -webkit-user-select: none; /* Safari */
    -khtml-user-select: none; /* Konqueror HTML */
    -moz-user-select: none; /* Firefox */
    -ms-user-select: none; /* Internet Explorer/Edge */
    user-select: none; /* Non-prefixed version, currently
                                  supported by Chrome and Opera */
  }
}

.result {
  position: absolute;
  left: calc(50% - 25px);
  top: calc(50% - 25px);
  font-size: 50px;
  opacity: 0;
  z-index: 9999;
  background: yellow;
  border-radius: 50%;
  text-align: center;
}

.result.left:before {
  content: '⬅';
  display: block;
  width: 50px;
  height: 50px;
  line-height: 50px;
}
.result.right:before {
  content: '➡';
  display: block;
  width: 50px;
  height: 50px;
  line-height: 50px;
}

.result.top:before {
  content: '⬆';
  display: block;
  width: 50px;
  height: 50px;
  line-height: 50px;
}
.result.bottom:before {
  content: '⬇';
  display: block;
  width: 50px;
  height: 50px;
  line-height: 50px;
}

.result.left.top:before {
  content: '⬉';
  display: block;
  width: 50px;
  height: 50px;
  line-height: 50px;
}
.result.left.bottom:before {
  content: '⬋';
  display: block;
  width: 50px;
  height: 50px;
  line-height: 50px;
}

.result.right.top:before {
  content: '⬈';
  display: block;
  width: 50px;
  height: 50px;
  line-height: 50px;
}
.result.right.bottom:before {
  content: '⬊';
  display: block;
  width: 50px;
  height: 50px;
  line-height: 50px;
}
              
            
!

JS

              
                const SwipeModes = {
  LeftRight: 0,
  TopBottom: 1,
  Edges: 2,
  EdgesAndCorners: 3
};

class App extends React.Component {
  render() {
    return (
      <div className="app">
        <SwipeableCards
          swipeMode={SwipeModes.EdgesAndCorners}
          onActionCompleted={(type, c) => {
            console.log(type);
          }}
        >
          <Card img="https://www.femalefirst.co.uk/image-library/land/1000/s/stocksnapnzkns1a1ow.jpg" />
          <Card img="http://materializecss.com/images/sample-1.jpg" />          
          <Card img="http://www.gandex.ru/upl/oboi/gandex02.jpg" />
          <Card img="http://www.hdwallpaperspulse.com/wp-content/uploads/2017/07/07/guitar-hd-wallpaper-for-desktop.jpg" />
          <Card img="http://cdn.wonderfulengineering.com/wp-content/uploads/2016/01/Desktop-Wallpaper-3.jpg" />
          <Card img="https://lh3.ggpht.com/dpVANeoetCnu6WZIMJb9SrfR3XQSwLfL4AT3CmzuHv_zQ9hw6fWgAlkm91HqedJ3VG8=h900" />
          <Card img="http://www.hdwallpapery.com/static/images/Beautiful-Bridge-Nature-Wallpaper-For-Desktop.jpg" />
          <Card img="http://www.wallpaperspots.com/wp-content/uploads/2013/07/Music-Desktop-Backgrounds-28-1024x768.jpg" />
        </SwipeableCards>
      </div>
    );
  }
}

class SwipeableCards extends React.Component {
  constructor(props) {
    super(props);
  }
  cardMouseDown(e) {
    this.cardPressed = true;

    this.shiftX = e.pageX;
    this.shiftY = e.pageY;
  }

  cardMouseUp(e) {
    this.cardPressed = false;
    this.handleMouseUp(e);
  }

  cardMouseMove(e) {
    if (!this.cardPressed) {
      return;
    }
        
    const left = e.pageX - this.shiftX;
    const top = e.pageY - this.shiftY;
    
    this.setState({
      top: top,
      left: left,
    });

    const minOpacity = Math.min(
      (this.acceptableOffset - Math.abs(left)) / this.acceptableOffset,
      (this.acceptableOffset - Math.abs(top)) / this.acceptableOffset
    );
    const calcedOpacity =
      minOpacity < this.minOpacity ? this.minOpacity : minOpacity;
    
    const resultType = this.getTypeForPosition(left, top);
    
    if (this.state.resultType !== resultType) {
      this.setState({
        resultType: resultType.trim(" "),
        resultOpacity: 1 - calcedOpacity,
        glowOpacity: minOpacity,
        cardOpacity: calcedOpacity,
      });
    } else {
      this.setState({
        resultOpacity: 1 - calcedOpacity,
        glowOpacity: minOpacity,
        cardOpacity: calcedOpacity,
      });
    }
  }

  getTypeForPosition(left, top, acceptableOffset = this.acceptableOffset) {
    let type = "";

    if (
      left < 0 &&
      left <= -acceptableOffset &&
      this.swipeMode !== SwipeModes.TopBottom
    ) {
      type += " left";
    }
    if (
      left > 0 &&
      left >= acceptableOffset &&
      this.swipeMode !== SwipeModes.TopBottom
    ) {
      type += " right";
    }

    if (
      top < 0 &&
      top <= -acceptableOffset &&
      this.swipeMode !== SwipeModes.LeftRight
    ) {
      type += " top";
    }
    if (
      top > 0 &&
      top >= acceptableOffset &&
      this.swipeMode !== SwipeModes.LeftRight
    ) {
      type += " bottom";
    }
    return type;
  }

  handleMouseUp(e) {
    const left = this.state.left;
    const top = this.state.top;

    const type = this.getTypeForPosition(left, top).trim(' ');
    if (
      (Math.abs(left) >= this.acceptableOffset ||
      Math.abs(top) >= this.acceptableOffset) &&
      type !== ''
    ) {      
      const currentCard = this.props.children.splice(0, 1);

      if (this.onActionCompleted) {
        this.onActionCompleted(type, currentCard);
      }
      
      this.resetStyle();
    } else {
      this.animateToStart();
    }
  }

  resetStyle() {
    this.setState({
      glowOpacity: 1,
      resultOpacity: 0,
      cardOpacity: 1,
      top: 0,
      left: 0,
    })
  }

  animateToStart() {
    var x = Math.lerp(parseInt(this.card.style.left), 0, 0.1);
    var y = Math.lerp(parseInt(this.card.style.top), 0, 0.1);

    
    const left = x;
    const top = y;
    
  this.setState({
    top: y,
    left: x,
  });

    let calcedOpacity = Math.min(
      (this.acceptableOffset - Math.abs(left)) / this.acceptableOffset,
      (this.acceptableOffset - Math.abs(top)) / this.acceptableOffset
    );
    calcedOpacity =
      calcedOpacity < this.minOpacity ? this.minOpacity : calcedOpacity;
    
    this.setState({
      resultOpacity: 1 - calcedOpacity,
      glowOpacity: 1,
      cardOpacity: calcedOpacity,
    });
    if (x === 0 && y === 0) {
      return;
    }
    requestAnimationFrame(this.animateToStart);
  }

  componentWillMount() {
    this.resetStyle();
  }

  componentDidMount() {
    this.containerWidth = this.container.clientWidth;
    this.containerHeigth = this.container.clientHeight;

    this.cardWidth = this.card.clientWidth;
    this.cardHeight = this.card.clientHeight;

    this.cardPressed = false;
    this.swipeMode = this.props.swipeMode;
    this.onActionCompleted = this.props.onActionCompleted;

    this.acceptableOffset =
      this.props.acceptableOffset || (this.containerWidth - this.cardWidth) / 2;
    this.opacityOffset = this.acceptableOffset / 100 * 10;
    this.minOpacity = this.props.minOpacity || 0.1;

    this.cardMouseDown = this.cardMouseDown.bind(this);
    this.cardMouseUp = this.cardMouseUp.bind(this);
    this.cardMouseMove = this.cardMouseMove.bind(this);
    this.animateToStart = this.animateToStart.bind(this);

    this.card.addEventListener("mousedown", this.cardMouseDown);
    this.card.addEventListener("mouseup", this.cardMouseUp);
    this.container.addEventListener("mousemove", this.cardMouseMove);
  }

  componentWillUnmount() {
    this.card.removeEventListener("mousedown", this.cardMouseDown);
    this.card.removeEventListener("mouseup", this.cardMouseUp);
    this.container.removeEventListener("mousemove", this.cardMouseMove);
  }

  render() {
    const { children } = this.props;
    const activeCard = children[0];
    const backCard = children[1];
    return (
      <div
        className="SwipableCards"
        ref={container => (this.container = container)}
      >
        <div
          style={{opacity: this.state.resultOpacity}}
          className={`result ${this.state.resultType}`}
        />

        {backCard && (
          <div className="backCard">
            <div className="glower" style={{opacity: this.state.glowOpacity}} />
            {React.cloneElement(backCard)}
          </div>
        )}
        {activeCard && (
          <div
            style={{opacity: this.state.cardOpacity, top: this.state.top, left: this.state.left}} ref={card => (this.card = card)} className="cardWrapper">
            {React.cloneElement(activeCard)}
          </div>
        )}
      </div>
    );
  }
}

class Card extends React.Component {
  constructor(props) {
    super(props);
  }
  render() {
    return (
      <div className="Card">
        <div className="card">
          <div className="card-image">
            <img src={this.props.img} />
            <span className="card-title">Card Title</span>
          </div>
          <div className="card-content">
            <p>
              I am a very simple card. I am good at containing small bits of
              information. I am convenient because I require little markup to
              use effectively.
            </p>
          </div>
          <div className="card-action">
            <a href="#">This is a link</a>
          </div>
        </div>
      </div>
    );
  }
}

ReactDOM.render(<App />, document.getElementById("root"));

// Obtient une valeur comprise dans un interval
Math.clamp = function(value, min, max) {
  if (value < min) {
    return min;
  } else if (value > max) {
    return max;
  }

  return value;
};


Math.lerp = function(value1, value2, amount) {
  amount = amount < 0 ? 0 : amount;
  amount = amount > 1 ? 1 : amount;
  return value1 + (value2 - value1) * amount;
};

              
            
!
999px

Console