2

I'm playing around with https://tympanus.net/codrops/2014/03/05/simple-stack-effects/ right now but I'm struggling to use a link instead of a button as the trigger for this animation.

<button>Click me</button>
<figure class="stack stack-sidegrid">
  <img src="img/1.png" alt="img01"/>
  <img src="img/3.png" alt="img03"/>
  <img src="img/4.png" alt="img04"/>
</figure>

change to

<a href="#">
  <figure class="stack stack-sidegrid">
    <img src="img/1.png" alt="img01"/>
    <img src="img/3.png" alt="img03"/>
    <img src="img/4.png" alt="img04"/>
  </figure>
</a>

This is the js script for handling the event.

(function() {
    [].slice.call( document.querySelectorAll( '.stack' ) ).forEach( function( el ) {
        var togglebtt = el.previousElementSibling,
            togglefn = function() {
                if( classie.hasClass( el, 'active' ) ) {
                    classie.removeClass( el, 'active' );
                }
                else {
                    classie.addClass( el, 'active' );
                }
            };
        togglebtt.addEventListener( 'click', togglefn );
    } );
})();

Does anybody maybe have a suggestion how I get this code to work?

Reference: https://tympanus.net/codrops/2014/03/05/simple-stack-effects/

1
  • Just make sure that, after loading document you should call above mentioned JS code. I've made a jsfiddle of above example using anchor tag instead of button & it's working. jsfiddle.net/cq0xfL72 Commented Oct 18, 2018 at 6:25

1 Answer 1

1

Just change var togglebtt = el.previousElementSibling, to var togglebtt = el.parentElement,

like this:

(function() {
    [].slice.call( document.querySelectorAll( '.stack' ) ).forEach( function( el ) {
        var togglebtt = el,
            togglefn = function() {
                if( classie.hasClass( el, 'active' ) ) {
                    classie.removeClass( el, 'active' );
                }
                else {
                    classie.addClass( el, 'active' );
                }
            };

        togglebtt.addEventListener( 'click', togglefn );
    } );
})();

In that demo, togglebtt = el.previousElementSibling is used as trigger,

el is each <figure class="stack ">

its previousElementSibling is <button>Click me</button> according the HTML.

You want to use a link to trigger instead of this button only need to change value of togglebtt.

change it to el or el.parentElement

Sign up to request clarification or add additional context in comments.

2 Comments

It works perfectly and thanks to your detailed explanation!! Really appreciate it :) Thanks!
glad to help, it's a demo seems you don't need now...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.