1

In the below code, there's the "initialHeight" property which has a fixed value of 222. The problem is, the website is designed responsively, so I need to have different values for "initialHeight" depending on the screen resolution. So if possible I need to have the script read the height from whatever I specify in the CSS.

Here's the current code:

<script type="text/javascript">
$().ready(function() { 
$("div.more-block").divgrow({
initialHeight: 222
}); 
});
</script>

So I'd like to have something like....

<style type="text/css">

.height {height:250px}

@media screen and (max-width: 1440px) {
.height {height:210px}
}

@media screen and (max-width: 1200px) {
.height {height:150px}
}

</style>

<script type="text/javascript">
    $().ready(function() { 
    $("div.more-block").divgrow({
    initialHeight: (css height style from .height)
    }); 
 });
 </script>

Is that possible somehow?

4
  • 1
    Why not just use initialHeight: $("div.more-block").height() ? Commented Aug 1, 2014 at 20:03
  • I guess he wouldn't want to divgrow() the div if it already had the right height. @user1610904 what does divgrow() does exactly? Commented Aug 1, 2014 at 20:07
  • Basically it takes the "more-block" div and shows more content after a specified height. So for example it's supposed to show only 222px of height on page load, but then when you click a button it expands the content. Here's the divgrow script page if that helps: mywebdeveloperblog.com/my-jquery-plugins/jquery-divgrow Commented Aug 1, 2014 at 20:12
  • I updated the answer to fit what you needed. Commented Aug 1, 2014 at 20:18

1 Answer 1

1

I believe what you are looking for is setting the minimum height of your container first:

@media screen and (max-width: 1440px) {
   .more-block {
      min-height: 210px;
   }
}

Then you can get that value in JS, like that:

var $more = $("div.more-block");
var initialHeight = parseInt($more.css('min-height'));

$more.divgrow({
    initialHeight: initialHeight
});
Sign up to request clarification or add additional context in comments.

2 Comments

var initialHeight = +$more.css('min-height'); bit shorter ;)
Shorter is not always nicer. Clear code that tells what is being done is way better than shorter code. Why use an addition operator to change the type of variable when you have proper method to do so?

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.