简体   繁体   English

动态更改div的位置

[英]Change the position of a div dynamically

I have a image upload button on my page and the area to preview those images. 我的页面上有一个图像上传按钮,还有一个预览这些图像的区域。 I just want to change the way of displaying those images. 我只想更改显示这些图像的方式。 Currently I have like this - 目前我有这样- 在此处输入图片说明

Where as I want to preview in this way - 我想以这种方式预览的地方-

在此处输入图片说明

Bit of explanation - When user uploads an image, uploaded image should replace the upload button div and button box will shift to right. 一点解释-用户上传图片时,上传的图片应替换上传按钮div,按钮框将向右移动。 When user uploads next image, same process would be repeated. 当用户上传下一张图像时,将重复相同的过程。 Upload button box will keep on shifting to right the image will take its place. 上传按钮框将继续向右移动,图像将取代其位置。

       for (var i=1; i<obj.image.length; i++) {
        var img_div_pre = document.createElement("div");

            img_div_pre.className = "col-md-4 col-lg-4 col-sm-6 clearfix pre_uploaded-img-wrapper";
            img_div_pre.id = "pre_uploaded-img-wrapper" +obj.image[i].image_id;

            img_div_pre.innerHTML = "<a href='"+obj.image[i].url+"' target='_blank'><img class='dl-new-iamges img-responsive' id='"+obj.image[i].image_id+"' src='"+obj.image[i].url+"' /></a>";

            img_div_pre.appendChild(pre_innerDiv);



            pre_uploaded_img_parent.appendChild(img_div_pre);
    }

Here, I am getting img source and id in an object(obj) from the server through Ajax. 在这里,我通过Ajax从服务器获取img源和id(obj)中的id。 Selected image first going to server and in response obj is returned. 所选图像首先进入服务器,然后返回obj。 Thanks. 谢谢。

There are two things involved in your problem. 您的问题涉及两件事。

  1. Previewing image once it is selected. 选择图像后预览图像。 For this use this code: 为此,请使用以下代码:

     function preview(){ var preview = new FileReader(); preview.onload = function (e) { // code to set height/width etc of preview element. // An img element is already present with src="#" // let us assume id of this element is "image". // preview will just src to this img element. }; var file = document.getELementById("file"); preview.readAsDataURL(file.files[0]); } 

2. Now you have to create one more Element where you can show new upload file dialog. 2.现在,您必须再创建一个元素,在其中可以显示新的上传文件对话框。

var uploadHtml = document.getElementById("file").innerHtml;
document.getElementById("file").innerHtml = "";
var newDiv = document.createElement("div");
var img = document.getElementById("img");
newDiv.appendChild(uploadHtml);
document.body.insertAdjacentHTML(img, newDiv);

Hope it was helpful. 希望对您有所帮助。

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

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