0

With the code below I want to the function to run when it called , for now nothing is happening, does the name of the function unacceptable?

function zoom(d) {
  if (d == "in")
    $("#animation_border").css("height", ($(window).height() * 100 / 85) + "px");
  else
    $("#animation_border").css("height", (canvas.width * 0.719) + "px");

}

if ($(window).width() >= 619) {
  zoom("out");
} else {
  zoom("in");
}
}
5
  • Are you trying to make zoom happen when the window is resized? Commented Feb 5, 2017 at 16:28
  • the code seems OK , just a "}" at the end. do you execute this on document ready ,window resize ,or some other actions ? Commented Feb 5, 2017 at 16:29
  • 1
    If you expect something to happen on window resize, then capture window resize event and have your code in there $(window).resize(function(){/* YOUR CODE HERE */}) Commented Feb 5, 2017 at 16:30
  • yes daniel...Vladu i removed that but it also not working, is using it in the document ready a problem? Commented Feb 5, 2017 at 16:33
  • Put that if ($(window).width() >= 619) { zoom("out"); } else { zoom("in"); } inside $(window).resize(function(){/* YOUR CODE HERE */}) and that's gonna work Commented Feb 5, 2017 at 16:35

2 Answers 2

2

I'm assuming you want to call the zoom() function when the window is resized. If so, we can use the $(window).resize() event to get what you want:

function zoom(d) {
  if (d == "in") {
    // Your code here
  } else {
    // Your code here
  };
}

// Automatically called when window is resized
$(window).resize(function() {
  if ($(window).width() >= 619) {
    zoom("out");
  } else {
    zoom("in");
  };
})

Working JSFidde: https://jsfiddle.net/209m4esk/2/

As a side note, it would be more efficient to compare a boolean in your zoom() function rather than a string. We should also allow for invalid parameters given to the function. i.e:

function zoom(zoomIn) {
  if (typeof(zoomIn) !== "boolean") {
      console.log("Parameter given is not a boolean.");
  } else if (zoomIn) {
    // Your code here
  } else {
    // Your code here
  };
}

So then you'd simply call zoom(true) to zoom in, and zoom(false) to zoom out.

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

Comments

1
  • first you have }
  • put your code in window.resize

    (function(){
    function zoom(d) {
      if (d == "in")
        $("#animation_border").css("height", ($(window).height() * 100 / 85) + "px");
      else
        $("#animation_border").css("height", (canvas.width * 0.719) + "px");
    
    }
    
    $(window).resize(function(){
        if ($(window).width() >= 619) {
          zoom("out");
        } else {
          zoom("in");
        }
    })})();
    

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.