我现在有一个拖放功能,可将图像传送分为三个div的,我的问题是如何能够在div尺寸调整到插入它的形象,使所有的图像可以适应在短短的一个盒子..
也有可能追加所以每个人都适合每个图像的仅仅是ALT?
<!DOCTYPE HTML>
<html>
<head>
<style>
div {
width: 350px;
height: 70px;
padding: 10px;
border: 1px solid #aaaaaa;
}
</style>
<script>
function allowDrop(ev) {
ev.preventDefault();
}
function drag(ev) {
ev.dataTransfer.setData("text", ev.target.id);
}
function drop(ev) {
ev.preventDefault();
var data = ev.dataTransfer.getData("text");
ev.target.appendChild(document.getElementById(data));
}
</script>
</head>
<body>
<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div id="div3" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<br>
<img id="drag1" src="img_logo.gif" draggable="true" ondragstart="drag(event)" width="336" height="69">
<img id="drag2" src="img_logo.gif" draggable="true" ondragstart="drag(event)" width="336" height="69">
<img id="drag3" src="img_logo.gif" draggable="true" ondragstart="drag(event)" width="336" height="69">
</body>
</html>
您可以设置DIV尺寸一旦图象被使用JavaScript下降,只是删除从CSS填充,这样的股利可以使图像适应,见下文
function allowDrop(ev) {
ev.preventDefault();
}
function drag(ev) {
//console.log(ev.target.id)
ev.dataTransfer.setData("text", ev.target.id);
}
function drop(ev) {
ev.preventDefault();
var data = ev.dataTransfer.getData("text");
var img = document.getElementById(data);
ev.target.appendChild(img);
//set the div width & height aacording to the image width
ev.target.style.width = parseInt(img.width) + 'px';
ev.target.style.height = parseInt(img.height) + 'px';
}
div {
border: 1px solid #aaaaaa;
width: 350px;
height: 70px;
}
<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<br/>
<div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<br/>
<div id="div3" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<br>
<img id="drag1" src="https://images.unsplash.com/photo-1507146426996-ef05306b995a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=336&q=69" draggable="true" ondragstart="drag(event)">
<img id="drag2" src="https://images.pexels.com/photos/33053/dog-young-dog-small-dog-maltese.jpg?auto=compress&cs=tinysrgb&dpr=2&w=336&h=69" draggable="true" ondragstart="drag(event)">
<img id="drag3" src="https://images.pexels.com/photos/257540/pexels-photo-257540.jpeg?auto=compress&cs=tinysrgb&h=69&w=336" draggable="true" ondragstart="drag(event)">