单击时调整图像大小

时间:2015-02-23 19:25:47

标签: javascript image onclick resize

好吧,我需要在点击时调整图像大小(根据用户点击的内容)选择50%,100%和150%。我还需要包含一条消息,告诉用户一旦缩放后图片的百分比是多少。

我认为我所拥有的是正确的或正确的方向(显然不是,因为它不起作用)想知道是否有人可以帮助我...这就是我目前所拥有的:

<!doctype html>
<html lang="en">
<head>
 <title>Change Image Size</title>
  <style>
   body {width: 80%; margin: auto;}
   header {border-bottom: 1px solid #aaa;}
   header h1 {font-size: 1.5em;}
   nav {float: left; width: 15%; margin: 2% 2%; }
   main {float: left; width: 60%; border: 0px black solid; margin: 2% 2%; height: 500px; padding: 1em;}
   main img {border: 1px black solid;}
   nav span {display: inline-block; width:100px; height: 30px; border: 1px solid black; border-radius: 10px; text-align: center; margin-bottom: 1em; background-color: green; color: white;}

</style>
<script>
function init (){
  var object = document.getElementById('div1');
  document.getElementById('small').onclick = function () {change('div1', '50%')};
  document.getElementById('medium').onclick = function () {change('div1', '100%')};
  document.getElementById('large').onclick = function () {change('div1', '150%')};

}

function change (id, width){
document.getElementById(id).style.width = width;
document.getElementById(id).style.height = 'auto';
}




 window.onload = init;

 </script>
</head>
<body>

 <header><h1>CTEC 4350 DHTML Exercise:  Change Image Size</h1></header>
 <nav>
  <span id="small">50%</span> 
<span id="medium">100%</span>
<span id="large">150%</span>
</nav>
<main>
<p> This image is displayed at 150% of its orignal size. (The number of % changes based on the size option picked by the user.)</p>
<div id="div1">
<img src="http://omega.uta.edu/~cyjang/ctec4350/labex/dhtml/alice28a.gif">
</div>
</main>
</body>
</html>

我仍然有这个问题。我需要找出当前的大小,然后添加它....但我不确定我是否正确地做了...我已经改变了我的代码一点,但甚至不确定我是否我正朝着正确的方向前进。什么都没有改变,我不知道为什么o.o

<!doctype html>
<html lang="en">
 <head>
 <title>Change Image Size</title>
  <style>
    body {width: 80%; margin: auto;}
    header {border-bottom: 1px solid #aaa;}
    header h1 {font-size: 1.5em;}
    nav {float: left; width: 15%; margin: 2% 2%; }
    main {float: left; width: 60%; border: 0px black solid; margin: 2% 2%; height: 500px; padding: 1em;}
    main img {border: 1px black solid;}
    nav span {display: inline-block; width:100px; height: 30px; border: 1px solid black; border-radius: 10px; text-align: center; margin-bottom: 1em; background-color: green; color: white;}

  </style>
  <script>
  function init (){
  document.getElementById('small').onclick = function () {change('div1',50,50)};
  document.getElementById('medium').onclick = function () {change('div1',100,100)};
  document.getElementById('large').onclick = function () {change('div1',150,150)};

  }

  function change(id, y, x){
      var img = document.getElementById('div1');

      img.style.width = img.offsetWidth + y + "px";
      img.style.height = img.offsetHeight + x + "px";

      document.getElementById(id).style.width = y;
      document.getElementById(id).style.height = x;



   }




  window.onload = init;

  </script>
 </head>
 <body>

  <header><h1>CTEC 4350 DHTML Exercise:  Change Image Size</h1></header>
  <nav>
    <span id="small">50%</span> 
    <span id="medium">100%</span>
    <span id="large">150%</span>
  </nav>
  <main>
    <p> This image is displayed at 150% of its orignal size. (The number of % changes based on the size option picked by the user.)</p>

    <div id="div1">
    <img src="http://omega.uta.edu/~cyjang/ctec4350/labex/dhtml/alice28a.gif" height="400px" width="400px">
    </div>
  </main>
 </body>
</html>

0 个答案:

没有答案