css 如何使用js使所有图像元素可移动/可拖动

pftdvrlh  于 2024-01-09  发布在  其他
关注(0)|答案(1)|浏览(131)

我正在制作一个拼贴应用程序,我把一些效果。当你点击选择文件,它会创建图像文件,并把图像。你可以添加边框,做一个背景颜色/图像。但我想使这些图像可移动。你能帮我吗?
代码如下:

$(function() {
    // Multiple images preview in browser
    var imagesPreview = function(input, placeToInsertImagePreview) {

        if (input.files) {
            var filesAmount = input.files.length;

            for (i = 0; i < filesAmount; i++) {
                var reader = new FileReader();

                reader.onload = function(event) {
                    $($.parseHTML('<img>')).attr('src', event.target.result).attr("classname", "img").attr("draggable", "true").appendTo(placeToInsertImagePreview);
                }

                reader.readAsDataURL(input.files[i]);
            }
        }

    };

    $('#gallery-photo-add').on('change', function() {
        imagesPreview(this, 'div.gallery');
    });
});

function border() {
    var color = document.getElementById('bordertext').value;
  document.getElementById("gallery").style.border = color;
}
function backgroundclr() {
    var black = document.getElementById('colortxt').value
  document.getElementById('gallery').style.backgroundColor = black ;
}
function previewFile(fileInput) {
  var file = fileInput.files[0];
  var reader = new FileReader();

  reader.addEventListener("load", function() {
    setBackground(reader.result);
  }, false);

  if (file) {
    reader.readAsDataURL(file);
  }
}
function setBackground(imageURL){
    document.getElementById('gallery').style.backgroundImage = "url(" + imageURL + ")";
    document.getElementById('gallery').style.backgroundSize = "100% auto";
    document.getElementById('gallery').style.backgroundRepeat = "no-repeat";
    document.getElementById('gallery').style.backgroundPosition = "center top";
}
img{
            min-width: 15px;
            max-width:400px;
        
  
  cursor: move;
 
}
        }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
  <script src="https://html2canvas.hertzen.com/dist/html2canvas.js"></script>
<input type="file" multiple id="gallery-photo-add">
<div class="gallery" id="gallery" ></div>
  <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
         border 
  </button><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal2">
    Background Image
  </button>
  <div class="modal" id="myModal">
    <div class="modal-dialog">
      <div class="modal-content">
      
        <!-- Modal Header -->
        <div class="modal-header">
          <h4 class="modal-title">Border</h4>
          <button type="button" class="close" data-dismiss="modal">&times;</button>
        </div>
        
        <!-- Modal body -->
        <div class="modal-body">
          Please choose the pixel, type of border, color
          <br>
          <i>Example:<br>7px solid blue</i>
          <input type="text" id="bordertext">
        </div>
        
        <!-- Modal footer -->
        <div class="modal-footer">
          <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
          <button type="btn" class="btn-primary"  data-dismiss="modal" onclick="border()">Set border</button>
        </div>
        
      </div>
    </div>
  </div>
  <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal1">
    Background Color
  </button>

  <!-- The Modal -->
  <div class="modal" id="myModal1">
    <div class="modal-dialog">
      <div class="modal-content">
      
        <!-- Modal Header -->
        <div class="modal-header">
          <h4 class="modal-title">Modal Heading</h4>
          <button type="button" class="close" data-dismiss="modal">&times;</button>
        </div>
        
        <!-- Modal body -->
        <div class="modal-body">
         Please choose the background color.
         <input type="text" id="colortxt">
        </div>
        
        <!-- Modal footer -->
        <div class="modal-footer">
          <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
          <button type="btn" class="btn-primary"  data-dismiss="modal" onclick="backgroundclr()">Set border</button>
        </div>
        
      </div>
    </div>
  </div>
  
</div>
<div class="container">
  
  <!-- Button to Open the Modal -->
  <br>
  

  <!-- The Modal -->
  <div class="modal" id="myModal2">
    <div class="modal-dialog">
      <div class="modal-content">
      
        <!-- Modal Header -->
        <div class="modal-header">
          <h4 class="modal-title">Background Image</h4>
          
          <button type="button" class="close" data-dismiss="modal">&times;</button>
        </div>
        
        <!-- Modal body -->
        <div class="modal-body">
          Upload your background image.
          <input type="file" onchange="previewFile(this);">
        </div>
        
        <!-- Modal footer -->
        <div class="modal-footer">
          <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
        </div>
        
      </div>
    </div>
  </div>
  
</div>

请帮助我。希望,你喜欢它,我们可以得到一个解决方案。

相关问题