0

I am trying to change the .css property so that the css transitions will animate on click. For some reason this is not working. This action needs to toggle.

 $("button.subnav-toggle").click(function(){              
          $(".subnav").toggle(
            function() {
              $(".subnav").css({'top':'0'});
              alert("something");
            }, function() {
              $(".subnav").css({'top':'-150px'});
          })
        });
6
  • How is it not working? What do you expect it to do and what does it actually do? Commented Apr 14, 2014 at 18:41
  • @Which jquery version are you using? Commented Apr 14, 2014 at 18:43
  • Toggle has been removed in the version 1.9 Commented Apr 14, 2014 at 18:43
  • Using the latest version of JQuery Commented Apr 14, 2014 at 18:46
  • The expected action is to change the .css from 0 to -150 and back. I am using the transition property in CSS3 so that every time the "top" position changes the div.subnav will animate up and down. Commented Apr 14, 2014 at 18:48

3 Answers 3

2

how about toggling a class with these positions?

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

4 Comments

Something like $(".subnav").toggleClass(theclass); and assign it the top value your looking for on the first click. -150px?
Yes it will, just make sure you have the transition calls set up on your subnav class and it should animate, you may have make the top attribute important on the class your toggling to override the other classes top attribute. Let me know if it works for you.
That sounds perfect. I will try this.
Don't forget to notify us as to which answer worked for you when you figure it out.
1

.toggle won't do a transition. You could use .animate though:

$("button.subnav-toggle").click(function(){  
      $(this).animate({top:'-150px'})
});

Revision:

$("button.subnav-toggle").click(function(){  

    var top = $(this).css('top');
    var topTo = (top == 'auto' || top == '0px') ? '-150px' : '0px';
    $(this).css('top', topTo)
});

Fiddled here: http://jsfiddle.net/mhfaust/K5hEb/

2 Comments

I need too toggle between two positions because it is a dropdown menu i am trying to animate. The position depends on the click of one button.
Revised according to this clarification.
0
$(document).ready(function () {
    $('button.subnav-toggle').click(function () {
        var visible = $('.subnav').is(":visible");
        $('.subnav').slideToggle(500);
        if (!visible) {
            $('html, body').animate({
                scrollTop: $('.subnav').offset().top
            }, 500);
        }
    });
});

Comments

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.