jesper.landberg

Gsap values/props from data-attribute?

Recommended Posts

Hi,

Im trying to build out some timelines based on props and values from data-attributes. However it doesn't seem to work, and it's throwing no errors or doing nothing when i try to play or build it. What am I missing?

 

Looks like this:

 

<figure class="c-photos__img c-photos__img--1">
  <div data-from="yPercent: 0, rotation: 0" data-to="yPercent: -100, rotation: 5, ease: Expo.easeOut">
    <img src="/static/images/photo-1.jpg">
  </div>
</figure>
<figure class="c-photos__img c-photos__img--2">
  <div data-from="yPercent: 0, rotation: 0" data-to="yPercent: -100, rotation: 5, ease: Expo.easeOut">
    <img src="/static/images/photo-3.jpg">
  </div>
</figure>
<figure class="c-photos__img c-photos__img--3">
  <div data-from="yPercent: 0, rotation: 0" data-to="yPercent: -100, rotation: 5, ease: Expo.easeOut">
    <img src="/static/images/photo-3.jpg">
  </div>
</figure>
<figure class="c-photos__img c-photos__img--4">
  <div data-from="yPercent: 0, rotation: 0" data-to="yPercent: -100, rotation: 5, ease: Expo.easeOut">
    <img src="/static/images/photo-4.jpg">
   </div>
</figure>
<figure class="c-photos__img c-photos__img--5">
  <div data-from="yPercent: 0, rotation: 0" data-to="yPercent: -100, rotation: 5, ease: Expo.easeOut">
    <img src="/static/images/photo-5.jpg">
  </div>
</figure>


 

this.dom.elems.forEach(el => {
  const tl = new TimelineLite({ paused: true })
  const from = el.dataset.from
  const to = el.dataset.to

  tl.fromTo(el, 1, { from }, { to })

  tl.play() // Example, this won't play or throw any error

  this.elems.push({
    tl: tl
  })
})

 

Share this post


Link to post
Share on other sites

Those values get passed as string. You need to create JSON string and parse it.

 

<div data-from='{"yPercent": "0", "rotation": "0"}' data-to='{"yPercent": "-100", "rotation": "5", "ease": "Expo.easeOut"}'>

 

  const from = JSON.parse(el.dataset.from);
  const to = JSON.parse(el.dataset.to);

 

Double quotes are important, if you don't want to use quotes, you will find some stack overflow threads with regex solutions. You can also encode JSON string from server in PHP, there will be equivalent solutions for other languages.

  • Like 6

Share this post


Link to post
Share on other sites
2 minutes ago, Sahil said:

Those values get passed as string. You need to create JSON string and parse it.

 


<div data-from='{"yPercent": "0", "rotation": "0"}' data-to='{"yPercent": "-100", "rotation": "5", "ease": "Expo.easeOut"}'>

 


  const from = JSON.parse(el.dataset.from);
  const to = JSON.parse(el.dataset.to);

 

Double quotes are important, if you don't want to use quotes, you will find some stack overflow threads with regex solutions. You can also encode JSON string from server in PHP, there will be equivalent solutions for other languages.


There it was, thanks you:)

Share this post


Link to post
Share on other sites

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.