gpt4 book ai didi

javascript - 上传图像并显示在可拖动的内部

转载 作者:行者123 更新时间:2023-11-28 17:29:14 26 4
gpt4 key购买 nike

我想知道如何在可拖动对象中显示图像。我有一些代码,但它没有按照我想要的方式工作。我想输入一些文本将其插入到我的可拖动对象中,并且能够单独上传图像并拖动它而不拖动文本。我想将图像上传到数据库中的文件夹和图像名称。请帮助我到处寻找,但我找不到答案

var z = 1; //value to make div overlappable

$('#addText').click(function (e) {
/** Make div draggable **/
$('<div />', {
class: 'ui-widget-content',
appendTo: '.container',
draggable: {
containment: 'parent',
start: function( event, ui ) {
$(this).css('z-index', ++z);
}
}
});
});


$(document).on("dblclick", '.text', function()
{
$(this).hide(); $(this).closest('.item').find('.edit_text').val($(this).text()).show();
});

$(document).on("click", ".edit_text", function()
{
return false;
});


$(document).on("click", function()
{
var editingText = $('.edit_text:visible');
if (editingText.length)
{
editingText.hide();
editingText.closest('.item').find('.text').text($(editingText).val()).show();
}
});


var count = 1;
var selectedDraggable;

ko.bindingHandlers.draggable={
init: function(element, valueAccessor, allBindingsAccessor, viewModel) {
$(element).draggable();
$(element).addClass('item' + count);
count++;
$(element).on('click', function () {
selectedDraggable = $(this);
})
}
};


var vm=function(){
var self=this;
self.items=ko.observableArray();
self.textContent = ko.observable('');
self.init=function(){
self.items([]);
}
self.remove=function(item){
console.log(item);
self.items.remove(item);
}
self.addNew = function() {
self.items.push( self.textContent() );
self.textContent('');
}
self.init();
}

ko.applyBindings(new vm());



function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();

reader.onload = function (e) {
$('#blah')
.attr('src', e.target.result)
.width(150)
.height(200);
};

reader.readAsDataURL(input.files[0]);
}
}
a:link {text-decoration:none;}    /* unvisited link */
a:visited {text-decoration:none;} /* visited link */
a:hover {text-decoration:none;} /* mouse over link */
a:active {text-decoration:none;} /* selected link */
.toolbar { background-color: lightgrey;
width: 490px;
height: 23px;
border: none;


position: none;

}.item{
width: 200px;
height: 200px;
padding: 0.5em;
background:transparent;
z-index: 1;
display:block;
}

.edit_text
{
display: none;
}

.fix_backround
{
background-color: transparent;
}

.container {background-color: lightgrey;
width: 500px;
height: 500px;
border: 2px solid;
position: relative;
overflow: auto;
}
  <p align="center">&nbsp;</p>
<p align="center"><textarea data-bind="value: textContent" Placeholder="Type text to append" rows="4" cols="21"></textarea>&nbsp;&nbsp;&nbsp;
<button data-bind="click: addNew">Create</button></p>


<p align="center"> <input type='file' onchange="readURL(this);" /></p>


<center>
<div class="container">
<div data-bind="foreach:items" class="fix_backround">
<div href="#" class="item" data-bind="draggable:true,droppable:true">
<span data-bind="click:$parent.remove">[x]</span><br/><br/>
<center><span class="text" data-bind="text:$data"></span><input class="edit_text"/></center>
<img id="blah" src="#" alt="your image" /> </div></div>
</div></center>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/knockout/2.3.0/knockout-min.js"></script>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script><script src="//cdnjs.cloudflare.com/ajax/libs/knockout/2.3.0/knockout-min.js"></script>
<link rel="stylesheet"
href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<link rel="stylesheet"
href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>

<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

<script src="http://circletype.labwire.ca/js/circletype.js"></script><script src="http://tympanus.net/Development/Arctext/js/jquery.arctext.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">

<link class="jsbin" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.0/jquery-ui.min.js"></script><!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->

最佳答案

我怀疑这段代码并没有完全按照您的要求执行,但您的问题含糊不清,所以我无法确定应该发生什么。

无论如何,这是一个工作示例:https://jsfiddle.net/Twisty/L81v3Ldh/3/

选择一个文件后,它会运行 readURL($(this)[0]); 并且渲染的图像可以拖动。

如果您通过表单添加文本,文本将附加到新的 div 并附加。它也是可拖动的。

HTML

<p align="center">&nbsp;</p>
<p align="center">
<textarea id="textContent" data-bind="value: textContent" Placeholder="Type text to append" rows="4" cols="21"></textarea>&nbsp;&nbsp;&nbsp;
<button data-bind="click: addNew" id="addText">Create</button>
</p>
<p align="center">
<input type='file' />
</p>
<center>
<div class="container">
<div data-bind="foreach:items" class="fix_backround">
<div href="#" class="item" data-bind="draggable:true,droppable:true">
<span data-bind="click:$parent.remove">[x]</span>
<br/>
<br/>
<center><span class="text" data-bind="text:$data"></span>
<input class="edit_text" />
</center>
<img id="blah" src="#" alt="your image" /> </div>
</div>
</div>
</center>

我必须添加一些 ID 标记,以便 JQuery 可以正确执行。

jQuery

var z = 1; //value to make div overlappable
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();

reader.onload = function(e) {
$('#blah')
.attr('src', e.target.result)
.width(150)
.height(200);
};

reader.readAsDataURL(input.files[0]);
}
}
$(function() {
$("input[type='file']").change(function() {
readURL($(this)[0]);
$("#blah").draggable({
containment: '.container'
});
});
$('#addText').click(function(e) {
/** Make div draggable **/
$('<div>', {
id: 'text_' + count,
class: 'ui-widget-content'
})
.html($("#textContent").val())
.draggable({
containment: 'parent',
start: function(event, ui) {
$(this).css('z-index', ++z);
}
})
.appendTo('.container');
});

$(document).on("dblclick", '.text', function() {
$(this).hide();
$(this).closest('.item').find('.edit_text').val($(this).text()).show();
});

$(document).on("click", ".edit_text", function() {
return false;
});

$(document).on("click", function() {
var editingText = $('.edit_text:visible');
if (editingText.length) {
editingText.hide();
editingText.closest('.item')
.find('.text')
.text($(editingText).val())
.show();
}
});
var count = 1;
var selectedDraggable;
});

我不熟悉 Knockout,它似乎没有正确加载。我怀疑它对您没有任何帮助,但由于我不知道它应该做什么或如何使用它,所以我在这个示例中将其注释掉了。

请随时评论或更新您的帖子以提供更多详细信息。

关于javascript - 上传图像并显示在可拖动的内部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37642949/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com