1

I am fairly new to jQuery/javascript and I have a quick question, probably very simple but I can't seem to figure it out.

I want to change a variable value when the window width has changed. The reason I'm doing it with jQuery is that I can't alter the width or height via css.

This is my code:

<script type="text/javascript">

    var jwheight = 390,
    windowSize = $(window).width();

    $(window).resize(function(){
        if (windowSize < 992) {
            jwheight = 39;
        }
    })

player.initialize(800, jwheight, "player", "http://www.website.com", "http://www.website.com/static/images/logo.png", true);

</script>

So I want to change the height (jwheight) when the window width is smaller than 992px.

All help would be appreciated, thank you.

5
  • Make the if if ($(window).width() < 992) Commented Sep 22, 2015 at 21:24
  • 1
    you have to re-calculate the window width inside the function. The initial width calculated was initialy before any window re-size. That is the point Commented Sep 22, 2015 at 21:27
  • Note that changing the variable jwheight doesn't change the player object. Commented Sep 22, 2015 at 21:28
  • @Guffa how would I go about changing the player object? I need that height to change Commented Sep 22, 2015 at 21:36
  • @KD1: Use the new variable value to call the resize method of the player. Commented Sep 22, 2015 at 21:52

2 Answers 2

3

your windowSize is outside the resize function, so it will always have the same value. You should use the width inside the function as follows:

<script type="text/javascript">
    var jwheight = 390;
    $(window).resize(function(){
        if ($(window).width() < 992) {
            jwheight = 39;
        }
    })

    player.initialize(800, jwheight, "player", "http://www.website.com", "http://www.website.com/static/images/logo.png", true);

</script>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanx @taxicala I'll try this out
0

Check the $(window).width() value inside the resize event, and resize the player depending on the result.

You would want to calculate the height depending on the width, then compare that to the current height so that you don't call player.resize when there isn't actually a size change:

var jwheight = 390;

$(window).resize(function(){
  var newHeight = $(window).width() < 992 ? 39 : 390;
  if (newHeight != jwheight) {
    jwheight = newHeight;
    player.resize(800, jwheight);
  }
})

player.initialize(800, jwheight, "player", "http://www.website.com", "http://www.website.com/static/images/logo.png", true);

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.