2

I'm trying to change browser width and height into .test div. whenever I re-size the browser the current value should change.

$(document).ready( function() { window.onresize = function(event) { resizeDiv(); } function resizeDiv() { var vpw=window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth; var vph=window.innerHeight || document.documentElement.clientHeight ||document.body.clientHeight; $(‘.test’).css({‘height': vph + ‘px’}); } );
.test { background: green; padding: 20px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="test"></div>

1
  • You'd be better of with $(‘.test’).height(vph); Commented Apr 2, 2015 at 11:22

3 Answers 3

3

Example

example

js

function resize() { $(".test").css({ width: $(window).width(), height: $(window).height() }); } resize(); $(window).resize(function(){ resize(); }); 

if you want minus some pixels

function resize() { $(".test").css({ width: $(window).width() - 15, height: $(window).height() - 15 }); } 
Sign up to request clarification or add additional context in comments.

5 Comments

Working well. I have some more doubt?
Nothing wrong. I want minus some px from this width and height. how can i achieve this.
Thanks for your time. and how can i get some div's height and width on re-size?
select the div width class /var width = $(".bllock").width();/ and put it on resize function and after that you can get thw width
I need one more help Aram.
1

Uncaught SyntaxError: Unexpected token ILLEGAL

You use ‘ and ’. In order that your script to run you need to use ' or ".

1 Comment

Yes, $(‘.test’).css({‘height': vph + ‘px’}); ==> $(‘.test’).css({‘height', vph + ‘px’});
0

Best practice for what you are asking is to use responsive design: http://responsivedesign.is/examples/playgrounds-and-sandboxes/lots-of-donuts

for example:

@media only screen and (min-width: 1300px) { #divId {width:1000px;}} @media screen and (max-width: 860px) {#divId {width:800px;} } @media screen and (max-width: 650px) { #divId {width:600px;}} @media screen and (max-width: 480px) { #divId {width:400px;}} 

1 Comment

I dont want css solution. because am doing some what different use.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.