简体   繁体   中英

if width == 200px run function

Here is a jsFiddle showing the problem

I'm not sure why this doesn't work:

if ($('#menu').width() == '200px') {
    alert("what");
}

I want an alert to appear when the animation is completed. So I assumed that I could say that since when the animation is done the element has a width of 200px, it would show the alert.

1) You're checking for the value before the animation completes.

2) You width value will be "200" not "200px"

Here is the jQuery documentation. You need to perform your check in the callback section of the code.

$('#clickme').click(function() {
  $('#book').animate({
    opacity: 0.25,
    left: '+=50',
    height: 'toggle'
  }, 5000, function() {
    // Animation complete.   <---- your value check code goes here
  });
});

jQuery.width returns a number. You are comparing with a string.

Check the updated fiddle. What I did was added a 'callback' function, which executes whenever the animation is complete.

$(this).stop().animate({ width: "200px" }, 250,
  function(){if ($('#menu').width() == '200') { alert("what"); } /* Callback Function */
 });

.width()应该返回一个数字,而不是“ ### px”值,请尝试if ($('#menu').width() == 200) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM