当前位置: 代码迷 >> HTML/CSS >> 根据“#big”的背景大小自动改变div的大小,该如何做
  详细解决方案

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

热度:193   发布时间:2012-09-20 09:36:50.0
根据“#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>

------解决方案--------------------
  相关解决方案