Jump to content
Search Community

Dynamic ID scrollto and target to each designated targeted id

Louienator test
Moderator Tag

Go to solution Solved by mvaneijgen,

Recommended Posts

Hi @Louienator.

 

We love helping with GSAP-related questions, but your question has nothing to do with GSAP, right? That's about php on your server. Of course anyone else is welcome to post an answer - we just want to manage expectations. 

 

Otherwise, if you've got a GSAP-specific question just post that here along with a minimal demo and we'd be happy to take a look. 

Link to comment
Share on other sites

By the way, this looks very odd to me: 

 $( '.fp-intro__list--item' ).each( function() {
   var test = document.querySelector( '.fp-intro__btn' );
   $( test ).click( function() {

     gsap.to( window, {
       duration: 3,
       scrollTo: '#<?= strtolower( get_the_title() ); ?>',
       ease: 'expo.out'
     });
   });
 });

You're looping through a bunch of elements, but for every single one you're using the exact same selector, ".fp-intro__btn" and adding a click event handler to it. So you end up having the same element with a bunch of different click event handlers. Is that really what you want? And I assume your get_the_title() php would return the same element every time through the jQuery loop as well which seems problematic. 

 

It kinda smells to me like you've got logic/engineering issues there, almost like you're trying to do php-related loops but inside a jQuery loop, as if jQuery is gonna iterate through a php loop. That can't happen. I don't think I've ever seen someone embed php logic into a JavaScript tween like that, so I think you might want to re-think your approach. Or perhaps I'm misunderstanding something. 

Link to comment
Share on other sites

Hi everyone,

 

Let me clarify what I'm trying to accomplish. I want a button that, upon being clicked, will smoothly scroll to a specific section on the page. To achieve this, I need the ID to be dynamic and outputted through the use of get_the_title().

 

In the code, there's a line that reads "<section class="screen" id="cebu">Cebu</section>". This line will be looped using PHP, or maybe just the ID will be used to make it unique. To target this specific line, I added the GSAP code.


Here's the fiddle:

https://jsfiddle.net/kwuthpbm/29/

Link to comment
Share on other sites

  • Solution

I would give the elements that care clickable and [data-id="TITLE"] (eg <button class="branch-name__btn" data-id="cebu">Cebu</button> ) attribute and then get that in the Javascript code.

 

Just set the data attribute to the same value as you've given the ID of the section. Than on click get that .dataset.id using javascript and put it in a querySelector() to get the element based on that id.  This all is basic Javascript and has nothing to with GSAP, so be sure to brush up on some Javascript knowledge if you want to take your skills a step further. 

 

I’ve placed some comments in your JS to better explain things, please be sure to read through them!

 

Hope it helps and happy tweening! 

 

See the Pen jOJdovx?editors=1001 by mvaneijgen (@mvaneijgen) on CodePen

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

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