Jump to content
Search Community

Repetitive code reduction

Marshmallow test
Moderator Tag

Warning: Please note

This thread was started before GSAP 3 was released. Some information, especially the syntax, may be out of date for GSAP 3. Please see the GSAP 3 migration guide and release notes for more information about how to update the code to GSAP 3's syntax. 

Recommended Posts

Hi,

 

I currently have a piece of code that works as a slideshow and on each slide it changes the values of some elements.

However to keep the user from going to the next slide while the last one is still busy I have added a onComplete and onUpdate variable to every part of the animation to keep checking if the animation is done.

However this has resulted in quite the long code. Is there any way I can use less lines here?

 

my code:

function moveSlideUp(w) {
    if(w.prev().is('.work-slide')) {
        w.toggleClass('active');
        w.prev().toggleClass('active');
        
        // Clearing all inline styles
        
        remove_style($('.title'));
        remove_style($('.subtitle'));
        remove_style($('.categories').find('li'));
        remove_style($('.case-button'));
        
        // Title animation
        
        TweenMax.to(w.find('.title'), 1, {
            left: -200,
            opacity: 0,
            onUpdate: function() {isUpdating = true;},
            onComplete: function() {isUpdating = false;}
        })
        TweenMax.to(w.find('.subtitle'), 1, {
            left: -200,
            opacity: 0,
            delay: 0.2,
            onUpdate: function() {isUpdating = true;},
            onComplete: function() {isUpdating = false;}
        })
        TweenMax.staggerTo(w.find('.categories').find('li'), 1, {
            left: -1000,
            opacity: 0,
            delay: 0.4,
            onUpdate: function() {isUpdating = true;},
            onComplete: function() {isUpdating = false;}
        }, 0.1)
        TweenMax.to(w.find('.case-button'), 1, {
            left: -200,
            opacity: 0,
            delay: 0.6,
            onUpdate: function() {isUpdating = true;},
            onComplete: function() {isUpdating = false;}
        })
        
        TweenMax.from(w.prev().find('.title'), 1, {
            left: -200,
            opacity: 0,
            delay: 1.2,
            onUpdate: function() {isUpdating = true;},
            onComplete: function() {isUpdating = false;}
        })
        TweenMax.from(w.prev().find('.subtitle'), 1, {
            left: -200,
            opacity: 0,
            delay: 1.4,
            onUpdate: function() {isUpdating = true;},
            onComplete: function() {isUpdating = false;}
        })
        TweenMax.staggerFrom(w.prev().find('.categories').find('li'), 1, {
            left: -1000,
            opacity: 0,
            delay: 1.6,
            onUpdate: function() {isUpdating = true;},
            onComplete: function() {isUpdating = false;}
        }, 0.1)
        TweenMax.from(w.prev().find('.case-button'), 1, {
            left: -200,
            opacity: 0,
            delay: 1.8,
            onUpdate: function() {isUpdating = true;},
            onComplete: function() {isUpdating = false;}
        })
        
        // Major movement below
        
        TweenMax.to(w, 1, {
            top: '100%',
            ease:Power2.easeInOut,
            delay: 1,
            onUpdate: function() {isUpdating = true;},
            onComplete: function() {isUpdating = false;}
        })
        TweenMax.to(w.prev(), 1, {
            top: 0,
            ease:Power2.easeInOut,
            delay: 1,
            onUpdate: function() {isUpdating = true;},
            onComplete: function() {isUpdating = false;}
        })
        TweenMax.to('.next-work', 0.5, {
            opacity: 1,
            cursor: 'pointer',
            ease:Power2.easeInOut,
            delay: 1,
            onUpdate: function() {isUpdating = true;},
            onComplete: function() {isUpdating = false;}
        })
        if(!w.prev().prev().is('.work-slide')) {
            TweenMax.to('.prev-work', 0.5, {
                opacity: 0,
		        cursor: 'auto',
                ease:Power2.easeInOut,
                delay: 1,
                onUpdate: function() {isUpdating = true;},
                onComplete: function() {isUpdating = false;}
            })
        } else {
            TweenMax.to('.prev-work', 0.5, {
                opacity: 1,
		        cursor: 'pointer',
                ease:Power2.easeInOut,
                delay: 1,
                onUpdate: function() {isUpdating = true;},
                onComplete: function() {isUpdating = false;}
            })            
        }
    }
}

Thanks

 

~  Marshmallow

Link to comment
Share on other sites

Hey there buddy and Welcome to GreenSock Forum!

First of all buddy please could you please provide a quick codepen of your work?

This will help us to explore your code and fix it by forking your codes....

Here's the link for creating your very own codepen!
http://greensock.com/forums/topic/9002-read-this-first-how-to-create-a-codepen-demo/

Here's a video buddy!



====

I suggest try to loop some of repetitive lines on your code.

Waren | GSAP Enthusiast
  • 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...