首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > CSS >

根据“#big”的背景大小自动改变div的大小,该如何做

2012-09-20 
根据“#big”的背景大小自动改变div的大小,该怎么做head runatservertitle/titlestyle typetext

根据“#big”的背景大小自动改变div的大小,该怎么做
<head runat="server">
  <title></title>
  <style type="text/css">
  .div1{ float:left; padding:20px 20px 20px 20px; background-color:Green; border:1px solid black; cursor:pointer;}
  .div2{ width:500px;height:500px;}
  </style>
  <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
  <script type="text/javascript">
  var currDiv
  $(document).ready(function () {
  $(".div1").click(function () {
  currDiv = $(this);
  var imgsrc = currDiv.find("img").attr("src");
  var image = new Image();
  image.src = imgsrc;
  var w = image.width;
  var h = image.height;

  $("#big").css('background-image', 'url(' + imgsrc + ')');
  $("#big").width(w);
  $("#big").height(h);
  $("#big").show();
  });
  $("#left").click(function () {
  var img1 = new Array();
  img1 = document.getElementById('big').currentStyle.backgroundImage;
  var a = Number(img1[32]) - 1;
  if (a > 0) {
  $("#big").css('background-image', 'url(img/' + a + '.jpg)');
  }
  else {
  alert("已经是第一张图片");
  }
  });
  $("#right").click(function () {
  var img1 = new Array();
  img1 = document.getElementById('big').currentStyle.backgroundImage;
  var a = Number(img1[32]) + 1;
  if (a < 9) {
  $("#big").css('background-image', 'url(img/' + a + '.jpg)');
  }
  else {
  alert("已经是最后一张图片");
  }
  });
  });
  </script>
</head>
<body>
<div class="div1"><img src="img/1.jpg" alt="无法显示" /></div>
<div class="div1"><img src="img/2.jpg" alt="无法显示" /></div>
<div class="div1"><img src="img/3.jpg" alt="无法显示" /></div>
<div class="div1"><img src="img/4.jpg" alt="无法显示" /></div>
<div style=" clear:both;"></div>
<div class="div1"><img src="img/5.jpg" alt="无法显示" /></div>
<div class="div1"><img src="img/6.gif" alt="无法显示" /></div>
<div class="div1"><img src="img/7.jpg" alt="无法显示" /></div>
<div class="div1"><img src="img/8.jpg" alt="无法显示" /></div>
<div style=" clear:both;"></div>

<div id="big" style="margin:0 auto;border:1px solid black; width:auto; height:auto; ">
<div id="left" style="float:left;width:50%;height:100%;border:0px solid black;"></div>
<div id="right" style="float:left;width:50%;height:100%;border:0px solid black;"></div>
</div>

</body>

------解决方案--------------------


如果单纯是用css+html显示div大小根据图片大小改变尺寸是可以实现的。
但是改动比较大。

HTML code
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>无标题文档</title><style type="text/css">.wbg{ width:auto;float:left; overflow:hidden;height:auto; position:relative;}.wbg-img{}.img{ position:absolute; z-index:10; left:0; top:0;}</style></head><body><div class="wbg">  <div class="wbg-img"><img src="http://info-database.csdn.net/Upload/2012-09-05/pongo3625.gif" width="600" height="500" /></div><!--这里的图片可以随意变动大小可以理解为是背景图片-->  <div class="img"><img src="http://info-database.csdn.net/Upload/2012-09-05/pongo3625.gif" alt="无法显示" /></div></div></body> 

热点排行