If you want the text box to retain its size when dragging and dropping, you can delete the following calls in the event handlers of the drag component:
$(".new-div").css("width", 'auto');
Final code:
$(".new-div").draggable({ containment: "#bord" });
In the code snippet below, I also made changes to the click event handler of the span element to keep the text box centered when the user types in new text. To get this behavior, I had to put an irreplaceable space in the field. Since this character is selected after clicking on message , the new content typed by the user will overwrite it.
Finally, a focus rectangle appeared in Chrome. This CSS attribute can be used to hide it:
.new-div:focus { outline: none; }
Credit: The range selection code was inspired by this answer given by Tim Down .
$(".new-div").draggable({ containment: "#bord" }); $(document).on("click", ".closeButton", function () { $(this).closest("div").remove(); }); $(".span1").on("click", function (e) { e.preventDefault(); $(".new-div").removeClass("active"); $(this).closest(".new-div").addClass("active"); if ($(this).text().trim() === "message") { $(this).html(" "); var range = document.createRange(); range.setStart(this, 0); range.setEnd(this, 1); var sel = window.getSelection(); sel.removeAllRanges(); sel.addRange(range); $(".new-div").focus(); } }); $("#font-size").on("change", function () { var v = $(this).val(); $(".new-div").css("font-size", v + "px"); }); $(".resizeButton").draggable({ containment: "#bord", drag: function () { $(".new-div").height($(".resizeButton").position().top + 17); $(".new-div").width($(".resizeButton").position().left + 17); $(".new-div").width($(".resizeButton").position().left + 17); $(".new-div").css({ "font-size": ($(".new-div").height() / 2.3) }); } });
.new-div { z-index: 1; position: absolute; width: auto; word-break: break-all; text-align: center; left: 30%; top: 15px; border: 2px solid black; } .new-div:focus { outline: none; } .parent-div { max-width: 236px; width: 236px; position: relative; overflow: hidden; } .closeButton { display: block; position: absolute; top: -10px; left: -10px; width: 27px; height: 27px; background: url('http://cdn-sg1.pgimgs.com/images/pg/close-button.png') no-repeat center center; } .resizeButton { display: block; position: absolute; bottom: -10px; right: -10px; width: 27px; height: 27px; background: url('http://img.freepik.com/free-icon/resize-button_318-99883.jpg') no-repeat center center; background-size: contain; cursor: resize; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.11.1/jquery-ui.min.js"></script> <div class="col-sm-12"> <div class="parent-div"> <div class="new-div" contenteditable="true"> <span data-scale-ratio="1" class="span1" data-scale-reference=".new-div"> message </span> <a class="closeButton"></a> <a class="resizeButton"></a> </div> <div class="bord" style="z-index: -1;"> <img src="https://s-media-cache-ak0.pinimg.com/236x/8b/8a/00/8b8a007ae01adf400e12b26f3b93fb3a.jpg"> </div>
source share