简体   繁体   English

如何使用javascript和html中的范围滑块更改图像大小?

[英]how to change images size using range slider in javascript and html?

the proplem is i have diffrents images all presented by id in selected option 问题是我在选择的选项中有所有由id表示的图像
the images from selected option , i want to change size any image that selected by range slider 选中选项中的图像,我想更改范围滑块选择的任何图像的大小
anyone can help plz? 任何人都可以帮助plz?

 var ranger = document.getElementById('ranger'); var image1 = document.getElementById('image1'); var width = image1.width; var height = image1.height; ranger.onchange = function(){ image1.width = width * (ranger.value / 1); image1.height = height * (ranger.value / 1); } 
 <div class="custom-select" > <select onchange="$('#image1').attr('src', this.options[this.selectedIndex].value);" id="select"> <option value=''> </option> <option value='0-0.png'>0-0</option> <option value='0-1.png'>0-1</option> <option value='0-2.png'>0-2</option> <option value='0-3.png'>0-3</option> <option value='0-4.png'>0-4</option> <option value='0-5.png'>0-5</option> </select> </div> <img id="image1" width="100 " height="200 " > <div class="slidecontainer"> <input id="ranger" type="range" min="1" max="5" value="0" step="1" class="slider" style="width: 500px" > </div> 

HTML: HTML:

<select id="mySelect" onchange="setImage();">
  <option value="0-0.jpeg">0-0</option>
  <option value="0-1.jpeg">0-1</option>
  <option value="0-2.jpeg">0-2</option>
  <option value="0-3.jpeg">0-3</option>
  <option value="0-4.jpeg">0-4</option>
  <option value="0-5.jpeg">0-5</option>
</select>

<input id="mySlider" type="range" min="1" max="5" value="1" step="1" onchange="resizeImage();" style="width:500px;">

<div id="img-wrapper" style="width:200px; height:200px;">
  <img src="0-0.jpeg" alt="My Image!" id="myImage" style="width:100%; height:1005;" />
</div>

Javascript: Javascript:

function setImage() {
  var e = document.getElementById('mySelect');
  document.getElementById('myImage').src = e.options[e.selectedIndex].value;
}

function resizeImage() {
  var image = document.getElementById('img-wrapper'),
      ranger = document.getElementById('mySlider');
  image.style.width = 200*(mySlider.value / 1)+'px';
}

Note that (in this case), you do not need to set width and height - changing one will automatically update the other ;) 请注意(在这种情况下),您无需设置width height -更改其中一个会自动更新另一个;)

I think you should change the height and width by usin .attr() 我认为您应该使用.attr()更改高度和宽度

Html : HTML:

   <img id="image" src="image.jpg" width="100 " height="200 "  >
      <div class="slidecontainer">
      <input id="ranger" type="range" min="1" max="5" value="1" step="1" class="slider" style="width: 500px"  >

Script : 剧本:

var width = $("#image").attr("width");
var height = $("#image").attr("height");
$("#ranger").change( function() {
  $("#image").attr("width",width * (ranger.value / 1))
  $("#image").attr("height",height * (ranger.value / 1) )
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM