Reputation: 2619
I want to make a web browser adjustment margin left. I use windowResize, but how to adjustment margin left even if the custom click the restore button of the web browser? I upload 2 screenshort images for explain. first image, I open the web browser not maximize, so the page make a windowResize and count the margin-left, then I click the windows maximize, then get the
second image, but the content is not in the center of the page. How to do a right windowResize? Thanks.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="jquery-1.4.4.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function() {
var width = document.body.clientWidth;
windowResize(width);
$(window).resize(function() {
windowResize(width);
});
});
function windowResize(width) {
if(width>1024){
$('#content').css({
'margin-left':(width-1024)/2+32 + 'px'
});
}else{
$('#content').css({
'margin-left': 32 + 'px'
});
}
}
</script>
</head>
<body>
<style>
body, #box{min-width:1024px;width:100%;}
#content{float:left;width:960px;height:300px;background-color:#F00;}
</style>
<div id="box">
<div id="content">
some words
</div>
</div>
</body>
</html>
Upvotes: 1
Views: 476
Reputation: 6208
One thing I notice is that this code will only ever call windowResize
with the initial width:
jQuery(document).ready(function() {
// this value will always be passed to windowResize
var width = document.body.clientWidth;
windowResize(width);
$(window).resize(function() {
windowResize(width);
});
});
I changed it so the width is calculated inside of windowResize
and things work.
jQuery(document).ready(function() {
windowResize();
$(window).resize(windowResize);
});
function windowResize() {
var width = document.body.clientWidth;
if(width>1024){
$('#content').css({
'margin-left':(width-1024)/2+32 + 'px'
});
}else{
$('#content').css({
'margin-left': 32 + 'px'
});
}
}
Upvotes: 1