Jump to content
Search Community

V shap dragable carousel in react & gsap-free

Mbb1
Moderator Tag

Recommended Posts

Posted

It has to look so:

image.png.592b4f3cc1a792f5798b60a8367333a0.pngbut the most I manage to achive is:

image.png.8ca25ec0290571980f94eae9e35d1958.png

I don't know how to make the second side of the V shape, I will be thirled for help by fixing my code or simply give me a new match code.

thanks in advanced.

 

attached is the current react code I'm using:

css:

* {
  box-sizingborder-box;
}
 
html,
body {
  height100%;
  min-height100%;
  font-family: Roboto, sans-serif;
  background#e2e0e0;
}
 
main {
  displayflex;
  positionrelative;
  flex-directioncolumn;
  width100vw;
  height100vh;
  overflowhidden;
  align-itemscenter;
  justify-contentcenter;
}
 
.cell {
  positionabsolute;
  top0;
  left0;
  width100%;
  font-size26px;
  font-weight500;
  colorrgba(0000.87);
  user-selectall;
  displayflex;
  align-itemscenter;
  justify-contentcenter;
  background#eee;
 
  height70%;
  transform-origincenter center;
}
 
.picker {
  positionrelative;
  overflowhidden;
  width80vw;
  height90vh;
  border-radius2px;
  background-color#fff;
  box-shadow0 2px 4px -1px rgba(0000.2), 0 4px 5px 0 rgba(0000.14),
    0 1px 10px 0 rgba(0000.12);
}

js:

import { useRef } from 'react';
import gsap from 'gsap';
import { Draggable } from 'gsap/all';
import { useGSAP } from '@gsap/react';
 
gsap.registerPlugin(Draggable);
 
const App = () => {
  const pickerRef = useRef(null);
  const cellsRef = useRef([]);
 
  useGSAP(() => {
    gsap.defaults({ ease: 'none' });
    const picker = pickerRef.current;
    const cells = cellsRef.current;
    const proxy = document.createElement('div');
    const myWrapper = gsap.utils.wrap(00.871);
 
    const cellWidth = 75;
 
    const numCells = cells.length;
    const cellStep = 1 / numCells;
    const wrapWidth = cellWidth * numCells;
 
    const baseTl = gsap.timeline({ paused: true });
 
    gsap.set(picker, {
      width: wrapWidth - cellWidth,
    });
    cells.forEach((celli=> {
      initCell(celli);
    });
 
    const animation = gsap
      .timeline({ repeat: -1paused: true })
      .add(baseTl.tweenFromTo(12));
 
    const draggable = Draggable.create(proxy, {
      type: 'x',
      trigger: picker,
      inertia: true,
      onDrag: updateProgress,
      onThrowUpdate: updateProgress,
      snap: {
        x: snapX,
      },
      onThrowComplete: () => {
        console.log('onThrowComplete');
        // TODO: animation that injects selected card title
      },
    })[0];
 
    function snapX(x) {
      return Math.round(x / cellWidth) * cellWidth;
    }
 
    function updateProgress() {
      animation.progress(myWrapper(draggable.x / wrapWidth));
    }
 
    function initCell(elementindex) {
      gsap.set(element, {
        width: cellWidth,
        scale: 0.6,
        x: -cellWidth,
        y: 0,
      });
 
      const tl = gsap
        .timeline({ repeat: 1 })
        .to(element, { duration: 1x: `+=${wrapWidth}`y: '+=70' }, 0)
        .to(
          element,
          {
            duration: cellStep,
            color: '#009688',
            scale: 0.778,
            repeat: 1,
            yoyo: true,
          },
          0.5 - cellStep
        );
 
      baseTl.add(tlindex * -cellStep);
    }
  }, []);
 
  return (
    <main>
      <div ref={pickerRef} className="picker">
        {Array.from({ length: 5 }, (_index=> (
          <div
            key={index}
            ref={(el=> (cellsRef.current[index] = el)}
            className="cell"
          >
            <div className="cell-content">{`Card ${index + 1}`}</div>
          </div>
        ))}
      </div>
    </main>
  );
};
 
export default App;
 
Posted

Without a minimal demo, it's very difficult to troubleshoot; the issue could be caused by CSS, markup, a third party library, a 3rd party script, etc. Would you please provide a very simple CodePen or Stackblitz that illustrates the issue? 

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best. See if you can recreate the issue with as few dependencies as possible. Start minimal and then incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

See the Pen aYYOdN by GreenSock (@GreenSock) on CodePen.

that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

 

Using a framework/library like React, Vue, Next, etc.? 

CodePen isn't always ideal for these tools, so here are some Stackblitz starter templates that you can fork and import the gsap-trial NPM package for using any of the bonus plugins: 

 

Please share the StackBlitz link directly to the file in question (where you've put the GSAP code) so we don't need to hunt through all the files. 

 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...