点击图片后缩小javascript

时间:2018-05-29 08:33:19

标签: javascript

我有一个带有图标的菜单和每个图标的底部 - 标题。单击图像时,图像缩放为100%,但问题是当我点击其他图标时此图标必须缩小85%宽度,但这不会发生。我尝试在每个图像上使用onclick,或者设置默认值,然后点击调用该宽度,但是注意......如果有人可以提供帮助......

这是我的演示:https://codepen.io/EVasileva/pen/BVayxz

这里是图标:

<div class="icon-registration hover15">
  <div class="bw pic col-xs-6 w20pr pad0" onclick="regType(1,'image_1')"> <img id="image_1" class="w85" src="/images/child.png"><label class="txt-uppercase child">Деца и Юноши</label></div>
  <div class="bw pic col-xs-6 w20pr pad0" onclick="regType(2,'image_2')"> <img id="image_2" class="w85" src="/images/amatior.png"><label class="txt-uppercase amatior">Аматьор</label></div>
  <div class="bw pic col-xs-6 w20pr pad0" onclick="regType(3,'image_3')"> <img id="image_3" class="w85" src="/images/coach.png"><label class="txt-uppercase coach">Треньор</label></div>
  <div class="bw pic col-xs-6 w20pr pad0" onclick="regType(4,'image_4')"> <img id="image_4" class="w85" src="/images/manager.png"><label class="txt-uppercase manager">Мениджър</label></div>
  <div class="bw pic col-xs-6 w20pr pad0" onclick="regType(5,'image_5')"> <img id="image_5" class="w85" src="/images/scouts.png"><label class="txt-uppercase scouts">Скаут</label></div>
  <input id="reg_type" name="reg_type" type="hidden" value="<?=(isset($_GET['reg_type']) ? $_GET['reg_type'] : '' )?>">
</div>

这是一个javacript:

function regType(type, image) {
  var choose = type;
  var new_image = document.getElementById(image);

  document.getElementById('reg_type').value = choose;
  var x = document.createElement("STYLE");
  var t = document.createTextNode("#registration-container {display: block;}");
  x.appendChild(t);
  document.head.appendChild(x);

  document.getElementById(image).style.width = "100%";
}

1 个答案:

答案 0 :(得分:2)

您自己代码的最低更改

首先更改所有图片&#39;宽度为85%然后缩放所选图像

&#13;
&#13;
function regType(type, image) {
  var choose = type;
  var new_image = document.getElementById(image);
  var imgs = document.querySelectorAll("img");
  for (var i = 0; i < imgs.length; i++) {
    imgs[i].style.width = "85%";
  }
  document.getElementById('reg_type').value = choose;
  var x = document.createElement("STYLE");
  var t = document.createTextNode("#registration-container {display: block;}");
  x.appendChild(t);
  document.head.appendChild(x);

  document.getElementById(image).style.width = "100%";


}
&#13;
.icon-registration {
  margin-top: 3%;
  text-align: center;
  float: left;
  width: 100%;
}

.pic {
  width: 200px;
}

.icon-registration img {
  width: 85%;
}

.icon-registration img:hover {
  cursor: pointer;
}

.bw {
  -webkit-transition: all 1s ease;
  -moz-transition: all 1s ease;
  -o-transition: all 1s ease;
  -ms-transition: all 1s ease;
  transition: all 1s ease;
  float: left;
}

.icon-registration img:hover .icon-registration {
  color: darkred!important;
}

.bw:hover img {
  -webkit-filter: grayscale(100%);
  color: darkred!important;
}

.bw:hover .child {
  color: darkred!important;
  -webkit-filter: grayscale(0%)!important;
}

.bw:hover .amatior {
  color: #1101a5!important;
  -webkit-filter: grayscale(0%)!important;
}

.bw:hover .coach {
  color: #c1a909 !important;
  -webkit-filter: grayscale(0%)!important;
}

.bw:hover .manager {
  color: #71A61F!important;
  -webkit-filter: grayscale(0%)!important;
}

.pad0 {
  padding: 0px;
}

.w20pr {
  width: 20%;
}

.txt-uppercase {
  text-transform: uppercase;
}
&#13;
<div class="icon-registration hover15">
  <div class="bw pic col-xs-6 w20pr pad0" onclick="regType(1,'image_1')"> <img id="image_1" class="w85" src="http://orig09.deviantart.net/72b1/f/2009/072/d/4/tf2_demoman_emblem_by_ninjasaus.png"><label class="txt-uppercase child">Деца и Юноши</label></div>
  <div class="bw pic col-xs-6 w20pr pad0" onclick="regType(2,'image_2')"> <img id="image_2" class="w85" src="https://ih1.redbubble.net/image.262178105.4811/flat,800x800,075,f.jpg"><label class="txt-uppercase amatior">Аматьор</label></div>
  <div class="bw pic col-xs-6 w20pr pad0" onclick="regType(3,'image_3')"> <img id="image_3" class="w85" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRaatwwJsY22QOg2Hk-s72fq_ju5ULgFQmsy5yJznFrLbhzut9nFQ"><label class="txt-uppercase coach">Треньор</label></div>
  <div class="bw pic col-xs-6 w20pr pad0" onclick="regType(4,'image_4')"> <img id="image_4" class="w85" src="https://www.freeiconspng.com/uploads/scout-icon-4.png"><label class="txt-uppercase manager">Мениджър</label></div>
  <div class="bw pic col-xs-6 w20pr pad0" onclick="regType(5,'image_5')"> <img id="image_5" class="w85" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAAkFBMVEX///95VB5uQwBxSABvRQBtQQBxRwB2UBR3URh2UBV4UxtySgB0TQzXzsOUeVZ1ThCum4Tk3tb6+ffs6OLc1MqHZzzh2tGbgmPy7+uBXzDDtaXSyLyOcUvw7ejNwrWehmmDYjSmkHa5qZbBsqGmkHWOcUy6qpjIvK6smYKJakJ9WSVkMQCxoI1gKwBoOQCSdlRIvWQ5AAATEElEQVR4nNVdCZequBK+JiFAiIKgjYItLm2rjG/8///ugQn7IkuCd75z5py+p3uUotZUVar+/JEO21ntz49j8LwfwnA2m4Xh4f4Mjo/zfuXY8r9eKhz/FKxNgDA0qKlqGpkxEE1TTWpAjIC5Dk6+8+kHHQJnb+0QgoaZklUPopkGRGhn7f9LZDrXgCBItVbSitAoRCS4/heotOe3EECznXEN7DQhCG/zv1o17b0LMR1CXUolxdDd/61E+i6CavOzv8yLadLov9joNL8HFSLX/zQxVSxvENeSR9TIaCKAzXC9WwTHGMFitw5NDCJDRNVaSlUMb8tPk1TAdQ1qhFOnBlKIa3n+8rsqePb30vcslyjIoHqNuIL19QOU1OLbwkbZbhLTQMbF2i/fq5S93FuX6K8r1kkzsPU9wfO/wzIAtMy76HkDr5+ULb0g+r/KvKQg+LSwLheKWWIexgtvmF9zvAXGJVaayuKTNC4XoGhdTGz8+GNsve3/GLj4zlTwMRodVynQp0MczAV87jzAsCCuquJ+ItixbyD/riPbt7uK8tT2dVe0zSa4TR4FeDBvXwiEltjX7FgQ5mmk0BP6+e/wdcC5r9fQVsbXe1uUc0IEH74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sQxyIsATzAMNCQURV3E/EwTGWC1BMfJvY+PHHvG3b/zFwsR9ABYvPHddiGpXi8xAT44U37JU73gLj0gYMU/kofTGWAShX8HUDGUF5Qd67j/GC6P8qV+soCD5NX4xvCxvlMhQxo+e9WPsOuXB7ubd20V9X1pdoBramLIS04roGNe1COjWQQlzL85ffVUrt76XvWS5RUN28z8g2r6XdFB6E5Q3WV/SJGhduADbD9W4RHGMEi906NDFA0GgYQqtiePsbxLME30WwedoHm8humnG9StW0lovCKkTuVFXkvrD3LsRN3W2dQCiG7v5vyKw3wp7fQgCrS4+6UGdCEN7mfzV5HM41IAjSPgP34lUQJLh+KnQZAidyASgyJWbz+g7GOM2MK8g7S+4qCFmIZ8mvTYDiKckv85KS9VoDATEC5jo4+f9J4nKwndX+/DgGz/shjPcUhOHh/gyOj/N+5UygdP8HSQgdmzjHecIAAAAASUVORK5CYII=">
    <label
      class="txt-uppercase scouts">Скаут</label>
  </div>
  <input id="reg_type" name="reg_type" type="hidden" value="<?=(isset($_GET['reg_type']) ? $_GET['reg_type'] : '' )?>">
</div>
&#13;
&#13;
&#13;